Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions test/asynchronous/test_change_stream.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
from test.utils import (
AllowListEventListener,
EventListener,
OvertCommandListener,
async_wait_until,
)

Expand Down Expand Up @@ -179,7 +180,7 @@ async def _wait_until():

@no_type_check
async def test_try_next_runs_one_getmore(self):
listener = EventListener()
listener = OvertCommandListener()
client = await self.async_rs_or_single_client(event_listeners=[listener])
# Connect to the cluster.
await client.admin.command("ping")
Expand Down Expand Up @@ -237,7 +238,7 @@ async def _wait_until():

@no_type_check
async def test_batch_size_is_honored(self):
listener = EventListener()
listener = OvertCommandListener()
client = await self.async_rs_or_single_client(event_listeners=[listener])
# Connect to the cluster.
await client.admin.command("ping")
Expand Down
4 changes: 2 additions & 2 deletions test/asynchronous/test_collation.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
import functools
import warnings
from test.asynchronous import AsyncIntegrationTest, async_client_context, unittest
from test.utils import EventListener
from test.utils import EventListener, OvertCommandListener
from typing import Any

from pymongo.asynchronous.helpers import anext
Expand Down Expand Up @@ -101,7 +101,7 @@ class TestCollation(AsyncIntegrationTest):
@async_client_context.require_connection
async def _setup_class(cls):
await super()._setup_class()
cls.listener = EventListener()
cls.listener = OvertCommandListener()
cls.client = await cls.unmanaged_async_rs_or_single_client(event_listeners=[cls.listener])
cls.db = cls.client.pymongo_test
cls.collation = Collation("en_US")
Expand Down
3 changes: 2 additions & 1 deletion test/asynchronous/test_collection.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
from test.utils import (
IMPOSSIBLE_WRITE_CONCERN,
EventListener,
OvertCommandListener,
async_get_pool,
async_is_mongos,
async_wait_until,
Expand Down Expand Up @@ -2116,7 +2117,7 @@ async def test_find_one_and(self):
self.assertEqual(4, (await c.find_one_and_update({}, {"$inc": {"i": 1}}, sort=sort))["j"])

async def test_find_one_and_write_concern(self):
listener = EventListener()
listener = OvertCommandListener()
db = (await self.async_single_client(event_listeners=[listener]))[self.db.name]
# non-default WriteConcern.
c_w0 = db.get_collection("test", write_concern=WriteConcern(w=0))
Expand Down
4 changes: 2 additions & 2 deletions test/asynchronous/test_cursor.py
Original file line number Diff line number Diff line change
Expand Up @@ -1601,7 +1601,7 @@ async def test_read_concern(self):
await anext(c.find_raw_batches())

async def test_monitoring(self):
listener = EventListener()
listener = OvertCommandListener()
client = await self.async_rs_or_single_client(event_listeners=[listener])
c = client.pymongo_test.test
await c.drop()
Expand Down Expand Up @@ -1768,7 +1768,7 @@ async def test_collation(self):
await anext(await self.db.test.aggregate_raw_batches([], collation=Collation("en_US")))

async def test_monitoring(self):
listener = EventListener()
listener = OvertCommandListener()
client = await self.async_rs_or_single_client(event_listeners=[listener])
c = client.pymongo_test.test
await c.drop()
Expand Down
4 changes: 2 additions & 2 deletions test/asynchronous/test_grid_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@

sys.path[0:0] = [""]

from test.utils import EventListener
from test.utils import OvertCommandListener

from bson.objectid import ObjectId
from gridfs.asynchronous.grid_file import (
Expand Down Expand Up @@ -810,7 +810,7 @@ async def test_survive_cursor_not_found(self):
# Use 102 batches to cause a single getMore.
chunk_size = 1024
data = b"d" * (102 * chunk_size)
listener = EventListener()
listener = OvertCommandListener()
client = await self.async_rs_or_single_client(event_listeners=[listener])
db = client.pymongo_test
async with AsyncGridIn(db.fs, chunk_size=chunk_size) as infile:
Expand Down
21 changes: 12 additions & 9 deletions test/asynchronous/test_monitoring.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
)
from test.utils import (
EventListener,
OvertCommandListener,
async_wait_until,
)

Expand All @@ -54,7 +55,7 @@ class AsyncTestCommandMonitoring(AsyncIntegrationTest):
@async_client_context.require_connection
async def _setup_class(cls):
await super()._setup_class()
cls.listener = EventListener()
cls.listener = OvertCommandListener()
cls.client = await cls.unmanaged_async_rs_or_single_client(
event_listeners=[cls.listener], retryWrites=False
)
Expand Down Expand Up @@ -1100,11 +1101,13 @@ async def test_first_batch_helper(self):

@async_client_context.require_version_max(6, 1, 99)
async def test_sensitive_commands(self):
listeners = self.client._event_listeners
listener = EventListener()
client = await self.async_rs_or_single_client(event_listeners=[listener])
listeners = client._event_listeners

self.listener.reset()
listener.reset()
cmd = SON([("getnonce", 1)])
listeners.publish_command_start(cmd, "pymongo_test", 12345, await self.client.address, None) # type: ignore[arg-type]
listeners.publish_command_start(cmd, "pymongo_test", 12345, await client.address, None) # type: ignore[arg-type]
delta = datetime.timedelta(milliseconds=100)
listeners.publish_command_success(
delta,
Expand All @@ -1115,15 +1118,15 @@ async def test_sensitive_commands(self):
None,
database_name="pymongo_test",
)
started = self.listener.started_events[0]
succeeded = self.listener.succeeded_events[0]
self.assertEqual(0, len(self.listener.failed_events))
started = listener.started_events[0]
succeeded = listener.succeeded_events[0]
self.assertEqual(0, len(listener.failed_events))
self.assertIsInstance(started, monitoring.CommandStartedEvent)
self.assertEqual({}, started.command)
self.assertEqual("pymongo_test", started.database_name)
self.assertEqual("getnonce", started.command_name)
self.assertIsInstance(started.request_id, int)
self.assertEqual(await self.client.address, started.connection_id)
self.assertEqual(await client.address, started.connection_id)
self.assertIsInstance(succeeded, monitoring.CommandSucceededEvent)
self.assertEqual(succeeded.duration_micros, 100000)
self.assertEqual(started.command_name, succeeded.command_name)
Expand All @@ -1140,7 +1143,7 @@ class AsyncTestGlobalListener(AsyncIntegrationTest):
@async_client_context.require_connection
async def _setup_class(cls):
await super()._setup_class()
cls.listener = EventListener()
cls.listener = OvertCommandListener()
# We plan to call register(), which internally modifies _LISTENERS.
cls.saved_listeners = copy.deepcopy(monitoring._LISTENERS)
monitoring.register(cls.listener)
Expand Down
3 changes: 2 additions & 1 deletion test/asynchronous/test_session.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
from test.utils import (
EventListener,
ExceptionCatchingThread,
OvertCommandListener,
async_wait_until,
wait_until,
)
Expand Down Expand Up @@ -199,7 +200,7 @@ def test_implicit_sessions_checkout(self):
lsid_set = set()
failures = 0
for _ in range(5):
listener = EventListener()
listener = OvertCommandListener()
client = self.async_rs_or_single_client(event_listeners=[listener], maxPoolSize=1)
cursor = client.db.test.find({})
ops: List[Tuple[Callable, List[Any]]] = [
Expand Down
6 changes: 3 additions & 3 deletions test/auth_oidc/test_auth_oidc.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
sys.path[0:0] = [""]

from test.unified_format import generate_test_classes
from test.utils import EventListener
from test.utils import EventListener, OvertCommandListener

from bson import SON
from pymongo import MongoClient
Expand Down Expand Up @@ -348,7 +348,7 @@ def test_4_1_reauthenticate_succeeds(self):
# Create a default OIDC client and add an event listener.
# The following assumes that the driver does not emit saslStart or saslContinue events.
# If the driver does emit those events, ignore/filter them for the purposes of this test.
listener = EventListener()
listener = OvertCommandListener()
client = self.create_client(event_listeners=[listener])

# Perform a find operation that succeeds.
Expand Down Expand Up @@ -1021,7 +1021,7 @@ def fetch(self, _):

def test_4_4_speculative_authentication_should_be_ignored_on_reauthentication(self):
# Create an OIDC configured client that can listen for `SaslStart` commands.
listener = EventListener()
listener = OvertCommandListener()
client = self.create_client(event_listeners=[listener])

# Preload the *Client Cache* with a valid access token to enforce Speculative Authentication.
Expand Down
5 changes: 3 additions & 2 deletions test/test_change_stream.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
from test.utils import (
AllowListEventListener,
EventListener,
OvertCommandListener,
wait_until,
)

Expand Down Expand Up @@ -177,7 +178,7 @@ def _wait_until():

@no_type_check
def test_try_next_runs_one_getmore(self):
listener = EventListener()
listener = OvertCommandListener()
client = self.rs_or_single_client(event_listeners=[listener])
# Connect to the cluster.
client.admin.command("ping")
Expand Down Expand Up @@ -235,7 +236,7 @@ def _wait_until():

@no_type_check
def test_batch_size_is_honored(self):
listener = EventListener()
listener = OvertCommandListener()
client = self.rs_or_single_client(event_listeners=[listener])
# Connect to the cluster.
client.admin.command("ping")
Expand Down
4 changes: 2 additions & 2 deletions test/test_collation.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
import functools
import warnings
from test import IntegrationTest, client_context, unittest
from test.utils import EventListener
from test.utils import EventListener, OvertCommandListener
from typing import Any

from pymongo.collation import (
Expand Down Expand Up @@ -101,7 +101,7 @@ class TestCollation(IntegrationTest):
@client_context.require_connection
def _setup_class(cls):
super()._setup_class()
cls.listener = EventListener()
cls.listener = OvertCommandListener()
cls.client = cls.unmanaged_rs_or_single_client(event_listeners=[cls.listener])
cls.db = cls.client.pymongo_test
cls.collation = Collation("en_US")
Expand Down
3 changes: 2 additions & 1 deletion test/test_collection.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
from test.utils import (
IMPOSSIBLE_WRITE_CONCERN,
EventListener,
OvertCommandListener,
get_pool,
is_mongos,
wait_until,
Expand Down Expand Up @@ -2093,7 +2094,7 @@ def test_find_one_and(self):
self.assertEqual(4, (c.find_one_and_update({}, {"$inc": {"i": 1}}, sort=sort))["j"])

def test_find_one_and_write_concern(self):
listener = EventListener()
listener = OvertCommandListener()
db = (self.single_client(event_listeners=[listener]))[self.db.name]
# non-default WriteConcern.
c_w0 = db.get_collection("test", write_concern=WriteConcern(w=0))
Expand Down
4 changes: 2 additions & 2 deletions test/test_cursor.py
Original file line number Diff line number Diff line change
Expand Up @@ -1590,7 +1590,7 @@ def test_read_concern(self):
next(c.find_raw_batches())

def test_monitoring(self):
listener = EventListener()
listener = OvertCommandListener()
client = self.rs_or_single_client(event_listeners=[listener])
c = client.pymongo_test.test
c.drop()
Expand Down Expand Up @@ -1757,7 +1757,7 @@ def test_collation(self):
next(self.db.test.aggregate_raw_batches([], collation=Collation("en_US")))

def test_monitoring(self):
listener = EventListener()
listener = OvertCommandListener()
client = self.rs_or_single_client(event_listeners=[listener])
c = client.pymongo_test.test
c.drop()
Expand Down
4 changes: 2 additions & 2 deletions test/test_grid_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@

sys.path[0:0] = [""]

from test.utils import EventListener
from test.utils import OvertCommandListener

from bson.objectid import ObjectId
from gridfs.errors import NoFile
Expand Down Expand Up @@ -808,7 +808,7 @@ def test_survive_cursor_not_found(self):
# Use 102 batches to cause a single getMore.
chunk_size = 1024
data = b"d" * (102 * chunk_size)
listener = EventListener()
listener = OvertCommandListener()
client = self.rs_or_single_client(event_listeners=[listener])
db = client.pymongo_test
with GridIn(db.fs, chunk_size=chunk_size) as infile:
Expand Down
4 changes: 2 additions & 2 deletions test/test_index_management.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@

from test import IntegrationTest, PyMongoTestCase, unittest
from test.unified_format import generate_test_classes
from test.utils import AllowListEventListener, EventListener
from test.utils import AllowListEventListener, EventListener, OvertCommandListener

from pymongo.errors import OperationFailure
from pymongo.operations import SearchIndexModel
Expand Down Expand Up @@ -88,7 +88,7 @@ def setUpClass(cls) -> None:
url = os.environ.get("MONGODB_URI")
username = os.environ["DB_USER"]
password = os.environ["DB_PASSWORD"]
cls.listener = listener = EventListener()
cls.listener = listener = OvertCommandListener()
cls.client = cls.unmanaged_simple_client(
url, username=username, password=password, event_listeners=[listener]
)
Expand Down
21 changes: 12 additions & 9 deletions test/test_monitoring.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
)
from test.utils import (
EventListener,
OvertCommandListener,
wait_until,
)

Expand All @@ -54,7 +55,7 @@ class TestCommandMonitoring(IntegrationTest):
@client_context.require_connection
def _setup_class(cls):
super()._setup_class()
cls.listener = EventListener()
cls.listener = OvertCommandListener()
cls.client = cls.unmanaged_rs_or_single_client(
event_listeners=[cls.listener], retryWrites=False
)
Expand Down Expand Up @@ -1100,11 +1101,13 @@ def test_first_batch_helper(self):

@client_context.require_version_max(6, 1, 99)
def test_sensitive_commands(self):
listeners = self.client._event_listeners
listener = EventListener()
client = self.rs_or_single_client(event_listeners=[listener])
listeners = client._event_listeners

self.listener.reset()
listener.reset()
cmd = SON([("getnonce", 1)])
listeners.publish_command_start(cmd, "pymongo_test", 12345, self.client.address, None) # type: ignore[arg-type]
listeners.publish_command_start(cmd, "pymongo_test", 12345, client.address, None) # type: ignore[arg-type]
delta = datetime.timedelta(milliseconds=100)
listeners.publish_command_success(
delta,
Expand All @@ -1115,15 +1118,15 @@ def test_sensitive_commands(self):
None,
database_name="pymongo_test",
)
started = self.listener.started_events[0]
succeeded = self.listener.succeeded_events[0]
self.assertEqual(0, len(self.listener.failed_events))
started = listener.started_events[0]
succeeded = listener.succeeded_events[0]
self.assertEqual(0, len(listener.failed_events))
self.assertIsInstance(started, monitoring.CommandStartedEvent)
self.assertEqual({}, started.command)
self.assertEqual("pymongo_test", started.database_name)
self.assertEqual("getnonce", started.command_name)
self.assertIsInstance(started.request_id, int)
self.assertEqual(self.client.address, started.connection_id)
self.assertEqual(client.address, started.connection_id)
self.assertIsInstance(succeeded, monitoring.CommandSucceededEvent)
self.assertEqual(succeeded.duration_micros, 100000)
self.assertEqual(started.command_name, succeeded.command_name)
Expand All @@ -1140,7 +1143,7 @@ class TestGlobalListener(IntegrationTest):
@client_context.require_connection
def _setup_class(cls):
super()._setup_class()
cls.listener = EventListener()
cls.listener = OvertCommandListener()
# We plan to call register(), which internally modifies _LISTENERS.
cls.saved_listeners = copy.deepcopy(monitoring._LISTENERS)
monitoring.register(cls.listener)
Expand Down
Loading
Loading