Merge "item allocator should return same value for same key"

This commit is contained in:
Jenkins 2016-02-23 09:19:38 +00:00 committed by Gerrit Code Review
commit f2ee8deb22
2 changed files with 20 additions and 0 deletions
neutron
agent/l3
tests/unit/agent/l3

@ -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]

@ -62,6 +62,23 @@ class TestItemAllocator(base.BaseTestCase):
self.assertNotIn(test_object, a.pool) self.assertNotIn(test_object, 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: