Merge "item allocator should return same value for same key" into stable/liberty

This commit is contained in:
Jenkins 2016-06-10 15:06:34 +00:00 committed by Gerrit Code Review
commit 5067c7b49a
2 changed files with 20 additions and 0 deletions

View File

@ -65,6 +65,9 @@ class ItemAllocator(object):
allocations to free the pool. This final desperate step will not
happen often in practice.
"""
if key in self.allocations:
return self.allocations[key]
if key in self.remembered:
self.allocations[key] = self.remembered.pop(key)
return self.allocations[key]

View File

@ -62,6 +62,23 @@ class TestItemAllocator(base.BaseTestCase):
self.assertTrue(test_object not in a.pool)
self.assertTrue(write.called)
def test_allocate_repeated_call_with_same_key(self):
test_pool = set([TestObject(33000), TestObject(33001),
TestObject(33002), TestObject(33003),
TestObject(33004), TestObject(33005)])
a = ia.ItemAllocator('/file', TestObject, test_pool)
with mock.patch.object(ia.ItemAllocator, '_write'):
test_object = a.allocate('test')
test_object1 = a.allocate('test')
test_object2 = a.allocate('test')
test_object3 = a.allocate('test1')
# same value for same key on repeated calls
self.assertEqual(test_object, test_object1)
self.assertEqual(test_object1, test_object2)
# values for different keys should be diffent
self.assertNotEqual(test_object, test_object3)
def test_allocate_from_file(self):
test_pool = set([TestObject(33000), TestObject(33001)])
with mock.patch.object(ia.ItemAllocator, '_read') as read: