1818
1919sys .path [0 :0 ] = ["" ]
2020
21+ from bson .objectid import ObjectId
2122from bson .py3compat import imap
23+
2224from pymongo import common
2325from pymongo .read_preferences import ReadPreference , Secondary
2426from pymongo .server_type import SERVER_TYPE
@@ -294,7 +296,7 @@ def test_readable_writable(self):
294296 'setName' : 'rs' ,
295297 'hosts' : ['a' , 'b' ]})
296298
297- self .assertTrue (
299+ self .assertEqual (
298300 t .description .topology_type_name , 'ReplicaSetWithPrimary' )
299301 self .assertTrue (t .description .has_writable_server ())
300302 self .assertTrue (t .description .has_readable_server ())
@@ -319,7 +321,7 @@ def test_readable_writable(self):
319321 'setName' : 'rs' ,
320322 'hosts' : ['a' , 'b' ]})
321323
322- self .assertTrue (
324+ self .assertEqual (
323325 t .description .topology_type_name , 'ReplicaSetNoPrimary' )
324326 self .assertFalse (t .description .has_writable_server ())
325327 self .assertFalse (t .description .has_readable_server ())
@@ -344,7 +346,7 @@ def test_readable_writable(self):
344346 'hosts' : ['a' , 'b' ],
345347 'tags' : {'tag' : 'exists' }})
346348
347- self .assertTrue (
349+ self .assertEqual (
348350 t .description .topology_type_name , 'ReplicaSetWithPrimary' )
349351 self .assertTrue (t .description .has_writable_server ())
350352 self .assertTrue (t .description .has_readable_server ())
@@ -680,6 +682,26 @@ def test_topology_repr(self):
680682 "<ServerDescription ('c', 27017) server_type: Unknown,"
681683 " rtt: None>]>" % (t ._topology_id ,))
682684
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+
683705
684706def wait_for_master (topology ):
685707 """Wait for a Topology to discover a writable server.
0 commit comments