Skip to content

Commit c185c95

Browse files
committed
Address review
1 parent aa86a3a commit c185c95

File tree

6 files changed

+36
-30
lines changed

6 files changed

+36
-30
lines changed

pymongo/asynchronous/mongo_client.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -768,6 +768,7 @@ def __init__(
768768
self._timeout: float | None = None
769769
self._topology_settings: TopologySettings = None # type: ignore[assignment]
770770
self._event_listeners: _EventListeners | None = None
771+
self._initial_topology_id: Optional[ObjectId] = None # type: ignore[assignment]
771772

772773
# _pool_class, _monitor_class, and _condition_class are for deep
773774
# customization of PyMongo, e.g. Motor.
@@ -976,6 +977,7 @@ def _init_based_on_options(
976977
srv_service_name=srv_service_name,
977978
srv_max_hosts=srv_max_hosts,
978979
server_monitoring_mode=self._options.server_monitoring_mode,
980+
topology_id=self._initial_topology_id,
979981
)
980982
if self._options.auto_encryption_opts:
981983
from pymongo.asynchronous.encryption import _Encrypter
@@ -1210,14 +1212,15 @@ def topology_description(self) -> TopologyDescription:
12101212
servers = {
12111213
(host, self._port): ServerDescription((host, self._port)) for host in self._seeds
12121214
}
1213-
return TopologyDescription(
1215+
td = TopologyDescription(
12141216
TOPOLOGY_TYPE.Unknown,
12151217
servers,
12161218
None,
12171219
None,
12181220
None,
12191221
TopologySettings(),
12201222
)
1223+
self._initial_topology_id = td._topology_settings._topology_id
12211224
return self._topology.description
12221225

12231226
@property

pymongo/asynchronous/settings.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ def __init__(
5151
srv_service_name: str = common.SRV_SERVICE_NAME,
5252
srv_max_hosts: int = 0,
5353
server_monitoring_mode: str = common.SERVER_MONITORING_MODE,
54+
topology_id: Optional[ObjectId] = None,
5455
):
5556
"""Represent MongoClient's configuration.
5657
@@ -78,8 +79,10 @@ def __init__(
7879
self._srv_service_name = srv_service_name
7980
self._srv_max_hosts = srv_max_hosts or 0
8081
self._server_monitoring_mode = server_monitoring_mode
81-
82-
self._topology_id = ObjectId()
82+
if topology_id is not None:
83+
self._topology_id = topology_id
84+
else:
85+
self._topology_id = ObjectId()
8386
# Store the allocation traceback to catch unclosed clients in the
8487
# test suite.
8588
self._stack = "".join(traceback.format_stack()[:-2])

pymongo/synchronous/mongo_client.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -766,6 +766,7 @@ def __init__(
766766
self._timeout: float | None = None
767767
self._topology_settings: TopologySettings = None # type: ignore[assignment]
768768
self._event_listeners: _EventListeners | None = None
769+
self._initial_topology_id: Optional[ObjectId] = None # type: ignore[assignment]
769770

770771
# _pool_class, _monitor_class, and _condition_class are for deep
771772
# customization of PyMongo, e.g. Motor.
@@ -974,6 +975,7 @@ def _init_based_on_options(
974975
srv_service_name=srv_service_name,
975976
srv_max_hosts=srv_max_hosts,
976977
server_monitoring_mode=self._options.server_monitoring_mode,
978+
topology_id=self._initial_topology_id,
977979
)
978980
if self._options.auto_encryption_opts:
979981
from pymongo.synchronous.encryption import _Encrypter
@@ -1208,14 +1210,15 @@ def topology_description(self) -> TopologyDescription:
12081210
servers = {
12091211
(host, self._port): ServerDescription((host, self._port)) for host in self._seeds
12101212
}
1211-
return TopologyDescription(
1213+
td = TopologyDescription(
12121214
TOPOLOGY_TYPE.Unknown,
12131215
servers,
12141216
None,
12151217
None,
12161218
None,
12171219
TopologySettings(),
12181220
)
1221+
self._initial_topology_id = td._topology_settings._topology_id
12191222
return self._topology.description
12201223

12211224
@property

pymongo/synchronous/settings.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ def __init__(
5151
srv_service_name: str = common.SRV_SERVICE_NAME,
5252
srv_max_hosts: int = 0,
5353
server_monitoring_mode: str = common.SERVER_MONITORING_MODE,
54+
topology_id: Optional[ObjectId] = None,
5455
):
5556
"""Represent MongoClient's configuration.
5657
@@ -78,8 +79,10 @@ def __init__(
7879
self._srv_service_name = srv_service_name
7980
self._srv_max_hosts = srv_max_hosts or 0
8081
self._server_monitoring_mode = server_monitoring_mode
81-
82-
self._topology_id = ObjectId()
82+
if topology_id is not None:
83+
self._topology_id = topology_id
84+
else:
85+
self._topology_id = ObjectId()
8386
# Store the allocation traceback to catch unclosed clients in the
8487
# test suite.
8588
self._stack = "".join(traceback.format_stack()[:-2])

test/asynchronous/test_client.py

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -825,20 +825,29 @@ async def test_init_disconnected(self):
825825
topology_description.server_descriptions(),
826826
{(host, port): ServerDescription((host, port))},
827827
)
828+
828829
# address causes client to block until connected
829830
self.assertIsNotNone(await c.address)
831+
# Initial seed topology and connected topology have the same ID
832+
self.assertEqual(
833+
c._topology._topology_id, topology_description._topology_settings._topology_id
834+
)
835+
830836
c = await self.async_rs_or_single_client(connect=False)
831837
# primary causes client to block until connected
832838
await c.primary
833839
self.assertIsNotNone(c._topology)
840+
834841
c = await self.async_rs_or_single_client(connect=False)
835842
# secondaries causes client to block until connected
836843
await c.secondaries
837844
self.assertIsNotNone(c._topology)
845+
838846
c = await self.async_rs_or_single_client(connect=False)
839847
# arbiters causes client to block until connected
840848
await c.arbiters
841849
self.assertIsNotNone(c._topology)
850+
842851
c = await self.async_rs_or_single_client(connect=False)
843852
# is_primary causes client to block until connected
844853
self.assertIsInstance(await c.is_primary, bool)
@@ -2194,18 +2203,6 @@ async def test_uuid_queries(self):
21942203
self.assertEqual(2, len(docs))
21952204
await coll.drop()
21962205

2197-
async def test_unconnected_client_properties_with_srv(self):
2198-
client = self.simple_client("mongodb+srv://test1.test.build.10gen.cc/", connect=False)
2199-
self.assertEqual(client.nodes, frozenset())
2200-
topology_description = client.topology_description
2201-
self.assertEqual(topology_description.topology_type, TOPOLOGY_TYPE.Unknown)
2202-
self.assertEqual(
2203-
topology_description.server_descriptions(),
2204-
{("unknown", None): ServerDescription(("unknown", None))},
2205-
)
2206-
await client.aconnect()
2207-
self.assertEqual(await client.address, None)
2208-
22092206

22102207
class TestExhaustCursor(AsyncIntegrationTest):
22112208
"""Test that clients properly handle errors from exhaust cursors."""

test/test_client.py

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -800,20 +800,29 @@ def test_init_disconnected(self):
800800
topology_description.server_descriptions(),
801801
{(host, port): ServerDescription((host, port))},
802802
)
803+
803804
# address causes client to block until connected
804805
self.assertIsNotNone(c.address)
806+
# Initial seed topology and connected topology have the same ID
807+
self.assertEqual(
808+
c._topology._topology_id, topology_description._topology_settings._topology_id
809+
)
810+
805811
c = self.rs_or_single_client(connect=False)
806812
# primary causes client to block until connected
807813
c.primary
808814
self.assertIsNotNone(c._topology)
815+
809816
c = self.rs_or_single_client(connect=False)
810817
# secondaries causes client to block until connected
811818
c.secondaries
812819
self.assertIsNotNone(c._topology)
820+
813821
c = self.rs_or_single_client(connect=False)
814822
# arbiters causes client to block until connected
815823
c.arbiters
816824
self.assertIsNotNone(c._topology)
825+
817826
c = self.rs_or_single_client(connect=False)
818827
# is_primary causes client to block until connected
819828
self.assertIsInstance(c.is_primary, bool)
@@ -2151,18 +2160,6 @@ def test_uuid_queries(self):
21512160
self.assertEqual(2, len(docs))
21522161
coll.drop()
21532162

2154-
def test_unconnected_client_properties_with_srv(self):
2155-
client = self.simple_client("mongodb+srv://test1.test.build.10gen.cc/", connect=False)
2156-
self.assertEqual(client.nodes, frozenset())
2157-
topology_description = client.topology_description
2158-
self.assertEqual(topology_description.topology_type, TOPOLOGY_TYPE.Unknown)
2159-
self.assertEqual(
2160-
topology_description.server_descriptions(),
2161-
{("unknown", None): ServerDescription(("unknown", None))},
2162-
)
2163-
client._connect()
2164-
self.assertEqual(client.address, None)
2165-
21662163

21672164
class TestExhaustCursor(IntegrationTest):
21682165
"""Test that clients properly handle errors from exhaust cursors."""

0 commit comments

Comments
 (0)