Skip to content

Commit d26a2e3

Browse files
committed
Revert "Mock the clock in test_selection_failure."
Not actually a practical method of testing PYTHON-831. This reverts commits a84eeb, 1ec4ab, and 55efc3.
1 parent a84eeb0 commit d26a2e3

File tree

3 files changed

+19
-55
lines changed

3 files changed

+19
-55
lines changed

pymongo/settings.py

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818

1919
from pymongo import monitor, pool
2020
from pymongo.topology_description import TOPOLOGY_TYPE
21-
from pymongo.monotonic import time as _time
2221
from pymongo.pool import PoolOptions
2322
from pymongo.server_description import ServerDescription
2423

@@ -33,7 +32,6 @@ def __init__(
3332
monitor_class=None,
3433
condition_class=None,
3534
local_threshold_ms=15,
36-
timer=None,
3735
):
3836
"""Represent MongoClient's configuration.
3937
@@ -46,7 +44,6 @@ def __init__(
4644
self._monitor_class = monitor_class or monitor.Monitor
4745
self._condition_class = condition_class or threading.Condition
4846
self._local_threshold_ms = local_threshold_ms
49-
self._timer = timer or _time
5047
self._direct = (len(self._seeds) == 1 and not replica_set_name)
5148

5249
@property
@@ -78,11 +75,6 @@ def condition_class(self):
7875
def local_threshold_ms(self):
7976
return self._local_threshold_ms
8077

81-
@property
82-
def timer(self):
83-
"""A function like time.time()."""
84-
return self._timer
85-
8678
@property
8779
def direct(self):
8880
"""Connect directly to a single server, or use a set of servers?

pymongo/topology.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
TOPOLOGY_TYPE,
2626
TopologyDescription)
2727
from pymongo.errors import AutoReconnect
28+
from pymongo.monotonic import time as _time
2829
from pymongo.server import Server
2930
from pymongo.server_selectors import (address_server_selector,
3031
apply_local_threshold,
@@ -80,7 +81,7 @@ def select_servers(self, selector, server_wait_time=None):
8081
with self._lock:
8182
self._description.check_compatible()
8283

83-
now = self._settings.timer()
84+
now = _time()
8485
end_time = now + wait_time
8586
server_descriptions = self._apply_selector(selector)
8687

@@ -98,7 +99,7 @@ def select_servers(self, selector, server_wait_time=None):
9899
# held the lock until now.
99100
self._condition.wait(common.MIN_HEARTBEAT_INTERVAL)
100101
self._description.check_compatible()
101-
now = self._settings.timer()
102+
now = _time()
102103
server_descriptions = self._apply_selector(selector)
103104

104105
return [self.get_server_by_address(sd.address)

test/test_topology.py

Lines changed: 16 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
from pymongo.server_selectors import (any_server_selector,
3737
writable_server_selector)
3838
from pymongo.settings import TopologySettings
39-
from test import client_knobs, unittest
39+
from test import client_knobs, SkipTest, unittest
4040
from test.utils import wait_until
4141

4242

@@ -93,19 +93,16 @@ def get_topology_type(self):
9393
def 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

Comments
 (0)