Skip to content

Commit ee955f1

Browse files
committed
address review
1 parent 40794fe commit ee955f1

File tree

2 files changed

+33
-4
lines changed

2 files changed

+33
-4
lines changed

pymongo/topology_description.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -325,10 +325,17 @@ def apply_selector(
325325
return [description] if description else []
326326

327327
# Primary selection fast path.
328-
if self.topology_type == TOPOLOGY_TYPE.ReplicaSetWithPrimary and type(selector) is Primary:
328+
if (
329+
custom_selector is None
330+
and self.topology_type == TOPOLOGY_TYPE.ReplicaSetWithPrimary
331+
and type(selector) is Primary
332+
):
329333
for sd in self._server_descriptions.values():
330334
if sd.server_type == SERVER_TYPE.RSPrimary:
331-
return [sd]
335+
sds = [sd]
336+
if custom_selector:
337+
sds = custom_selector(sds)
338+
return sds
332339
# No primary found, return an empty list.
333340
return []
334341

test/test_topology.py

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
from pymongo import common
3131
from pymongo.errors import AutoReconnect, ConfigurationError, ConnectionFailure
3232
from pymongo.hello import Hello, HelloCompat
33-
from pymongo.read_preferences import ReadPreference, Secondary
33+
from pymongo.read_preferences import Primary, ReadPreference, Secondary
3434
from pymongo.server_description import ServerDescription
3535
from pymongo.server_selectors import any_server_selector, writable_server_selector
3636
from pymongo.server_type import SERVER_TYPE
@@ -51,7 +51,10 @@ def get_topology_type(self):
5151

5252

5353
def create_mock_topology(
54-
seeds=None, replica_set_name=None, monitor_class=DummyMonitor, direct_connection=False
54+
seeds=None,
55+
replica_set_name=None,
56+
monitor_class=DummyMonitor,
57+
direct_connection=False,
5558
):
5659
partitioned_seeds = list(map(common.partition_node, seeds or ["a"]))
5760
topology_settings = TopologySettings(
@@ -123,6 +126,25 @@ def test_timeout_configuration(self):
123126
# The monitor, not its pool, is responsible for calling hello.
124127
self.assertTrue(monitor._pool.is_sdam)
125128

129+
def test_selector_fast_path(self):
130+
topology = create_mock_topology(seeds=["a", "b:27018"], replica_set_name="foo")
131+
description = topology.description
132+
description._topology_type = TOPOLOGY_TYPE.ReplicaSetWithPrimary
133+
134+
# There is no primary yet, so it should give an empty list.
135+
self.assertEqual(description.apply_selector(Primary()), [])
136+
137+
# If we set a primary server, we should get it back.
138+
sd = list(description._server_descriptions.values())[0]
139+
sd._server_type = SERVER_TYPE.RSPrimary
140+
self.assertEqual(description.apply_selector(Primary()), [sd])
141+
142+
# If there is a custom selector, it should be applied.
143+
def custom_selector(servers):
144+
return []
145+
146+
self.assertEqual(description.apply_selector(Primary(), custom_selector=custom_selector), [])
147+
126148

127149
class TestSingleServerTopology(TopologyTest):
128150
def test_direct_connection(self):

0 commit comments

Comments
 (0)