Skip to content

Commit f2cd5c6

Browse files
author
A. Jesse Jiryu Davis
committed
Slightly refactor threading tests
1 parent e83ad81 commit f2cd5c6

File tree

4 files changed

+22
-16
lines changed

4 files changed

+22
-16
lines changed

test/test_collection.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
OperationFailure,
4545
TimeoutError)
4646
from test.test_connection import get_connection
47+
from test.utils import joinall
4748
from test import (qcheck,
4849
version)
4950

@@ -224,8 +225,7 @@ def run(self):
224225
for i in xrange(10):
225226
threads[i].start()
226227

227-
for i in xrange(10):
228-
threads[i].join()
228+
joinall(threads)
229229

230230
self.assertEqual(10001, coll.count())
231231
coll.drop()

test/test_gridfs.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
from gridfs.errors import (FileExists,
3535
NoFile)
3636
from test.test_connection import get_connection
37+
from test.utils import joinall
3738

3839

3940
class JustWrite(threading.Thread):
@@ -156,8 +157,7 @@ def test_threaded_reads(self):
156157
threads.append(JustRead(self.fs, 10, results))
157158
threads[i].start()
158159

159-
for i in range(10):
160-
threads[i].join()
160+
joinall(threads)
161161

162162
self.assertEqual(
163163
100 * [b('hello')],
@@ -170,8 +170,7 @@ def test_threaded_writes(self):
170170
threads.append(JustWrite(self.fs, 10))
171171
threads[i].start()
172172

173-
for i in range(10):
174-
threads[i].join()
173+
joinall(threads)
175174

176175
f = self.fs.get_last_version("test")
177176
self.assertEqual(f.read(), b("hello"))

test/test_threads.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020

2121
from nose.plugins.skip import SkipTest
2222

23-
from test.utils import server_started_with_auth
23+
from test.utils import server_started_with_auth, joinall
2424
from test.test_connection import get_connection
2525
from pymongo.connection import Connection
2626
from pymongo.replica_set_connection import ReplicaSetConnection
@@ -254,8 +254,7 @@ def test_threading(self):
254254
t.start()
255255
threads.append(t)
256256

257-
for t in threads:
258-
t.join()
257+
joinall(threads)
259258

260259
def test_safe_insert(self):
261260
self.db.drop_collection("test1")
@@ -313,8 +312,7 @@ def test_low_network_timeout(self):
313312
t.start()
314313
threads.append(t)
315314

316-
for t in threads:
317-
t.join()
315+
joinall(threads)
318316

319317
def test_server_disconnect(self):
320318
# PYTHON-345, we need to make sure that threads' request sockets are
@@ -380,9 +378,7 @@ def test_server_disconnect(self):
380378
# Let threads do a second find()
381379
state.ev_resume.set()
382380

383-
for t in threads:
384-
t.join(10)
385-
self.assertFalse(t.isAlive(), "Thread timeout")
381+
joinall(threads)
386382

387383
for t in threads:
388384
self.assertTrue(t.passed, "%s threw exception" % t)
@@ -434,8 +430,10 @@ def test_auto_auth_login(self):
434430
t = AutoAuthenticateThreads(conn.auth_test.test, 100)
435431
t.start()
436432
threads.append(t)
433+
434+
joinall(threads)
435+
437436
for t in threads:
438-
t.join()
439437
self.assertTrue(t.success)
440438

441439
# Database-specific auth
@@ -447,8 +445,10 @@ def test_auto_auth_login(self):
447445
t = AutoAuthenticateThreads(conn.auth_test.test, 100)
448446
t.start()
449447
threads.append(t)
448+
449+
joinall(threads)
450+
450451
for t in threads:
451-
t.join()
452452
self.assertTrue(t.success)
453453

454454
class TestThreads(BaseTestThreads, unittest.TestCase):

test/utils.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
"""
1717

1818
def delay(sec):
19+
# Javascript sleep() only available in MongoDB since version ~1.9
1920
return '''function() {
2021
var d = new Date((new Date()).getTime() + %s * 1000);
2122
while (d > (new Date())) { }; return true;
@@ -37,3 +38,9 @@ def drop_collections(db):
3738
for coll in db.collection_names():
3839
if not coll.startswith('system'):
3940
db.drop_collection(coll)
41+
42+
def joinall(threads):
43+
"""Join threads with a 5-minute timeout, assert joins succeeded"""
44+
for t in threads:
45+
t.join(300)
46+
assert not t.isAlive(), "Thread %s hung" % t

0 commit comments

Comments
 (0)