30
30
from pymongo import common
31
31
from pymongo .errors import AutoReconnect , ConfigurationError , ConnectionFailure
32
32
from pymongo .hello import Hello , HelloCompat
33
- from pymongo .read_preferences import ReadPreference , Secondary
33
+ from pymongo .read_preferences import Primary , ReadPreference , Secondary
34
34
from pymongo .server_description import ServerDescription
35
35
from pymongo .server_selectors import any_server_selector , writable_server_selector
36
36
from pymongo .server_type import SERVER_TYPE
@@ -51,7 +51,10 @@ def get_topology_type(self):
51
51
52
52
53
53
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 ,
55
58
):
56
59
partitioned_seeds = list (map (common .partition_node , seeds or ["a" ]))
57
60
topology_settings = TopologySettings (
@@ -123,6 +126,25 @@ def test_timeout_configuration(self):
123
126
# The monitor, not its pool, is responsible for calling hello.
124
127
self .assertTrue (monitor ._pool .is_sdam )
125
128
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
+
126
148
127
149
class TestSingleServerTopology (TopologyTest ):
128
150
def test_direct_connection (self ):
0 commit comments