Bug 764661 pt 2 - fix test to avoid sporadic KeyError; r=telliott
authorRyan Kelly <rfkelly@mozilla.com>
Tue, 23 Oct 2012 09:16:37 +1100
changeset 242 a08c5268c96223c3fcc61bd2c89880c325b39d92
parent 241 fee9281853e590c2c4acab1097f32ca2d3ad5508
child 243 490cf66fe3a53be4a2b0972b8d0cdddad83d8e7f
push id167
push userrkelly@mozilla.com
push dateMon, 22 Oct 2012 22:16:58 +0000
reviewerstelliott
bugs764661
Bug 764661 pt 2 - fix test to avoid sporadic KeyError; r=telliott
keyexchange/tests/test_filtering.py
--- a/keyexchange/tests/test_filtering.py
+++ b/keyexchange/tests/test_filtering.py
@@ -184,19 +184,31 @@ class TestIPFiltering(unittest.TestCase)
                 #   - add 10 elements
                 #   - remove 1
                 #   - save the list
                 self.blacklist.update()
 
                 for i in range(10):
                     self.blacklist.add(self.name + str(i))
 
-                # remove a random element
+                # Remove a random element.
+                # It's possible that multiple threads pick the same
+                # element to remove, producing a KeyError.  Loop until
+                # one is successfully removed.
                 ips = list(self.blacklist.ips)
-                self.blacklist.remove(random.choice(ips))
+                for _ in xrange(1000):
+                    try:
+                        self.blacklist.remove(random.choice(ips))
+                        break
+                    except KeyError:
+                        pass
+                else:
+                    msg = "Repeated collisions while trying to remove an "\
+                          "element, something is almost certainly wrong."
+                    raise Exception(msg)
 
                 # save the list
                 self.blacklist.save()
 
         workers = [Worker(str(i), blacklist) for i in range(10)]
         for worker in workers:
             worker.start()