Slightly refactor threading tests

This commit is contained in:
A. Jesse Jiryu Davis 2012-04-29 12:59:48 -04:00
parent e83ad81522
commit f2cd5c6f2b
4 changed files with 22 additions and 16 deletions

View File

@ -44,6 +44,7 @@ from pymongo.errors import (ConfigurationError,
OperationFailure,
TimeoutError)
from test.test_connection import get_connection
from test.utils import joinall
from test import (qcheck,
version)
@ -224,8 +225,7 @@ class TestCollection(unittest.TestCase):
for i in xrange(10):
threads[i].start()
for i in xrange(10):
threads[i].join()
joinall(threads)
self.assertEqual(10001, coll.count())
coll.drop()

View File

@ -34,6 +34,7 @@ from bson.py3compat import b
from gridfs.errors import (FileExists,
NoFile)
from test.test_connection import get_connection
from test.utils import joinall
class JustWrite(threading.Thread):
@ -156,8 +157,7 @@ class TestGridfs(unittest.TestCase):
threads.append(JustRead(self.fs, 10, results))
threads[i].start()
for i in range(10):
threads[i].join()
joinall(threads)
self.assertEqual(
100 * [b('hello')],
@ -170,8 +170,7 @@ class TestGridfs(unittest.TestCase):
threads.append(JustWrite(self.fs, 10))
threads[i].start()
for i in range(10):
threads[i].join()
joinall(threads)
f = self.fs.get_last_version("test")
self.assertEqual(f.read(), b("hello"))

View File

@ -20,7 +20,7 @@ import traceback
from nose.plugins.skip import SkipTest
from test.utils import server_started_with_auth
from test.utils import server_started_with_auth, joinall
from test.test_connection import get_connection
from pymongo.connection import Connection
from pymongo.replica_set_connection import ReplicaSetConnection
@ -254,8 +254,7 @@ class BaseTestThreads(object):
t.start()
threads.append(t)
for t in threads:
t.join()
joinall(threads)
def test_safe_insert(self):
self.db.drop_collection("test1")
@ -313,8 +312,7 @@ class BaseTestThreads(object):
t.start()
threads.append(t)
for t in threads:
t.join()
joinall(threads)
def test_server_disconnect(self):
# PYTHON-345, we need to make sure that threads' request sockets are
@ -380,9 +378,7 @@ class BaseTestThreads(object):
# Let threads do a second find()
state.ev_resume.set()
for t in threads:
t.join(10)
self.assertFalse(t.isAlive(), "Thread timeout")
joinall(threads)
for t in threads:
self.assertTrue(t.passed, "%s threw exception" % t)
@ -434,8 +430,10 @@ class BaseTestThreadsAuth(object):
t = AutoAuthenticateThreads(conn.auth_test.test, 100)
t.start()
threads.append(t)
joinall(threads)
for t in threads:
t.join()
self.assertTrue(t.success)
# Database-specific auth
@ -447,8 +445,10 @@ class BaseTestThreadsAuth(object):
t = AutoAuthenticateThreads(conn.auth_test.test, 100)
t.start()
threads.append(t)
joinall(threads)
for t in threads:
t.join()
self.assertTrue(t.success)
class TestThreads(BaseTestThreads, unittest.TestCase):

View File

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