3030from pymongo import common
3131from pymongo .errors import AutoReconnect , ConfigurationError , ConnectionFailure
3232from pymongo .hello import Hello , HelloCompat
33- from pymongo .read_preferences import ReadPreference , Secondary
33+ from pymongo .read_preferences import Primary , ReadPreference , Secondary
3434from pymongo .server_description import ServerDescription
3535from pymongo .server_selectors import any_server_selector , writable_server_selector
3636from pymongo .server_type import SERVER_TYPE
@@ -51,7 +51,10 @@ def get_topology_type(self):
5151
5252
5353def 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
127149class TestSingleServerTopology (TopologyTest ):
128150 def test_direct_connection (self ):
0 commit comments