item allocator should return same value for same key

When ItemAllocator.allocate called with same key again,
it is returning different value.
To fix this, we check if a value for key already exists in
self.allocations, and return that value if already exists.

Closes-bug: #1533216
Change-Id: I1f6191b07d33a1f542de18a942cefaf7ecb6c143
(cherry picked from commit 334dae0190)
This commit is contained in:
venkata anil 2016-01-12 13:40:36 +00:00 committed by Ritesh Anand
parent 483ceec083
commit 87d12c9a0f
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 allocations to free the pool. This final desperate step will not
happen often in practice. happen often in practice.
""" """
if key in self.allocations:
return self.allocations[key]
if key in self.remembered: if key in self.remembered:
self.allocations[key] = self.remembered.pop(key) self.allocations[key] = self.remembered.pop(key)
return self.allocations[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(test_object not in a.pool)
self.assertTrue(write.called) 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): def test_allocate_from_file(self):
test_pool = set([TestObject(33000), TestObject(33001)]) test_pool = set([TestObject(33000), TestObject(33001)])
with mock.patch.object(ia.ItemAllocator, '_read') as read: with mock.patch.object(ia.ItemAllocator, '_read') as read: