-
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
Merged
+170
−2
Merged
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
c0bd9b1
PYTHON-5904
ronan-merrick 2293e36
added regression tests for PYTHON-5904
ronan-merrick b7961a7
PR changes
ronan-merrick d6aad63
added synchronous files and improved comment
ronan-merrick 1c674b7
updated synchro configuration and generated synchronous test file
ronan-merrick 369ceec
Merge branch 'master' into master
NoahStapp bbdaa93
Merge branch 'master' into master
NoahStapp 1a3c11b
changed test_hello_latched.py to use AsyncUnitTest
ronan-merrick 53759db
Merge branch 'master' of github.com:ronan-merrick/mongo-python-driver
ronan-merrick 3f5e597
narrowed unasync replacement for test_hello_latched.py
ronan-merrick e7b22d5
inlined mock_conn_command in test_hello_latched and removed the mock …
ronan-merrick ca14790
Merge branch 'master' into master
ronan-merrick cdf1083
Merge branch 'main' into master
aclark4life File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,82 @@ | ||
| # Copyright 2026-present MongoDB, Inc. | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
|
|
||
| """Test that connection continues to use hello after ismaster returns hello_ok""" | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| import unittest | ||
| from types import SimpleNamespace | ||
|
|
||
| from pymongo.asynchronous.pool import AsyncConnection | ||
| from test.asynchronous import AsyncUnitTest | ||
|
|
||
| _IS_SYNC = False | ||
|
|
||
|
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(AsyncUnitTest): | ||
| 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 test_hello_is_latched(self): | ||
| """ | ||
| Regression Test for PYTHON-5904 | ||
| Tests for connection hello_ok persistence when connection | ||
| Switches from ismaster to hello | ||
| """ | ||
|
|
||
| async def mock_conn_command(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} | ||
|
|
||
| conn = self.create_connection() | ||
| conn.command = 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[2].get("ismaster", None)) | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| unittest.main() | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,82 @@ | ||
| # Copyright 2026-present MongoDB, Inc. | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
|
|
||
| """Test that connection continues to use hello after ismaster returns hello_ok""" | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| import unittest | ||
| from types import SimpleNamespace | ||
|
|
||
| from pymongo.synchronous.pool import Connection | ||
| from test import UnitTest | ||
|
|
||
| _IS_SYNC = True | ||
|
|
||
|
|
||
| class TestHelloLatched(UnitTest): | ||
| def setUp(self): | ||
| self._sent = [] | ||
|
|
||
| def create_connection(self) -> Connection: | ||
| """Returns a minimal connection object for _hello""" | ||
| conn = object.__new__(Connection) | ||
| conn.hello_ok = False | ||
| conn.performed_handshake = True | ||
| conn.opts = SimpleNamespace(server_api=None, load_balanced=False, _credentials=None) | ||
|
|
||
| return conn | ||
|
|
||
| def test_hello_is_latched(self): | ||
| """ | ||
| Regression Test for PYTHON-5904 | ||
| Tests for connection hello_ok persistence when connection | ||
| Switches from ismaster to hello | ||
| """ | ||
|
|
||
| def mock_conn_command(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} | ||
|
|
||
| conn = self.create_connection() | ||
| conn.command = mock_conn_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[2].get("ismaster", None)) | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| unittest.main() |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Needs the standard license header, see another test file for an example.