Repository navigation
Expand file tree
/
Copy pathtest_threads.py
More file actions
290 lines (231 loc) · 8.38 KB
/
Copy pathtest_threads.py
File metadata and controls
290 lines (231 loc) · 8.38 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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
# Copyright 2009-2010 10gen, 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 that pymongo is thread safe."""
import unittest
import threading
from nose.plugins.skip import SkipTest
from test_connection import get_connection
from pymongo.errors import (AutoReconnect,
OperationFailure,
DuplicateKeyError)
class AutoAuthenticateThreads(threading.Thread):
def __init__(self, collection, num):
threading.Thread.__init__(self)
self.coll = collection
self.num = num
self.success = True
def run(self):
try:
for i in xrange(self.num):
self.coll.insert({'num':i}, safe=True)
self.coll.find_one({'num':i})
except Exception:
self.success = False
class SaveAndFind(threading.Thread):
def __init__(self, collection):
threading.Thread.__init__(self)
self.collection = collection
def run(self):
sum = 0
for document in self.collection.find():
sum += document["x"]
assert sum == 499500, "sum was %d not 499500" % sum
class Insert(threading.Thread):
def __init__(self, collection, n, expect_exception):
threading.Thread.__init__(self)
self.collection = collection
self.n = n
self.expect_exception = expect_exception
def run(self):
for _ in xrange(self.n):
error = True
try:
self.collection.insert({"test": "insert"}, safe=True)
error = False
except:
if not self.expect_exception:
raise
if self.expect_exception:
assert error
class Update(threading.Thread):
def __init__(self, collection, n, expect_exception):
threading.Thread.__init__(self)
self.collection = collection
self.n = n
self.expect_exception = expect_exception
def run(self):
for _ in xrange(self.n):
error = True
try:
self.collection.update({"test": "unique"},
{"$set": {"test": "update"}}, safe=True)
error = False
except:
if not self.expect_exception:
raise
if self.expect_exception:
assert error
class IgnoreAutoReconnect(threading.Thread):
def __init__(self, collection, n):
threading.Thread.__init__(self)
self.c = collection
self.n = n
def run(self):
for _ in range(self.n):
try:
self.c.find_one()
except AutoReconnect:
pass
class BaseTestThreads(object):
"""
Base test class for TestThreads and TestThreadsReplicaSet. (This is not
itself a unittest.TestCase, otherwise it'd be run twice -- once when nose
imports this module, and once when nose imports
test_threads_replica_set_connection.py, which imports this module.)
"""
def setUp(self):
self.db = self._get_connection().pymongo_test
def _get_connection(self):
"""
Intended for overriding in TestThreadsReplicaSet. This method
returns a Connection here, and a ReplicaSetConnection in
test_threads_replica_set_connection.py.
"""
# Regular test connection
return get_connection()
def test_threading(self):
self.db.drop_collection("test")
for i in xrange(1000):
self.db.test.save({"x": i}, safe=True)
threads = []
for i in range(10):
t = SaveAndFind(self.db.test)
t.start()
threads.append(t)
for t in threads:
t.join()
def test_safe_insert(self):
self.db.drop_collection("test1")
self.db.test1.insert({"test": "insert"})
self.db.drop_collection("test2")
self.db.test2.insert({"test": "insert"})
self.db.test2.create_index("test", unique=True)
self.db.test2.find_one()
okay = Insert(self.db.test1, 2000, False)
error = Insert(self.db.test2, 2000, True)
error.start()
okay.start()
error.join()
okay.join()
def test_safe_update(self):
self.db.drop_collection("test1")
self.db.test1.insert({"test": "update"})
self.db.test1.insert({"test": "unique"})
self.db.drop_collection("test2")
self.db.test2.insert({"test": "update"})
self.db.test2.insert({"test": "unique"})
self.db.test2.create_index("test", unique=True)
self.db.test2.find_one()
okay = Update(self.db.test1, 2000, False)
error = Update(self.db.test2, 2000, True)
error.start()
okay.start()
error.join()
okay.join()
def test_low_network_timeout(self):
db = None
i = 0
n = 10
while db is None and i < n:
try:
db = get_connection(network_timeout=0.0001).pymongo_test
except AutoReconnect:
i += 1
if i == n:
raise SkipTest()
threads = []
for _ in range(4):
t = IgnoreAutoReconnect(db.test, 100)
t.start()
threads.append(t)
for t in threads:
t.join()
class BaseTestThreadsAuth(object):
"""
Base test class for TestThreadsAuth and TestThreadsAuthReplicaSet. (This is
not itself a unittest.TestCase, otherwise it'd be run twice -- once when
nose imports this module, and once when nose imports
test_threads_replica_set_connection.py, which imports this module.)
"""
def _get_connection(self):
"""
Intended for overriding in TestThreadsAuthReplicaSet. This method
returns a Connection here, and a ReplicaSetConnection in
test_threads_replica_set_connection.py.
"""
# Regular test connection
return get_connection()
def setUp(self):
self.conn = self._get_connection()
self.conn.admin.system.users.remove({})
self.conn.admin.add_user('admin-user', 'password')
try:
self.conn.admin.system.users.find_one()
# If we reach here mongod was likely started
# without --auth. Skip this test since it's
# pointless without auth enabled.
self.tearDown()
raise SkipTest()
except OperationFailure:
pass
self.conn.admin.authenticate("admin-user", "password")
self.conn.auth_test.system.users.remove({})
self.conn.auth_test.add_user("test-user", "password")
def tearDown(self):
# Remove auth users from databases
self.conn.admin.authenticate("admin-user", "password")
self.conn.admin.system.users.remove({})
self.conn.auth_test.system.users.remove({})
self.conn.drop_database('auth_test')
def test_auto_auth_login(self):
conn = self._get_connection()
self.assertRaises(OperationFailure, conn.auth_test.test.find_one)
# Admin auth
conn = self._get_connection()
conn.admin.authenticate("admin-user", "password")
threads = []
for _ in xrange(10):
t = AutoAuthenticateThreads(conn.auth_test.test, 100)
t.start()
threads.append(t)
for t in threads:
t.join()
self.assertTrue(t.success)
# Database-specific auth
conn = self._get_connection()
conn.auth_test.authenticate("test-user", "password")
threads = []
for _ in xrange(10):
t = AutoAuthenticateThreads(conn.auth_test.test, 100)
t.start()
threads.append(t)
for t in threads:
t.join()
self.assertTrue(t.success)
class TestThreads(BaseTestThreads, unittest.TestCase):
pass
class TestThreadsAuth(BaseTestThreadsAuth, unittest.TestCase):
pass
if __name__ == "__main__":
unittest.main()