1262
1265
self.assertTrue(res is None)
1268
class GetContentMixinTestCase(ConnectedBaseTestCase):
1269
"""Test for GetContentMixin ActionQueueCommand subclass."""
1273
res = super(GetContentMixinTestCase, self).setUp()
1275
self.rq = RequestQueue(name='FOO', action_queue=self.action_queue)
1278
def test_progress(self):
1279
"""Test progress_hook invocation."""
1281
gunzip = mocker.mock()
1282
gunzip.decompress('some compressed bytes')
1283
mocker.result('some bytes')
1287
class MyGetContent(GetContentMixin):
1288
"""Subclass to check progress_hook invocation."""
1289
def progress_hook(innerself, n_bytes):
1290
calls.append((innerself.fileobj.getvalue(),
1292
# check just that is there
1293
super(MyGetContent, innerself).progress_hook(n_bytes)
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()
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)])
1265
1311
class ListDirTestCase(ConnectedBaseTestCase):
1266
1312
"""Test for ListDir ActionQueueCommand."""
1322
1368
self.assertTrue(res is None)
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])
1377
class DownloadUnconnectedTestCase(FactoryBaseTestCase):
1378
"""Test for Download ActionQueueCommand, no connection"""
1382
super(DownloadUnconnectedTestCase, self).setUp()
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
1391
def test_progress_information_setup(self):
1392
"""Test the setting up of the progress information in ._run()."""
1394
d = defer.Deferred()
1395
class FakedRequest():
1398
class FakedClient(object):
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()
1407
self.command.action_queue.connect_in_progress = False
1408
self.command.action_queue.client = FakedClient()
1411
self.assertEqual(self.command.n_bytes_read_last, 0)
1412
self.assertEqual(calls, [0])
1415
dloading = self.command.action_queue.downloading['a_share_id',
1417
dloading['n_bytes_read'] = 20
1420
self.assertEqual(self.command.n_bytes_read_last, 20)
1421
self.assertEqual(calls, [20])
1423
class DownloadTestCase(ConnectedBaseTestCase):
1424
"""Test for Download ActionQueueCommand."""
1428
res = super(DownloadTestCase, self).setUp()
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
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])
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)
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)
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)
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)
1471
class UploadUnconnectedTestCase(FactoryBaseTestCase):
1472
"""Test for Upload ActionQueueCommand, no connection"""
1476
super(UploadUnconnectedTestCase, self).setUp()
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
1487
def test_upload_progress_wrapper_setup(self):
1488
"""Test the setting up of the progress wrapper in ._run()."""
1490
d = defer.Deferred()
1491
class FakedRequest():
1494
class FakedClient(object):
1496
def put_content_request(innerself, *args):
1497
"""Fake a put content request with its deferred."""
1499
req = FakedRequest()
1503
self.command.action_queue.connect_in_progress = False
1504
self.command.action_queue.client = FakedClient()
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)
1515
self.command.n_bytes_written_last = 20
1517
self.assertEqual(self.command.n_bytes_written_last, 0)
1519
class UploadProgressWrapperTestCase(BaseTwistedTestCase):
1520
"""Test for the UploadProgressWrapper helper class."""
1522
def test_read(self):
1523
"""Test the read method."""
1526
def progress_hook(n_bytes_written):
1527
progress.append(n_bytes_written)
1529
info = {'n_bytes_written': 0}
1530
f = StringIO("x"*10+"y"*5)
1531
upw = UploadProgressWrapper(f, info, progress_hook)
1534
self.assertEqual(res, "x"*10)
1535
self.assertEqual(progress, [0])
1538
self.assertEqual(res, "y"*5)
1539
self.assertEqual(progress, [0, 10])
1331
1542
class UploadTestCase(ConnectedBaseTestCase):
1332
1543
"""Test for Upload ActionQueueCommand."""
1411
1622
events = [('AQ_UPLOAD_ERROR', (), kwargs)]
1412
1623
self.assertEquals(events, self.command.action_queue.event_queue.events)
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])
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
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)
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)
1646
self.command.n_bytes_written_last = 5
1647
self.command.progress_hook(TRANSFER_PROGRESS_THRESHOLD+14)
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)
1415
1658
class CreateShareTestCase(ConnectedBaseTestCase):
1416
1659
"""Test for CreateShare ActionQueueCommand."""