3636from pymongo .server_selectors import (any_server_selector ,
3737 writable_server_selector )
3838from pymongo .settings import TopologySettings
39- from test import client_knobs , unittest
39+ from test import client_knobs , SkipTest , unittest
4040from test .utils import wait_until
4141
4242
@@ -93,19 +93,16 @@ def get_topology_type(self):
9393def create_mock_topology (
9494 seeds = None ,
9595 replica_set_name = None ,
96- monitor_class = MockMonitor ,
97- condition_class = None ,
98- timer = None ):
96+ monitor_class = MockMonitor ):
9997 partitioned_seeds = list (imap (common .partition_node , seeds or ['a' ]))
10098 topology_settings = TopologySettings (
10199 partitioned_seeds ,
102100 replica_set_name = replica_set_name ,
103101 pool_class = MockPool ,
104- monitor_class = monitor_class ,
105- condition_class = condition_class ,
106- timer = timer )
102+ monitor_class = monitor_class )
107103
108104 t = Topology (topology_settings )
105+ t .open ()
109106 return t
110107
111108
@@ -544,7 +541,6 @@ def _check_with_socket(self, sock_info):
544541 raise socket .error ()
545542
546543 t = create_mock_topology (monitor_class = TestMonitor )
547- t .open ()
548544 server = wait_for_master (t )
549545 self .assertEqual (1 , ismaster_count [0 ])
550546 pool_id = server .pool .pool_id
@@ -566,7 +562,6 @@ def _check_with_socket(self, sock_info):
566562 raise socket .error ()
567563
568564 t = create_mock_topology (monitor_class = TestMonitor )
569- t .open ()
570565 server = wait_for_master (t )
571566 self .assertEqual (1 , ismaster_count [0 ])
572567 self .assertEqual (SERVER_TYPE .Standalone ,
@@ -578,50 +573,26 @@ def _check_with_socket(self, sock_info):
578573 self .assertEqual (SERVER_TYPE .Standalone , get_type (t , 'a' ))
579574
580575 def test_selection_failure (self ):
581- # While ismaster fails, ensure it's called about every 10 ms.
582- timeouts = []
583-
584- class TestCondition (object ):
585- def __init__ (self , lock = None ):
586- self .condition = threading .Condition (lock )
587-
588- def acquire (self ):
589- return self .condition .acquire ()
590-
591- def release (self ):
592- self .condition .release ()
593-
594- def wait (self , timeout = None ):
595- assert timeout is not None
596- timeouts .append (timeout )
597- self .condition .wait (timeout )
576+ if sys .platform == 'win32' :
577+ raise SkipTest ('timing unreliable on Windows' )
598578
599- def notify (self , n = 1 ):
600- self .condition .notify (n )
601-
602- def notify_all (self ):
603- self .condition .notify_all ()
604-
605- def timer ():
606- return sum (timeouts )
579+ # While ismaster fails, ensure it's called about every 10 ms.
580+ ismaster_count = [0 ]
607581
608582 class TestMonitor (Monitor ):
609583 def _check_with_socket (self , sock_info ):
584+ ismaster_count [0 ] += 1
610585 raise socket .error ('my error' )
611586
612- t = create_mock_topology (monitor_class = TestMonitor ,
613- condition_class = TestCondition ,
614- timer = timer )
587+ t = create_mock_topology (monitor_class = TestMonitor )
615588
616589 with self .assertRaisesRegex (ConnectionFailure , 'my error' ):
617- # Add slop to prevent rounding error.
618- t .select_servers (any_server_selector , server_wait_time = 0.101 )
619-
620- self .assertEqual (
621- 11 ,
622- len (timeouts ),
623- "Expected ismaster to be attempted 101 times, not %d" %
624- len (timeouts ))
590+ t .select_servers (any_server_selector , server_wait_time = 0.5 )
591+
592+ self .assertTrue (
593+ 25 <= ismaster_count [0 ] <= 100 ,
594+ "Expected ismaster to be attempted about 50 times, not %d" %
595+ ismaster_count [0 ])
625596
626597 def test_internal_monitor_error (self ):
627598 exception = AssertionError ('internal error' )
0 commit comments