374
374
t.put_file, 'a', StringIO('some text for a\n'))
377
t.put_file('a', StringIO('some text for a\n'))
377
result = t.put_file('a', StringIO('some text for a\n'))
378
# put_file returns the length of the data written
379
self.assertEqual(16, result)
378
380
self.failUnless(t.has('a'))
379
381
self.check_transport_contents('some text for a\n', t, 'a')
380
382
# Put also replaces contents
381
t.put_file('a', StringIO('new\ncontents for\na\n'))
383
result = t.put_file('a', StringIO('new\ncontents for\na\n'))
384
self.assertEqual(19, result)
382
385
self.check_transport_contents('new\ncontents for\na\n', t, 'a')
383
386
self.assertRaises(NoSuchFile,
384
387
t.put_file, 'path/doesnt/exist/c',
1485
1488
self.assertEqual(d[2], (0, '0'))
1486
1489
self.assertEqual(d[3], (3, '34'))
1491
def test_readv_with_adjust_for_latency(self):
1492
transport = self.get_transport()
1493
# the adjust for latency flag expands the data region returned
1494
# according to a per-transport heuristic, so testing is a little
1495
# tricky as we need more data than the largest combining that our
1496
# transports do. To accomodate this we generate random data and cross
1497
# reference the returned data with the random data. To avoid doing
1498
# multiple large random byte look ups we do several tests on the same
1500
content = osutils.rand_bytes(200*1024)
1501
content_size = len(content)
1502
if transport.is_readonly():
1503
file('a', 'w').write(content)
1505
transport.put_bytes('a', content)
1506
def check_result_data(result_vector):
1507
for item in result_vector:
1508
data_len = len(item[1])
1509
self.assertEqual(content[item[0]:item[0] + data_len], item[1])
1512
result = list(transport.readv('a', ((0, 30),),
1513
adjust_for_latency=True, upper_limit=content_size))
1514
# we expect 1 result, from 0, to something > 30
1515
self.assertEqual(1, len(result))
1516
self.assertEqual(0, result[0][0])
1517
self.assertTrue(len(result[0][1]) >= 30)
1518
check_result_data(result)
1519
# end of file corner case
1520
result = list(transport.readv('a', ((204700, 100),),
1521
adjust_for_latency=True, upper_limit=content_size))
1522
# we expect 1 result, from 204800- its length, to the end
1523
self.assertEqual(1, len(result))
1524
data_len = len(result[0][1])
1525
self.assertEqual(204800-data_len, result[0][0])
1526
self.assertTrue(data_len >= 100)
1527
check_result_data(result)
1528
# out of order ranges are made in order
1529
result = list(transport.readv('a', ((204700, 100), (0, 50)),
1530
adjust_for_latency=True, upper_limit=content_size))
1531
# we expect 2 results, in order, start and end.
1532
self.assertEqual(2, len(result))
1534
data_len = len(result[0][1])
1535
self.assertEqual(0, result[0][0])
1536
self.assertTrue(data_len >= 30)
1538
data_len = len(result[1][1])
1539
self.assertEqual(204800-data_len, result[1][0])
1540
self.assertTrue(data_len >= 100)
1541
check_result_data(result)
1542
# close ranges get combined (even if out of order)
1543
for request_vector in [((400,50), (800, 234)), ((800, 234), (400,50))]:
1544
result = list(transport.readv('a', request_vector,
1545
adjust_for_latency=True, upper_limit=content_size))
1546
self.assertEqual(1, len(result))
1547
data_len = len(result[0][1])
1548
# minimmum length is from 400 to 1034 - 634
1549
self.assertTrue(data_len >= 634)
1550
# must contain the region 400 to 1034
1551
self.assertTrue(result[0][0] <= 400)
1552
self.assertTrue(result[0][0] + data_len >= 1034)
1553
check_result_data(result)
1488
1556
def test_get_with_open_write_stream_sees_all_content(self):
1489
1557
t = self.get_transport()
1490
1558
if t.is_readonly():