From 334dae0190f239a2cba4cebb9f8881e3817cee61 Mon Sep 17 00:00:00 2001 From: venkata anil Date: Tue, 12 Jan 2016 13:40:36 +0000 Subject: [PATCH] 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 --- neutron/agent/l3/item_allocator.py | 3 +++ .../tests/unit/agent/l3/test_item_allocator.py | 17 +++++++++++++++++ 2 files changed, 20 insertions(+) diff --git a/neutron/agent/l3/item_allocator.py b/neutron/agent/l3/item_allocator.py index 79e8ee1cf13..0d0ea74b5ef 100644 --- a/neutron/agent/l3/item_allocator.py +++ b/neutron/agent/l3/item_allocator.py @@ -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] diff --git a/neutron/tests/unit/agent/l3/test_item_allocator.py b/neutron/tests/unit/agent/l3/test_item_allocator.py index 7f4c365df49..f920c6bfbb5 100644 --- a/neutron/tests/unit/agent/l3/test_item_allocator.py +++ b/neutron/tests/unit/agent/l3/test_item_allocator.py @@ -62,6 +62,23 @@ class TestItemAllocator(base.BaseTestCase): self.assertNotIn(test_object, 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: