forked from mongodb/mongo-python-driver
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_heartbeat_monitoring.py
More file actions
142 lines (114 loc) · 4.67 KB
/
test_heartbeat_monitoring.py
File metadata and controls
142 lines (114 loc) · 4.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# Copyright 2016-present MongoDB, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Test the monitoring of the server heartbeats."""
import sys
import threading
sys.path[0:0] = [""]
from pymongo import monitoring
from pymongo.errors import ConnectionFailure
from pymongo.ismaster import IsMaster
from pymongo.monitor import Monitor
from pymongo.pool import PoolOptions
from test import unittest, client_knobs
from test.utils import HeartbeatEventListener, single_client, wait_until
class MockSocketInfo(object):
def close(self):
pass
def __enter__(self):
return self
def __exit__(self, exc_type, exc_val, exc_tb):
pass
class MockPool(object):
def __init__(self, *args, **kwargs):
self.pool_id = 0
self._lock = threading.Lock()
self.opts = PoolOptions()
def get_socket(self, all_credentials):
return MockSocketInfo()
def return_socket(self, _):
pass
def reset(self):
with self._lock:
self.pool_id += 1
def remove_stale_sockets(self):
pass
class TestHeartbeatMonitoring(unittest.TestCase):
@classmethod
def setUpClass(cls):
cls.saved_listeners = monitoring._LISTENERS
monitoring._LISTENERS = monitoring._Listeners([], [], [], [])
@classmethod
def tearDownClass(cls):
monitoring._LISTENERS = cls.saved_listeners
def create_mock_monitor(self, responses, uri, expected_results):
listener = HeartbeatEventListener()
with client_knobs(heartbeat_frequency=0.1,
min_heartbeat_interval=0.1,
events_queue_frequency=0.1):
class MockMonitor(Monitor):
def _check_with_socket(self, *args, **kwargs):
if isinstance(responses[1], Exception):
raise responses[1]
return IsMaster(responses[1]), 99
m = single_client(
h=uri,
event_listeners=(listener,),
_monitor_class=MockMonitor,
_pool_class=MockPool)
expected_len = len(expected_results)
# Wait for *at least* expected_len number of results. The
# monitor thread may run multiple times during the execution
# of this test.
wait_until(
lambda: len(listener.results) >= expected_len,
"publish all events")
try:
# zip gives us len(expected_results) pairs.
for expected, actual in zip(expected_results, listener.results):
self.assertEqual(expected,
actual.__class__.__name__)
self.assertEqual(actual.connection_id,
responses[0])
if expected != 'ServerHeartbeatStartedEvent':
if isinstance(actual.reply, IsMaster):
self.assertEqual(actual.duration, 99)
self.assertEqual(actual.reply._doc, responses[1])
else:
self.assertEqual(actual.reply, responses[1])
finally:
m.close()
def test_standalone(self):
responses = (('a', 27017),
{
"ismaster": True,
"maxWireVersion": 4,
"minWireVersion": 0,
"ok": 1
})
uri = "mongodb://a:27017"
expected_results = ['ServerHeartbeatStartedEvent',
'ServerHeartbeatSucceededEvent']
self.create_mock_monitor(responses, uri, expected_results)
def test_standalone_error(self):
responses = (('a', 27017),
ConnectionFailure("SPECIAL MESSAGE"))
uri = "mongodb://a:27017"
# _check_with_socket failing results in a second attempt.
expected_results = ['ServerHeartbeatStartedEvent',
'ServerHeartbeatFailedEvent',
'ServerHeartbeatStartedEvent',
'ServerHeartbeatFailedEvent']
self.create_mock_monitor(responses, uri, expected_results)
if __name__ == "__main__":
unittest.main()