18
18
19
19
sys .path [0 :0 ] = ["" ]
20
20
21
+ from bson .objectid import ObjectId
21
22
from bson .py3compat import imap
23
+
22
24
from pymongo import common
23
25
from pymongo .read_preferences import ReadPreference , Secondary
24
26
from pymongo .server_type import SERVER_TYPE
@@ -294,7 +296,7 @@ def test_readable_writable(self):
294
296
'setName' : 'rs' ,
295
297
'hosts' : ['a' , 'b' ]})
296
298
297
- self .assertTrue (
299
+ self .assertEqual (
298
300
t .description .topology_type_name , 'ReplicaSetWithPrimary' )
299
301
self .assertTrue (t .description .has_writable_server ())
300
302
self .assertTrue (t .description .has_readable_server ())
@@ -319,7 +321,7 @@ def test_readable_writable(self):
319
321
'setName' : 'rs' ,
320
322
'hosts' : ['a' , 'b' ]})
321
323
322
- self .assertTrue (
324
+ self .assertEqual (
323
325
t .description .topology_type_name , 'ReplicaSetNoPrimary' )
324
326
self .assertFalse (t .description .has_writable_server ())
325
327
self .assertFalse (t .description .has_readable_server ())
@@ -344,7 +346,7 @@ def test_readable_writable(self):
344
346
'hosts' : ['a' , 'b' ],
345
347
'tags' : {'tag' : 'exists' }})
346
348
347
- self .assertTrue (
349
+ self .assertEqual (
348
350
t .description .topology_type_name , 'ReplicaSetWithPrimary' )
349
351
self .assertTrue (t .description .has_writable_server ())
350
352
self .assertTrue (t .description .has_readable_server ())
@@ -680,6 +682,26 @@ def test_topology_repr(self):
680
682
"<ServerDescription ('c', 27017) server_type: Unknown,"
681
683
" rtt: None>]>" % (t ._topology_id ,))
682
684
685
+ def test_unexpected_load_balancer (self ):
686
+ # Note: This behavior should not be reachable in practice but we
687
+ # should handle it gracefully nonetheless. See PYTHON-2791.
688
+ # Load balancers are included in topology with a single seed.
689
+ t = create_mock_topology (seeds = ['a' ])
690
+ mock_lb_response = {'ok' : 1 , 'msg' : 'isdbgrid' ,
691
+ 'serviceId' : ObjectId (), 'maxWireVersion' : 13 }
692
+ got_ismaster (t , ('a' , 27017 ), mock_lb_response )
693
+ sds = t .description .server_descriptions ()
694
+ self .assertIn (('a' , 27017 ), sds )
695
+ self .assertEqual (sds [('a' , 27017 )].server_type_name , 'LoadBalancer' )
696
+ self .assertEqual (t .description .topology_type_name , 'Single' )
697
+ self .assertTrue (t .description .has_writable_server ())
698
+
699
+ # Load balancers are removed from a topology with multiple seeds.
700
+ t = create_mock_topology (seeds = ['a' , 'b' ])
701
+ got_ismaster (t , ('a' , 27017 ), mock_lb_response )
702
+ self .assertNotIn (('a' , 27017 ), t .description .server_descriptions ())
703
+ self .assertEqual (t .description .topology_type_name , 'Unknown' )
704
+
683
705
684
706
def wait_for_master (topology ):
685
707
"""Wait for a Topology to discover a writable server.
0 commit comments