@@ -75,15 +75,16 @@ class MockMonitor(object):
7575 def __init__ (self , server_description , topology , pool , topology_settings ):
7676 self ._server_description = server_description
7777 self ._topology = topology
78+ self .opened = False
7879
7980 def open (self ):
80- pass
81+ self . opened = True
8182
8283 def request_check (self ):
8384 pass
8485
8586 def close (self ):
86- pass
87+ self . opened = False
8788
8889
8990class SetNameDiscoverySettings (TopologySettings ):
@@ -122,9 +123,16 @@ def disconnected(topology, server_address):
122123 topology .on_change (ServerDescription (server_address ))
123124
124125
126+ def get_server (topology , hostname ):
127+ return topology .get_server_by_address ((hostname , 27017 ))
128+
129+
125130def get_type (topology , hostname ):
126- description = topology .get_server_by_address ((hostname , 27017 )).description
127- return description .server_type
131+ return get_server (topology , hostname ).description .server_type
132+
133+
134+ def get_monitor (topology , hostname ):
135+ return get_server (topology , hostname )._monitor
128136
129137
130138class TopologyTest (unittest .TestCase ):
@@ -398,26 +406,37 @@ def test_close(self):
398406
399407 self .assertEqual (SERVER_TYPE .RSPrimary , get_type (t , 'a' ))
400408 self .assertEqual (SERVER_TYPE .RSSecondary , get_type (t , 'b' ))
409+ self .assertTrue (get_monitor (t , 'a' ).opened )
410+ self .assertTrue (get_monitor (t , 'b' ).opened )
401411 self .assertEqual (TOPOLOGY_TYPE .ReplicaSetWithPrimary ,
402412 t .description .topology_type )
403413
404414 t .close ()
405415 self .assertEqual (2 , len (t .description .server_descriptions ()))
406416 self .assertEqual (SERVER_TYPE .Unknown , get_type (t , 'a' ))
407417 self .assertEqual (SERVER_TYPE .Unknown , get_type (t , 'b' ))
418+ self .assertFalse (get_monitor (t , 'a' ).opened )
419+ self .assertFalse (get_monitor (t , 'b' ).opened )
408420 self .assertEqual ('rs' , t .description .replica_set_name )
409421 self .assertEqual (TOPOLOGY_TYPE .ReplicaSetNoPrimary ,
410422 t .description .topology_type )
411423
424+ # A closed topology should not be updated when receiving an isMaster.
412425 got_ismaster (t , ('a' , 27017 ), {
413426 'ok' : 1 ,
414427 'ismaster' : True ,
415428 'setName' : 'rs' ,
416- 'hosts' : ['a' , 'b' ]})
429+ 'hosts' : ['a' , 'b' , 'c' ]})
417430
418- self .assertEqual (SERVER_TYPE .RSPrimary , get_type (t , 'a' ))
431+ self .assertEqual (2 , len (t .description .server_descriptions ()))
432+ self .assertEqual (SERVER_TYPE .Unknown , get_type (t , 'a' ))
419433 self .assertEqual (SERVER_TYPE .Unknown , get_type (t , 'b' ))
420- self .assertEqual (TOPOLOGY_TYPE .ReplicaSetWithPrimary ,
434+ self .assertFalse (get_monitor (t , 'a' ).opened )
435+ self .assertFalse (get_monitor (t , 'b' ).opened )
436+ # Server c should not have been added.
437+ self .assertEqual (None , get_server (t , 'c' ))
438+ self .assertEqual ('rs' , t .description .replica_set_name )
439+ self .assertEqual (TOPOLOGY_TYPE .ReplicaSetNoPrimary ,
421440 t .description .topology_type )
422441
423442 def test_reset_server (self ):
@@ -480,7 +499,7 @@ def test_discover_set_name_from_primary(self):
480499 self .assertEqual (t .description .replica_set_name , None )
481500 self .assertEqual (t .description .topology_type ,
482501 TOPOLOGY_TYPE .ReplicaSetNoPrimary )
483-
502+ t . open ()
484503 got_ismaster (t , address , {
485504 'ok' : 1 ,
486505 'ismaster' : True ,
@@ -516,7 +535,7 @@ def test_discover_set_name_from_secondary(self):
516535 self .assertEqual (t .description .replica_set_name , None )
517536 self .assertEqual (t .description .topology_type ,
518537 TOPOLOGY_TYPE .ReplicaSetNoPrimary )
519-
538+ t . open ()
520539 got_ismaster (t , address , {
521540 'ok' : 1 ,
522541 'ismaster' : False ,
0 commit comments