DVR: Check for item_allocator key before releasing

item_allocator class maintains the allocation of
items(link local address) from a given pool.
There may be cases where the key has been already
released or not being remembered while trying to
release it.
So it makes sense to lookup for the key before
releasing the key.

Change-Id: I34765dd3efa6c6742caeb7f6ae2ff14009dce6dd
Closes-Bug: #1761260
(cherry picked from commit ed7dd4dae0)
This commit is contained in:
Swaminathan Vasudevan 2018-04-04 11:33:56 -07:00
parent 48ae4b7ed1
commit 279a1e9243
2 changed files with 6 additions and 2 deletions

View File

@ -113,8 +113,9 @@ class ItemAllocator(object):
return self.allocations[key]
def release(self, key):
self.pool.add(self.allocations.pop(key))
self._write_allocations()
if self.lookup(key):
self.pool.add(self.allocations.pop(key))
self._write_allocations()
def _write_allocations(self):
current = ["%s,%s\n" % (k, v) for k, v in self.allocations.items()]

View File

@ -133,6 +133,9 @@ class TestItemAllocator(base.BaseTestCase):
allocation = a.allocate('deadbeef')
write.reset_mock()
a.release('deadbeef')
# Just try to release the item again to see if it
# throws any error
a.release('deadbeef')
self.assertNotIn('deadbeef', a.allocations)
self.assertIn(allocation, a.pool)