Skip to content

Commit e0fc2e0

Browse files
author
A. Jesse Jiryu Davis
committed
Apply multithreading unittests to new ReplicaSetConnection class.
1 parent 5ed9ea4 commit e0fc2e0

File tree

3 files changed

+144
-13
lines changed

3 files changed

+144
-13
lines changed

test/test_replica_set_connection.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,7 @@
4242
port = os.environ.get("DB_PORT", 27017)
4343
pair = '%s:%d' % (host, port)
4444

45-
class TestConnection(unittest.TestCase):
46-
45+
class TestConnectionReplicaSetBase(unittest.TestCase):
4746
def setUp(self):
4847
conn = Connection(pair)
4948
response = conn.admin.command('ismaster')
@@ -58,11 +57,12 @@ def setUp(self):
5857
else:
5958
raise SkipTest()
6059

61-
def _get_connection(self, **kwargs):
62-
return ReplicaSetConnection(pair,
63-
replicaSet=self.name,
64-
**kwargs)
60+
def _get_connection(self, **kwargs):
61+
return ReplicaSetConnection(pair,
62+
replicaSet=self.name,
63+
**kwargs)
6564

65+
class TestConnection(TestConnectionReplicaSetBase):
6666
def test_connect(self):
6767
self.assertRaises(ConnectionFailure, ReplicaSetConnection,
6868
"somedomainthatdoesntexist.org:27017",

test/test_threads.py

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,16 @@ def run(self):
120120
class TestThreads(unittest.TestCase):
121121

122122
def setUp(self):
123-
self.db = get_connection().pymongo_test
123+
self.db = self._get_connection().pymongo_test
124+
125+
def _get_connection(self):
126+
"""
127+
Intended for overriding in TestThreadsReplicaSet. This method
128+
returns a Connection here, and a ReplicaSetConnection in
129+
test_threads_replica_set_connection.py.
130+
"""
131+
# Regular test connection
132+
return get_connection()
124133

125134
def test_threading(self):
126135
self.db.drop_collection("test")
@@ -197,11 +206,17 @@ def test_low_network_timeout(self):
197206

198207

199208
class TestThreadsAuth(unittest.TestCase):
209+
def _get_connection(self):
210+
"""
211+
Intended for overriding in TestThreadsAuthReplicaSet. This method
212+
returns a Connection here, and a ReplicaSetConnection in
213+
test_threads_replica_set_connection.py.
214+
"""
215+
# Regular test connection
216+
return get_connection()
200217

201218
def setUp(self):
202-
self.conn = get_connection()
203-
204-
# Setup auth users
219+
self.conn = self._get_connection()
205220
self.conn.admin.system.users.remove({})
206221
self.conn.admin.add_user('admin-user', 'password')
207222
try:
@@ -225,11 +240,11 @@ def tearDown(self):
225240
self.conn.drop_database('auth_test')
226241

227242
def test_auto_auth_login(self):
228-
conn = get_connection()
243+
conn = self._get_connection()
229244
self.assertRaises(OperationFailure, conn.auth_test.test.find_one)
230245

231246
# Admin auth
232-
conn = get_connection()
247+
conn = self._get_connection()
233248
conn.admin.authenticate("admin-user", "password")
234249

235250
threads = []
@@ -242,7 +257,7 @@ def test_auto_auth_login(self):
242257
self.assertTrue(t.success)
243258

244259
# Database-specific auth
245-
conn = get_connection()
260+
conn = self._get_connection()
246261
conn.auth_test.authenticate("test-user", "password")
247262

248263
threads = []
Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
# Copyright 2009-2010 10gen, Inc.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
"""Test that pymongo is thread safe."""
16+
17+
import unittest
18+
import os
19+
20+
from nose.plugins.skip import SkipTest
21+
from pymongo.replica_set_connection import ReplicaSetConnection
22+
from test.test_threads import IgnoreAutoReconnect
23+
from test.test_threads import TestThreads as _testThreads
24+
from test.test_threads import TestThreadsAuth as _testThreadsAuth
25+
26+
from test.test_replica_set_connection import (TestConnectionReplicaSetBase,
27+
pair)
28+
29+
from pymongo.errors import AutoReconnect
30+
31+
class TestThreadsReplicaSet(TestConnectionReplicaSetBase, _testThreads):
32+
def setUp(self):
33+
"""
34+
Prepare to test all the same things that TestThreads tests, but do it
35+
with a replica-set connection
36+
"""
37+
TestConnectionReplicaSetBase.setUp(self)
38+
_testThreads.setUp(self)
39+
40+
def _get_connection(self):
41+
"""
42+
Override TestThreads, so its tests run on a ReplicaSetConnection
43+
instead of a regular Connection.
44+
"""
45+
return ReplicaSetConnection(pair, replicaSet=self.name)
46+
47+
def test_low_network_timeout(self):
48+
"""
49+
Override the similar test as TestThreads.test_low_connect_timeout(),
50+
but use the newer connectTimeoutMS and socketTimeoutMS arguments instead
51+
of the soon-to-be-deprecated network_timeout.
52+
"""
53+
db = None
54+
n = 10
55+
56+
# First test connection timeout
57+
i = 0
58+
while db is None and i < n:
59+
try:
60+
db = ReplicaSetConnection(
61+
pair,
62+
replicaSet=self.name,
63+
connectTimeoutMS=1
64+
).pymongo_test
65+
except AutoReconnect:
66+
i += 1
67+
if i == n:
68+
raise SkipTest()
69+
70+
# ... then test socket timeout
71+
i = 0
72+
while db is None and i < n:
73+
try:
74+
db = ReplicaSetConnection(
75+
pair,
76+
replicaSet=self.name,
77+
socketTimeoutMS=1
78+
).pymongo_test
79+
except AutoReconnect:
80+
i += 1
81+
if i == n:
82+
raise SkipTest()
83+
84+
threads = []
85+
for _ in range(4):
86+
t = IgnoreAutoReconnect(db.test, 100)
87+
t.start()
88+
threads.append(t)
89+
90+
for t in threads:
91+
t.join()
92+
93+
94+
class TestThreadsAuthReplicaSet(TestConnectionReplicaSetBase, _testThreadsAuth):
95+
96+
def setUp(self):
97+
"""
98+
Prepare to test all the same things that TestThreads tests, but do it
99+
with a replica-set connection
100+
"""
101+
TestConnectionReplicaSetBase.setUp(self)
102+
_testThreadsAuth.setUp(self)
103+
104+
def _get_connection(self):
105+
"""
106+
Override TestThreadsAuth, so its tests run on a ReplicaSetConnection
107+
instead of a regular Connection.
108+
"""
109+
return ReplicaSetConnection(pair, replicaSet=self.name)
110+
111+
if __name__ == "__main__":
112+
suite = unittest.TestSuite([
113+
unittest.makeSuite(TestThreadsReplicaSet),
114+
unittest.makeSuite(TestThreadsAuthReplicaSet)
115+
])
116+
unittest.TextTestRunner(verbosity=2).run(suite)

0 commit comments

Comments
 (0)