Skip to content
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

PYTHON-5073 Convert test.test_connection_monitoring to async #2087

Open
wants to merge 14 commits into
base: master
Choose a base branch
from
22 changes: 11 additions & 11 deletions test/asynchronous/test_connection_monitoring.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
from __future__ import annotations

import os
import pathlib
import sys
import time

Expand Down Expand Up @@ -83,7 +84,12 @@

class AsyncTestCMAP(AsyncIntegrationTest):
# 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")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can do Path instead of pathlib.Path and change the import to from pathlib import Path for clarity here.

else:
TEST_PATH = os.path.join(
pathlib.Path(__file__).resolve().parent.parent, "connection_monitoring"
)

# Test operations:

Expand Down Expand Up @@ -128,7 +134,7 @@ def check_out(self, op):
if label:
self.labels[label] = conn
else:
self.addAsyncCleanup(conn.aclose_conn, None)
self.addAsyncCleanup(conn.close_conn, None)

def check_in(self, op):
"""Run the 'checkIn' operation."""
Expand Down Expand Up @@ -260,7 +266,6 @@ async def run_scenario(self, scenario_def, test):
client._topology.open()
else:
client._get_topology()
self.addAsyncCleanup(client.close)
self.pool = list(client._topology._servers.values())[0].pool

# Map of target names to Thread objects.
Expand Down Expand Up @@ -317,13 +322,11 @@ async def cleanup():
#
async def test_1_client_connection_pool_options(self):
client = await self.async_rs_or_single_client(**self.POOL_OPTIONS)
self.addAsyncCleanup(client.close)
pool_opts = (await async_get_pool(client)).opts
self.assertEqual(pool_opts.non_default_options, self.POOL_OPTIONS)

async def test_2_all_client_pools_have_same_options(self):
client = await self.async_rs_or_single_client(**self.POOL_OPTIONS)
self.addAsyncCleanup(client.close)
await client.admin.command("ping")
# Discover at least one secondary.
if await async_client_context.has_secondaries:
Expand All @@ -339,14 +342,12 @@ async def test_3_uri_connection_pool_options(self):
opts = "&".join([f"{k}={v}" for k, v in self.POOL_OPTIONS.items()])
uri = f"mongodb://{await async_client_context.pair}/?{opts}"
client = await self.async_rs_or_single_client(uri)
self.addAsyncCleanup(client.close)
pool_opts = (await async_get_pool(client)).opts
self.assertEqual(pool_opts.non_default_options, self.POOL_OPTIONS)

async def test_4_subscribe_to_events(self):
listener = CMAPListener()
client = await self.async_single_client(event_listeners=[listener])
self.addAsyncCleanup(client.close)
self.assertEqual(listener.event_count(PoolCreatedEvent), 1)

# Creates a new connection.
Expand All @@ -370,7 +371,6 @@ async def test_4_subscribe_to_events(self):
async def test_5_check_out_fails_connection_error(self):
listener = CMAPListener()
client = await self.async_single_client(event_listeners=[listener])
self.addAsyncCleanup(client.close)
pool = await async_get_pool(client)

def mock_connect(*args, **kwargs):
Expand Down Expand Up @@ -399,7 +399,6 @@ async def test_5_check_out_fails_auth_error(self):
client = await self.async_single_client_noauth(
username="notauser", password="fail", event_listeners=[listener]
)
self.addAsyncCleanup(client.close)

# Attempt to create a new connection.
with self.assertRaisesRegex(OperationFailure, "failed"):
Expand Down Expand Up @@ -471,8 +470,9 @@ async def tests(self, scenario_def):
return [scenario_def]


test_creator = CMAPSpecTestCreator(create_test, AsyncTestCMAP, AsyncTestCMAP.TEST_PATH)
test_creator.create_tests()
if _IS_SYNC:
test_creator = CMAPSpecTestCreator(create_test, AsyncTestCMAP, AsyncTestCMAP.TEST_PATH)
test_creator.create_tests()


if __name__ == "__main__":
Expand Down
20 changes: 10 additions & 10 deletions test/test_connection_monitoring.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
from __future__ import annotations

import os
import pathlib
import sys
import time

Expand Down Expand Up @@ -83,7 +84,12 @@

class TestCMAP(IntegrationTest):
# 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")
else:
TEST_PATH = os.path.join(
pathlib.Path(__file__).resolve().parent.parent, "connection_monitoring"
)

# Test operations:

Expand Down Expand Up @@ -260,7 +266,6 @@ def run_scenario(self, scenario_def, test):
client._topology.open()
else:
client._get_topology()
self.addCleanup(client.close)
self.pool = list(client._topology._servers.values())[0].pool

# Map of target names to Thread objects.
Expand Down Expand Up @@ -317,13 +322,11 @@ def cleanup():
#
def test_1_client_connection_pool_options(self):
client = self.rs_or_single_client(**self.POOL_OPTIONS)
self.addCleanup(client.close)
pool_opts = (get_pool(client)).opts
self.assertEqual(pool_opts.non_default_options, self.POOL_OPTIONS)

def test_2_all_client_pools_have_same_options(self):
client = self.rs_or_single_client(**self.POOL_OPTIONS)
self.addCleanup(client.close)
client.admin.command("ping")
# Discover at least one secondary.
if client_context.has_secondaries:
Expand All @@ -339,14 +342,12 @@ def test_3_uri_connection_pool_options(self):
opts = "&".join([f"{k}={v}" for k, v in self.POOL_OPTIONS.items()])
uri = f"mongodb://{client_context.pair}/?{opts}"
client = self.rs_or_single_client(uri)
self.addCleanup(client.close)
pool_opts = (get_pool(client)).opts
self.assertEqual(pool_opts.non_default_options, self.POOL_OPTIONS)

def test_4_subscribe_to_events(self):
listener = CMAPListener()
client = self.single_client(event_listeners=[listener])
self.addCleanup(client.close)
self.assertEqual(listener.event_count(PoolCreatedEvent), 1)

# Creates a new connection.
Expand All @@ -370,7 +371,6 @@ def test_4_subscribe_to_events(self):
def test_5_check_out_fails_connection_error(self):
listener = CMAPListener()
client = self.single_client(event_listeners=[listener])
self.addCleanup(client.close)
pool = get_pool(client)

def mock_connect(*args, **kwargs):
Expand Down Expand Up @@ -399,7 +399,6 @@ def test_5_check_out_fails_auth_error(self):
client = self.single_client_noauth(
username="notauser", password="fail", event_listeners=[listener]
)
self.addCleanup(client.close)

# Attempt to create a new connection.
with self.assertRaisesRegex(OperationFailure, "failed"):
Expand Down Expand Up @@ -471,8 +470,9 @@ def tests(self, scenario_def):
return [scenario_def]


test_creator = CMAPSpecTestCreator(create_test, TestCMAP, TestCMAP.TEST_PATH)
test_creator.create_tests()
if _IS_SYNC:
test_creator = CMAPSpecTestCreator(create_test, TestCMAP, TestCMAP.TEST_PATH)
test_creator.create_tests()


if __name__ == "__main__":
Expand Down
Loading