Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
4 changes: 2 additions & 2 deletions .evergreen/resync-specs.sh
Original file line number Diff line number Diff line change
Expand Up @@ -132,8 +132,8 @@ do
discovery_and_monitoring/sharded
cpjson server-discovery-and-monitoring/tests/single \
discovery_and_monitoring/single
cpjson server-discovery-and-monitoring/tests/integration \
discovery_and_monitoring_integration
cpjson server-discovery-and-monitoring/tests/unified \
discovery_and_monitoring/unified
cpjson server-discovery-and-monitoring/tests/load-balanced \
discovery_and_monitoring/load-balanced
;;
Expand Down
64 changes: 28 additions & 36 deletions pymongo/monitoring.py
Original file line number Diff line number Diff line change
Expand Up @@ -914,13 +914,12 @@ class ConnectionCheckOutFailedReason(object):


class _ConnectionEvent(object):
Copy link
Member Author

@ShaneHarvey ShaneHarvey Aug 1, 2022

Choose a reason for hiding this comment

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

I refactored the private base class hierarchy here to make _ConnectionEvent the base class for ALL connection events. This makes the CMAP event filtering events easier since we can do isinstance(e, (_ConnectionEvent, _PoolEvent))

"""Private base class for some connection events."""
"""Private base class for connection events."""

__slots__ = ("__address", "__connection_id")
__slots__ = ("__address",)

def __init__(self, address: _Address, connection_id: int) -> None:
def __init__(self, address: _Address) -> None:
self.__address = address
self.__connection_id = connection_id

@property
def address(self) -> _Address:
Expand All @@ -929,16 +928,29 @@ def address(self) -> _Address:
"""
return self.__address

def __repr__(self):
return "%s(%r)" % (self.__class__.__name__, self.__address)


class _ConnectionIdEvent(_ConnectionEvent):
"""Private base class for connection events with an id."""

__slots__ = ("__connection_id",)

def __init__(self, address: _Address, connection_id: int) -> None:
super().__init__(address)
self.__connection_id = connection_id

@property
def connection_id(self) -> int:
"""The ID of the Connection."""
return self.__connection_id

def __repr__(self):
return "%s(%r, %r)" % (self.__class__.__name__, self.__address, self.__connection_id)
return "%s(%r, %r)" % (self.__class__.__name__, self.address, self.__connection_id)


class ConnectionCreatedEvent(_ConnectionEvent):
class ConnectionCreatedEvent(_ConnectionIdEvent):
"""Published when a Connection Pool creates a Connection object.

NOTE: This connection is not ready for use until the
Expand All @@ -955,7 +967,7 @@ class ConnectionCreatedEvent(_ConnectionEvent):
__slots__ = ()


class ConnectionReadyEvent(_ConnectionEvent):
class ConnectionReadyEvent(_ConnectionIdEvent):
"""Published when a Connection has finished its setup, and is ready to use.

:Parameters:
Expand All @@ -969,7 +981,7 @@ class ConnectionReadyEvent(_ConnectionEvent):
__slots__ = ()


class ConnectionClosedEvent(_ConnectionEvent):
class ConnectionClosedEvent(_ConnectionIdEvent):
"""Published when a Connection is closed.

:Parameters:
Expand Down Expand Up @@ -1005,7 +1017,7 @@ def __repr__(self):
)


class ConnectionCheckOutStartedEvent(object):
class ConnectionCheckOutStartedEvent(_ConnectionEvent):
"""Published when the driver starts attempting to check out a connection.

:Parameters:
Expand All @@ -1015,23 +1027,10 @@ class ConnectionCheckOutStartedEvent(object):
.. versionadded:: 3.9
"""

__slots__ = ("__address",)

def __init__(self, address):
self.__address = address

@property
def address(self):
"""The address (host, port) pair of the server this connection is
attempting to connect to.
"""
return self.__address

def __repr__(self):
return "%s(%r)" % (self.__class__.__name__, self.__address)
__slots__ = ()


class ConnectionCheckOutFailedEvent(object):
class ConnectionCheckOutFailedEvent(_ConnectionEvent):
"""Published when the driver's attempt to check out a connection fails.

:Parameters:
Expand All @@ -1042,19 +1041,12 @@ class ConnectionCheckOutFailedEvent(object):
.. versionadded:: 3.9
"""

__slots__ = ("__address", "__reason")
__slots__ = ("__reason",)

def __init__(self, address: _Address, reason: str) -> None:
self.__address = address
super().__init__(address)
self.__reason = reason

@property
def address(self) -> _Address:
"""The address (host, port) pair of the server this connection is
attempting to connect to.
"""
return self.__address

@property
def reason(self) -> str:
"""A reason explaining why connection check out failed.
Expand All @@ -1065,10 +1057,10 @@ def reason(self) -> str:
return self.__reason

def __repr__(self):
return "%s(%r, %r)" % (self.__class__.__name__, self.__address, self.__reason)
return "%s(%r, %r)" % (self.__class__.__name__, self.address, self.__reason)


class ConnectionCheckedOutEvent(_ConnectionEvent):
class ConnectionCheckedOutEvent(_ConnectionIdEvent):
"""Published when the driver successfully checks out a Connection.

:Parameters:
Expand All @@ -1082,7 +1074,7 @@ class ConnectionCheckedOutEvent(_ConnectionEvent):
__slots__ = ()


class ConnectionCheckedInEvent(_ConnectionEvent):
class ConnectionCheckedInEvent(_ConnectionIdEvent):
"""Published when the driver checks in a Connection into the Pool.

:Parameters:
Expand Down
230 changes: 230 additions & 0 deletions test/discovery_and_monitoring/unified/auth-error.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,230 @@
{
"description": "auth-error",
"schemaVersion": "1.10",
"runOnRequirements": [
{
"minServerVersion": "4.4",
"auth": true,
"serverless": "forbid",
"topologies": [
"single",
"replicaset",
"sharded"
]
}
],
"createEntities": [
{
"client": {
"id": "setupClient",
"useMultipleMongoses": false
}
}
],
"initialData": [
{
"collectionName": "auth-error",
"databaseName": "sdam-tests",
"documents": [
{
"_id": 1
},
{
"_id": 2
}
]
}
],
"tests": [
{
"description": "Reset server and pool after AuthenticationFailure error",
"operations": [
{
"name": "failPoint",
"object": "testRunner",
"arguments": {
"client": "setupClient",
"failPoint": {
"configureFailPoint": "failCommand",
"mode": {
"times": 1
},
"data": {
"failCommands": [
"saslContinue"
],
"appName": "authErrorTest",
"errorCode": 18
}
}
}
},
{
"name": "createEntities",
"object": "testRunner",
"arguments": {
"entities": [
{
"client": {
"id": "client",
"useMultipleMongoses": false,
"observeEvents": [
"commandStartedEvent",
"serverDescriptionChangedEvent",
"poolClearedEvent"
],
"uriOptions": {
"retryWrites": false,
"appname": "authErrorTest"
}
}
},
{
"database": {
"id": "database",
"client": "client",
"databaseName": "sdam-tests"
}
},
{
"collection": {
"id": "collection",
"database": "database",
"collectionName": "auth-error"
}
}
]
}
},
{
"name": "insertMany",
"object": "collection",
"arguments": {
"documents": [
{
"_id": 3
},
{
"_id": 4
}
]
},
"expectError": {
"isError": true
}
},
{
"name": "waitForEvent",
"object": "testRunner",
"arguments": {
"client": "client",
"event": {
"serverDescriptionChangedEvent": {
"newDescription": {
"type": "Unknown"
}
}
},
"count": 1
}
},
{
"name": "waitForEvent",
"object": "testRunner",
"arguments": {
"client": "client",
"event": {
"poolClearedEvent": {}
},
"count": 1
}
},
{
"name": "insertMany",
"object": "collection",
"arguments": {
"documents": [
{
"_id": 5
},
{
"_id": 6
}
]
}
},
{
"name": "assertEventCount",
"object": "testRunner",
"arguments": {
"client": "client",
"event": {
"serverDescriptionChangedEvent": {
"newDescription": {
"type": "Unknown"
}
}
},
"count": 1
}
},
{
"name": "assertEventCount",
"object": "testRunner",
"arguments": {
"client": "client",
"event": {
"poolClearedEvent": {}
},
"count": 1
}
}
],
"expectEvents": [
{
"client": "client",
"eventType": "command",
"events": [
{
"commandStartedEvent": {
"command": {
"insert": "auth-error",
"documents": [
{
"_id": 5
},
{
"_id": 6
}
]
},
"commandName": "insert",
"databaseName": "sdam-tests"
}
}
]
}
],
"outcome": [
{
"collectionName": "auth-error",
"databaseName": "sdam-tests",
"documents": [
{
"_id": 1
},
{
"_id": 2
},
{
"_id": 5
},
{
"_id": 6
}
]
}
]
}
]
}
Loading