PYTHON-5073 Convert test.test_connection_monitoring to async#2087
PYTHON-5073 Convert test.test_connection_monitoring to async#2087sleepyStick merged 16 commits intomongodb:masterfrom
Conversation
| # | ||
| async def test_1_client_connection_pool_options(self): | ||
| client = await self.async_rs_or_single_client(**self.POOL_OPTIONS) | ||
| self.addAsyncCleanup(client.close) |
There was a problem hiding this comment.
Our helper methods like self.async_rs_or_single_client call self.addAsyncCleanup(client.close) for us, so we don't need to do it ourselves.
| def start(self, op): | ||
| """Run the 'start' thread operation.""" | ||
| target = op["target"] | ||
| thread = SpecRunnerThread(target) |
There was a problem hiding this comment.
There's a separate ticket to create an asynchronous version of SpecRunnerThread using asyncio tasks: https://jira.mongodb.org/browse/PYTHON-4864. Until we implement that ticket, we don't want to use a thread-based SpecRunnerThread for asynchronous tests due to the poor performance and inconsistent behavior of mixing threads and asyncio.
There was a problem hiding this comment.
Makes sense, until that ticket is complete, what should I do for the tests? (or is this going to need to wait for that ticket to be completed?)
There was a problem hiding this comment.
wait ignore me, my brain is finally waking up and realizing that's what the last comment was for HAHA
|
|
||
| class AsyncTestCMAP(AsyncIntegrationTest): | ||
| # Location of JSON test specifications. | ||
| TEST_PATH = os.path.join(os.path.dirname(os.path.realpath(__file__)), "connection_monitoring") |
There was a problem hiding this comment.
We need to update this path to reflect the asynchronous subfolder inside test. The pattern is the same across all asynchronous tests that use a TEST_PATH:
if _IS_SYNC:
_TEST_PATH = os.path.join(pathlib.Path(__file__).resolve().parent, <module_name>)
else:
_TEST_PATH = os.path.join(pathlib.Path(__file__).resolve().parent.parent, <module_name>)
| return [scenario_def] | ||
|
|
||
|
|
||
| test_creator = CMAPSpecTestCreator(create_test, AsyncTestCMAP, AsyncTestCMAP.TEST_PATH) |
There was a problem hiding this comment.
To only run the SpecRunnerThread tests on the sync API:
if _IS_SYNC:
test_creator = CMAPSpecTestCreator(create_test, AsyncTestCMAP, AsyncTestCMAP.TEST_PATH)
test_creator.create_tests()
NoahStapp
left a comment
There was a problem hiding this comment.
Great work, just one minor cleanup!
| # Location of JSON test specifications. | ||
| TEST_PATH = os.path.join(os.path.dirname(os.path.realpath(__file__)), "connection_monitoring") | ||
| if _IS_SYNC: | ||
| TEST_PATH = os.path.join(pathlib.Path(__file__).resolve().parent, "connection_monitoring") |
There was a problem hiding this comment.
We can do Path instead of pathlib.Path and change the import to from pathlib import Path for clarity here.
|
|
||
| # Construct test from scenario. | ||
| for test_def in self.tests(scenario_def): | ||
| for test_def in await self.tests(scenario_def): |
There was a problem hiding this comment.
self.tests is not asynchronous, so no await needed here.
There was a problem hiding this comment.
okay i thought it was really weird too, but without the await i got the following error:
Traceback (most recent call last):
File "/opt/homebrew/Cellar/python@3.12/3.12.8/Frameworks/Python.framework/Versions/3.12/lib/python3.12/warnings.py", line 555, in _warn_unawaited_coroutine
warn(msg, category=RuntimeWarning, stacklevel=2, source=coro)
RuntimeWarning: coroutine 'CMAPSpecTestCreator.tests' was never awaited
test/asynchronous/test_connection_monitoring.py:None (test/asynchronous/test_connection_monitoring.py)
test_connection_monitoring.py:475: in <module>
test_creator.create_tests()
utils_spec_runner.py:243: in create_tests
asyncio.run(self._create_tests())
/opt/homebrew/Cellar/python@3.12/3.12.8/Frameworks/Python.framework/Versions/3.12/lib/python3.12/asyncio/runners.py:194: in run
return runner.run(main)
/opt/homebrew/Cellar/python@3.12/3.12.8/Frameworks/Python.framework/Versions/3.12/lib/python3.12/asyncio/runners.py:118: in run
return self._loop.run_until_complete(task)
/opt/homebrew/Cellar/python@3.12/3.12.8/Frameworks/Python.framework/Versions/3.12/lib/python3.12/asyncio/base_events.py:686: in run_until_complete
return future.result()
utils_spec_runner.py:225: in _create_tests
for test_def in self.tests(scenario_def):
E TypeError: 'coroutine' object is not iterable```
There was a problem hiding this comment.
Ah, found the issue:
class CMAPSpecTestCreator(AsyncSpecTestCreator):
async def tests(self, scenario_def):
...
should be
class CMAPSpecTestCreator(AsyncSpecTestCreator):
def tests(self, scenario_def):
...
And then await self.tests() can become self.tests()
There was a problem hiding this comment.
ahh! good find! thanks!
|
Encryption test failure is due to |
Should be fixed now :) |
Convert test.test_connection_monitoring to async