~nataliabidart/ubuntuone-client/split-oauth

« back to all changes in this revision

Viewing changes to tests/syncdaemon/test_action_queue.py

  • Committer: Tarmac
  • Author(s): Samuele Pedroni (Canonical Services Ltd.)
  • Date: 2010-05-18 14:05:52 UTC
  • mfrom: (511.1.17 transfer-progress-events)
  • Revision ID: dobey@wayofthemonkey.com-20100518140552-d3l55ob7yfdz0ekw
addressing the demands of Bug #570747, introduces internal events AQ_DOWNLOAD/UPLOAD_FILE_PROGRESS that then get converted into D-Bus signals Download/UploadFileProgress.

Show diffs side-by-side

added added

removed removed

Lines of Context:
40
40
from contrib.testing.testcase import (
41
41
    BaseTwistedTestCase, MementoHandler, DummyClass
42
42
)
 
43
from contrib.mocker import Mocker
43
44
 
44
45
from ubuntuone.storageprotocol import client, errors, protocol_pb2, volumes
45
46
from ubuntuone.syncdaemon import states
47
48
from ubuntuone.syncdaemon.main import Main
48
49
from ubuntuone.syncdaemon.action_queue import (
49
50
    ActionQueue, ActionQueueCommand, ChangePublicAccess, CreateUDF,
50
 
    DeleteVolume, ListDir, ListVolumes, NoisyRequestQueue, RequestQueue,
51
 
    Upload, CreateShare, GetPublicFiles,
 
51
    DeleteVolume, Download, GetContentMixin, ListDir, ListVolumes,
 
52
    NoisyRequestQueue, RequestQueue, UploadProgressWrapper, Upload,
 
53
    CreateShare, GetPublicFiles,
 
54
    TRANSFER_PROGRESS_THRESHOLD
52
55
)
53
56
from ubuntuone.syncdaemon.event_queue import EventQueue, EVENTS
54
57
from ubuntuone.syncdaemon.volume_manager import UDF
1262
1265
        self.assertTrue(res is None)
1263
1266
 
1264
1267
 
 
1268
class GetContentMixinTestCase(ConnectedBaseTestCase):
 
1269
    """Test for GetContentMixin ActionQueueCommand subclass."""
 
1270
 
 
1271
    def setUp(self):
 
1272
        """Init."""
 
1273
        res = super(GetContentMixinTestCase, self).setUp()
 
1274
 
 
1275
        self.rq = RequestQueue(name='FOO', action_queue=self.action_queue)
 
1276
        return res
 
1277
 
 
1278
    def test_progress(self):
 
1279
        """Test progress_hook invocation."""
 
1280
        mocker = Mocker()
 
1281
        gunzip = mocker.mock()
 
1282
        gunzip.decompress('some compressed bytes')
 
1283
        mocker.result('some bytes')
 
1284
        mocker.replay()
 
1285
 
 
1286
        calls = []
 
1287
        class MyGetContent(GetContentMixin):
 
1288
            """Subclass to check progress_hook invocation."""
 
1289
            def progress_hook(innerself, n_bytes):
 
1290
                calls.append((innerself.fileobj.getvalue(),
 
1291
                              n_bytes))
 
1292
                # check just that is there
 
1293
                super(MyGetContent, innerself).progress_hook(n_bytes)
 
1294
 
 
1295
        command = MyGetContent(self.rq, share_id='a_share_id',
 
1296
                               node_id='a_node_id', server_hash='a_server_hash',
 
1297
                               fileobj_factory=lambda: None)
 
1298
        command.gunzip = gunzip
 
1299
        command.fileobj = StringIO()
 
1300
        dloading = {
 
1301
            'n_bytes_read': 0
 
1302
        }
 
1303
        command.action_queue.downloading['a_share_id', 'a_node_id'] = dloading
 
1304
        command.cb('some compressed bytes')
 
1305
        expected = len('some compressed bytes')
 
1306
        self.assertEqual(calls, [('some bytes', expected)])
 
1307
        mocker.restore()
 
1308
        mocker.verify()
 
1309
 
 
1310
 
1265
1311
class ListDirTestCase(ConnectedBaseTestCase):
1266
1312
    """Test for ListDir ActionQueueCommand."""
1267
1313
 
1322
1368
        self.assertTrue(res is None)
1323
1369
 
1324
1370
    def test_AQ_DOWNLOAD_DOES_NOT_EXIST_is_valid_event(self):
1325
 
        """AQ_DOWNLOAD_DOES_NOT_EXIST is a valdi event."""
 
1371
        """AQ_DOWNLOAD_DOES_NOT_EXIST is a valid event."""
1326
1372
        event = 'AQ_DOWNLOAD_DOES_NOT_EXIST'
1327
1373
        self.assertTrue(event in EVENTS)
1328
1374
        self.assertEquals(('share_id', 'node_id'), EVENTS[event])
1329
1375
 
1330
1376
 
 
1377
class DownloadUnconnectedTestCase(FactoryBaseTestCase):
 
1378
    """Test for Download ActionQueueCommand, no connection"""
 
1379
    
 
1380
    def setUp(self):
 
1381
        """Init."""
 
1382
        super(DownloadUnconnectedTestCase, self).setUp()
 
1383
 
 
1384
        self.rq = request_queue = RequestQueue(name='FOO',
 
1385
                                               action_queue=self.action_queue)
 
1386
        self.command = Download(request_queue, share_id='a_share_id',
 
1387
                               node_id='a_node_id', server_hash='a_server_hash',
 
1388
                               fileobj_factory=lambda: None)
 
1389
        self.command.start_unqueued() # create the logger
 
1390
 
 
1391
    def test_progress_information_setup(self):
 
1392
        """Test the setting up of the progress information in ._run()."""
 
1393
        calls = []
 
1394
        d = defer.Deferred()
 
1395
        class FakedRequest():
 
1396
            """Fake Request."""
 
1397
            pass
 
1398
        class FakedClient(object):
 
1399
            """Fake Client."""
 
1400
            def get_content_request(innerself, *args, **kwds):
 
1401
                """Fake a get content request with its deferred."""
 
1402
                calls.append(kwds.get('offset'))
 
1403
                req = FakedRequest()
 
1404
                req.deferred = d
 
1405
                return req
 
1406
 
 
1407
        self.command.action_queue.connect_in_progress = False
 
1408
        self.command.action_queue.client = FakedClient()
 
1409
        self.command._run()
 
1410
 
 
1411
        self.assertEqual(self.command.n_bytes_read_last, 0)
 
1412
        self.assertEqual(calls, [0])
 
1413
 
 
1414
        calls = []
 
1415
        dloading = self.command.action_queue.downloading['a_share_id',
 
1416
                                                         'a_node_id']
 
1417
        dloading['n_bytes_read'] = 20
 
1418
        self.command._run()
 
1419
        
 
1420
        self.assertEqual(self.command.n_bytes_read_last, 20)        
 
1421
        self.assertEqual(calls, [20])
 
1422
        
 
1423
class DownloadTestCase(ConnectedBaseTestCase):
 
1424
    """Test for Download ActionQueueCommand."""
 
1425
 
 
1426
    def setUp(self):
 
1427
        """Init."""
 
1428
        res = super(DownloadTestCase, self).setUp()
 
1429
 
 
1430
        request_queue = RequestQueue(name='FOO', action_queue=self.action_queue)
 
1431
        self.command = Download(request_queue, share_id='a_share_id',
 
1432
                               node_id='a_node_id', server_hash='a_server_hash',
 
1433
                               fileobj_factory=lambda: None)
 
1434
        self.command.start_unqueued() # create the logger
 
1435
 
 
1436
        return res
 
1437
 
 
1438
    def test_AQ_DOWNLOAD_FILE_PROGRESS_is_valid_event(self):
 
1439
        """AQ_DOWNLOAD_FILE_PROGRESS is a valid event."""
 
1440
        event = 'AQ_DOWNLOAD_FILE_PROGRESS'
 
1441
        self.assertTrue(event in EVENTS)
 
1442
        self.assertEqual(('share_id', 'node_id', 'n_bytes_read',
 
1443
                          'deflated_size'), EVENTS[event])
 
1444
 
 
1445
    def test_progress_hook(self):
 
1446
        # would first get the node attribute including this
 
1447
        self.command.action_queue.downloading['a_share_id', 'a_node_id'] = {}
 
1448
        self.command.nacb(deflated_size = 2*TRANSFER_PROGRESS_THRESHOLD)
 
1449
        self.command.progress_start(0)
 
1450
 
 
1451
        self.command.progress_hook(5)
 
1452
        self.assertEqual([], self.command.action_queue.event_queue.events)
 
1453
        self.assertEqual(self.command.n_bytes_read_last, 5)
 
1454
 
 
1455
        self.command.progress_hook(TRANSFER_PROGRESS_THRESHOLD)
 
1456
        self.assertEqual([], self.command.action_queue.event_queue.events)
 
1457
        self.assertEqual(self.command.n_bytes_read_last,
 
1458
                         TRANSFER_PROGRESS_THRESHOLD)
 
1459
 
 
1460
        self.command.progress_start(5)
 
1461
        self.command.progress_hook(TRANSFER_PROGRESS_THRESHOLD+5)
 
1462
        kwargs = {'share_id': 'a_share_id', 'node_id': 'a_node_id',
 
1463
                  'deflated_size': 2*TRANSFER_PROGRESS_THRESHOLD,
 
1464
                  'n_bytes_read': 5+TRANSFER_PROGRESS_THRESHOLD}
 
1465
        events = [('AQ_DOWNLOAD_FILE_PROGRESS', (), kwargs)]
 
1466
        self.assertEqual(events, self.command.action_queue.event_queue.events)
 
1467
        self.assertEqual(self.command.n_bytes_read_last,
 
1468
                         5+TRANSFER_PROGRESS_THRESHOLD)
 
1469
 
 
1470
 
 
1471
class UploadUnconnectedTestCase(FactoryBaseTestCase):
 
1472
    """Test for Upload ActionQueueCommand, no connection"""
 
1473
    
 
1474
    def setUp(self):
 
1475
        """Init."""
 
1476
        super(UploadUnconnectedTestCase, self).setUp()
 
1477
 
 
1478
        self.rq = request_queue = RequestQueue(name='FOO',
 
1479
                                               action_queue=self.action_queue)
 
1480
        self.command = Upload(request_queue, share_id='a_share_id',
 
1481
                              node_id='a_node_id', previous_hash='prev_hash',
 
1482
                              hash='yadda', crc32=0, size=0,
 
1483
                              fileobj_factory=lambda: None,
 
1484
                              tempfile_factory=lambda: None)
 
1485
        self.command.start_unqueued() # create the logger
 
1486
 
 
1487
    def test_upload_progress_wrapper_setup(self):
 
1488
        """Test the setting up of the progress wrapper in ._run()."""
 
1489
        calls = []
 
1490
        d = defer.Deferred()
 
1491
        class FakedRequest():
 
1492
            """Fake Request."""
 
1493
            pass
 
1494
        class FakedClient(object):
 
1495
            """Fake Client."""
 
1496
            def put_content_request(innerself, *args):
 
1497
                """Fake a put content request with its deferred."""
 
1498
                calls.append(args)
 
1499
                req = FakedRequest()
 
1500
                req.deferred = d
 
1501
                return req
 
1502
 
 
1503
        self.command.action_queue.connect_in_progress = False
 
1504
        self.command.action_queue.client = FakedClient()
 
1505
        self.command._run()
 
1506
 
 
1507
        self.assertEqual(len(calls), 1)
 
1508
        upload_wrapper = calls[0][-1]
 
1509
        uploading = self.action_queue.uploading['a_share_id', 'a_node_id']
 
1510
        self.assertTrue(upload_wrapper.data_dict is uploading)
 
1511
        self.assertEqual(upload_wrapper.progress_hook,
 
1512
                         self.command.progress_hook)
 
1513
        self.assertEqual(self.command.n_bytes_written_last, 0)
 
1514
 
 
1515
        self.command.n_bytes_written_last = 20
 
1516
        self.command._run()
 
1517
        self.assertEqual(self.command.n_bytes_written_last, 0)        
 
1518
 
 
1519
class UploadProgressWrapperTestCase(BaseTwistedTestCase):
 
1520
    """Test for the UploadProgressWrapper helper class."""
 
1521
 
 
1522
    def test_read(self):
 
1523
        """Test the read method."""
 
1524
        progress = []
 
1525
        
 
1526
        def progress_hook(n_bytes_written):
 
1527
            progress.append(n_bytes_written)
 
1528
 
 
1529
        info = {'n_bytes_written': 0}
 
1530
        f = StringIO("x"*10+"y"*5)
 
1531
        upw = UploadProgressWrapper(f, info, progress_hook)
 
1532
 
 
1533
        res = upw.read(10)
 
1534
        self.assertEqual(res, "x"*10)
 
1535
        self.assertEqual(progress, [0])
 
1536
 
 
1537
        res = upw.read(5)
 
1538
        self.assertEqual(res, "y"*5)        
 
1539
        self.assertEqual(progress, [0, 10])        
 
1540
        
 
1541
 
1331
1542
class UploadTestCase(ConnectedBaseTestCase):
1332
1543
    """Test for Upload ActionQueueCommand."""
1333
1544
 
1411
1622
        events = [('AQ_UPLOAD_ERROR', (), kwargs)]
1412
1623
        self.assertEquals(events, self.command.action_queue.event_queue.events)
1413
1624
 
 
1625
    def test_AQ_UPLOAD_FILE_PROGRESS_is_valid_event(self):
 
1626
        """AQ_UPLOAD_FILE_PROGRESS is a valid event."""
 
1627
        event = 'AQ_UPLOAD_FILE_PROGRESS'
 
1628
        self.assertTrue(event in EVENTS)
 
1629
        self.assertEqual(('share_id', 'node_id', 'n_bytes_written',
 
1630
                          'deflated_size'), EVENTS[event])
 
1631
 
 
1632
    def test_progress_hook(self):
 
1633
        """Test the progress hook."""
 
1634
        self.command.deflated_size = 2*TRANSFER_PROGRESS_THRESHOLD
 
1635
        self.command.n_bytes_written_last = 0
 
1636
        
 
1637
        self.command.progress_hook(5)
 
1638
        self.assertEqual([], self.command.action_queue.event_queue.events)
 
1639
        self.assertEqual(self.command.n_bytes_written_last, 5)
 
1640
 
 
1641
        self.command.progress_hook(TRANSFER_PROGRESS_THRESHOLD)
 
1642
        self.assertEqual([], self.command.action_queue.event_queue.events)
 
1643
        self.assertEqual(self.command.n_bytes_written_last,
 
1644
                         TRANSFER_PROGRESS_THRESHOLD)        
 
1645
        
 
1646
        self.command.n_bytes_written_last = 5
 
1647
        self.command.progress_hook(TRANSFER_PROGRESS_THRESHOLD+14)        
 
1648
 
 
1649
        kwargs = {'share_id': 'a_share_id', 'node_id': 'a_node_id',
 
1650
                  'deflated_size': 2*TRANSFER_PROGRESS_THRESHOLD,
 
1651
                  'n_bytes_written': 14+TRANSFER_PROGRESS_THRESHOLD }
 
1652
        events = [('AQ_UPLOAD_FILE_PROGRESS', (), kwargs)]
 
1653
        self.assertEqual(events, self.command.action_queue.event_queue.events)
 
1654
        self.assertEqual(self.command.n_bytes_written_last,
 
1655
                         14+TRANSFER_PROGRESS_THRESHOLD)
 
1656
 
1414
1657
 
1415
1658
class CreateShareTestCase(ConnectedBaseTestCase):
1416
1659
    """Test for CreateShare ActionQueueCommand."""