Browse Source

Merge "Fix race in test_handler_poll_session_expired"

tags/3.4.0
Zuul 6 months ago
parent
commit
6aa1fff882
1 changed files with 35 additions and 12 deletions
  1. 35
    12
      nodepool/tests/unit/test_launcher.py

+ 35
- 12
nodepool/tests/unit/test_launcher.py View File

@@ -1655,31 +1655,54 @@ class TestLauncher(tests.DBTestCase):
1655 1655
         self.assertEqual(req.state, zk.FULFILLED)
1656 1656
         self.assertEqual(len(req.nodes), 1)
1657 1657
 
1658
-    @mock.patch(
1659
-        'nodepool.driver.openstack.handler.OpenStackNodeRequestHandler.poll')
1658
+    @mock.patch('nodepool.driver.NodeRequestHandler.poll')
1660 1659
     def test_handler_poll_session_expired(self, mock_poll):
1661 1660
         '''
1662
-        Test ZK session lost during handler poll().
1661
+        Test ZK session lost during handler poll() removes handler.
1663 1662
         '''
1664
-        mock_poll.side_effect = kze.SessionExpiredError()
1663
+        req = zk.NodeRequest()
1664
+        req.state = zk.REQUESTED
1665
+        req.node_types.append('fake-label')
1666
+        self.zk.storeNodeRequest(req)
1667
+
1668
+        # We need to stop processing of this request so that it does not
1669
+        # re-enter request handling, so we can then verify that it was
1670
+        # actually removed from request_handlers in the final assert of
1671
+        # this test.
1672
+        def side_effect():
1673
+            req.state = zk.FAILED
1674
+            # Intentionally ignore that it is already locked.
1675
+            self.zk.storeNodeRequest(req)
1676
+            raise kze.SessionExpiredError()
1677
+
1678
+        mock_poll.side_effect = side_effect
1665 1679
 
1666 1680
         # use a config with min-ready of 0
1667 1681
         configfile = self.setup_config('node_launch_retry.yaml')
1668 1682
         self.useBuilder(configfile)
1683
+
1684
+        # Wait for the image to exist before starting the launcher, else
1685
+        # we'll decline the request.
1686
+        self.waitForImage('fake-provider', 'fake-image')
1687
+
1669 1688
         pool = self.useNodepool(configfile, watermark_sleep=1)
1670 1689
         pool.cleanup_interval = 60
1671 1690
         pool.start()
1672
-        self.waitForImage('fake-provider', 'fake-image')
1673 1691
 
1674
-        req = zk.NodeRequest()
1675
-        req.state = zk.REQUESTED
1676
-        req.node_types.append('fake-label')
1677
-        self.zk.storeNodeRequest(req)
1692
+        # Wait for request handling to occur
1693
+        while not mock_poll.call_count:
1694
+            time.sleep(.1)
1695
+
1696
+        # Note: The launcher is not setting FAILED state here, but our mock
1697
+        # side effect should be doing so. Just verify that.
1698
+        req = self.waitForNodeRequest(req)
1699
+        self.assertEqual(zk.FAILED, req.state)
1678 1700
 
1679 1701
         # A session loss during handler poll should at least remove the
1680
-        # request from active handlers
1681
-        req = self.waitForNodeRequest(req, states=(zk.PENDING,))
1682
-        self.assertEqual(1, mock_poll.call_count)
1702
+        # request from active handlers. The session exception from our first
1703
+        # time through poll() should handle removing the request handler.
1704
+        # And our mock side effect should ensure it does not re-enter
1705
+        # request handling before we check it.
1683 1706
         self.assertEqual(0, len(
1684 1707
             pool._pool_threads["fake-provider-main"].request_handlers))
1685 1708
 

Loading…
Cancel
Save