-
Notifications
You must be signed in to change notification settings - Fork 1.2k
PYTHON-5904 RTT Thread Flips Between isMaster and hello #2904
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 2 commits
c0bd9b1
2293e36
b7961a7
d6aad63
1c674b7
369ceec
bbdaa93
1a3c11b
53759db
3f5e597
e7b22d5
ca14790
cdf1083
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -0,0 +1,67 @@ | ||||||
| import unittest | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Needs the standard license header, see another test file for an example. |
||||||
| from unittest.mock import AsyncMock | ||||||
| from types import SimpleNamespace | ||||||
| from pymongo.asynchronous.pool import AsyncConnection | ||||||
|
|
||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The line
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No worries, I will make these changes. |
||||||
| class TestHelloLatched(unittest.IsolatedAsyncioTestCase): | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
|
|
||||||
| def setUp(self): | ||||||
| self._sent = [] | ||||||
|
|
||||||
| def create_connection(self) -> AsyncConnection: | ||||||
| """Returns a minimal connection object for _hello""" | ||||||
| conn = object.__new__(AsyncConnection) | ||||||
| conn.hello_ok = False | ||||||
| conn.performed_handshake = True | ||||||
| conn.opts = SimpleNamespace( | ||||||
| server_api = None, | ||||||
| load_balanced = False, | ||||||
| _credentials = None | ||||||
| ) | ||||||
|
|
||||||
| return conn | ||||||
|
|
||||||
| async def mock_conn_command(self, db, cmd, **kwargs): | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This isn't used elsewhere, let's inline it within
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. no problem. |
||||||
| """Returns mocked hello and ismaster results for conn.command""" | ||||||
| self._sent.append(cmd.copy()) | ||||||
| if cmd.get("ismaster") == 1: | ||||||
| return {"ok":1, "helloOk": True, "ismaster": True, "maxWireVersion": 25} | ||||||
| return {"ok":1, "isWritablePrimary": True, "maxWireVersion": 25} | ||||||
|
|
||||||
|
|
||||||
| async def test_hello_is_latched(self): | ||||||
| """ | ||||||
| Regression Test for PYTHON-5904 | ||||||
| Tests for connection hello_ok persistence when connection | ||||||
| Switches from ismaster to hello | ||||||
| """ | ||||||
| conn = self.create_connection() | ||||||
| conn.command = AsyncMock(side_effect=self.mock_conn_command) | ||||||
|
|
||||||
| # First hello | ||||||
| await conn._hello(None, None) | ||||||
| # Verify hello_ok is True | ||||||
| self.assertTrue(conn.hello_ok) | ||||||
| # Verify command sent is ismaster | ||||||
| self.assertEqual(self._sent[0].get("ismaster"), 1) | ||||||
| self.assertEqual(self._sent[0].get("helloOk"), True) | ||||||
|
|
||||||
| # Second hello | ||||||
| await conn._hello(None, None) | ||||||
| # Verify hello_ok has not changed | ||||||
| self.assertTrue(conn.hello_ok) | ||||||
| # Verify command sent is hello | ||||||
| self.assertEqual(self._sent[1].get("hello"), 1) | ||||||
| self.assertIsNone(self._sent[1].get("ismaster", None)) | ||||||
|
|
||||||
| # Third hello | ||||||
| await conn._hello(None, None) | ||||||
| # Verify connection continues to use hello | ||||||
| self.assertEqual(self._sent[2].get("hello"), 1) | ||||||
| self.assertIsNone(self._sent[1].get("ismaster", None)) | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
|
|
||||||
|
|
||||||
| if __name__ == "__main__": | ||||||
| unittest.main() | ||||||
|
|
||||||
|
|
||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,74 @@ | ||
| import unittest | ||
| from unittest.mock import Mock | ||
| from types import SimpleNamespace | ||
| from pymongo.synchronous.pool import Connection | ||
|
|
||
|
|
||
| class TestHelloLatched(unittest.TestCase): | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This file must be generated using the
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. got it. I misunderstood that these run on commit. |
||
| """ | ||
| Test to Ensure that hello_ok remains latched | ||
| when the RTT thread switches to hello | ||
| """ | ||
|
|
||
| def setUp(self): | ||
| self._sent = [] | ||
|
|
||
| def create_connection(self) -> Connection: | ||
| """Returns a minimal connection object""" | ||
| conn = object.__new__(Connection) | ||
| conn.hello_ok = False | ||
| # Isolate this test to post-handshake hello_ok persistence | ||
| conn.performed_handshake = True | ||
| conn.opts = SimpleNamespace( | ||
| server_api = None, | ||
| load_balanced = False, | ||
| _credentials = None | ||
| ) | ||
|
|
||
| return conn | ||
|
|
||
| def mock_connection_command(self, db, cmd, **kwargs): | ||
| """Returns mocked hello and ismaster results for conn.command""" | ||
| self._sent.append(cmd.copy()) | ||
| if cmd.get("ismaster") == 1: | ||
| return {"ok":1, "helloOk": True, "ismaster": True, "maxWireVersion": 25} | ||
| return {"ok":1, "isWritablePrimary": True, "maxWireVersion": 25} | ||
|
|
||
|
|
||
| def test_hello_is_latched(self): | ||
| """ | ||
| Regression Test for PYTHON-5904 | ||
| Tests for connection hello_ok persistence when connection | ||
| Switches from ismaster to hello | ||
| """ | ||
|
|
||
| conn = self.create_connection() | ||
| conn.command = Mock(side_effect=self.mock_connection_command) | ||
|
|
||
| # First hello | ||
| conn._hello(None, None) | ||
| # Verify hello_ok is True | ||
| self.assertTrue(conn.hello_ok) | ||
| # Verify command sent is ismaster | ||
| self.assertEqual(self._sent[0].get("ismaster"), 1) | ||
| self.assertEqual(self._sent[0].get("helloOk"), True) | ||
|
|
||
| # Second hello | ||
| conn._hello(None, None) | ||
| # Verify hello_ok has not changed | ||
| self.assertTrue(conn.hello_ok) | ||
| # Verify command sent is hello | ||
| self.assertEqual(self._sent[1].get("hello"), 1) | ||
| self.assertIsNone(self._sent[1].get("ismaster", None)) | ||
|
|
||
| # Third hello | ||
| conn._hello(None, None) | ||
| # Verify connection continues to use hello | ||
| self.assertEqual(self._sent[2].get("hello"), 1) | ||
| self.assertIsNone(self._sent[1].get("ismaster", None)) | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| unittest.main() | ||
|
|
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you make this comment a little more informative rather than just the ticket name?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
no problem.