From f2cd5c6f2bb06b5557d3fa05fc23f651d3d8e0bc Mon Sep 17 00:00:00 2001 From: "A. Jesse Jiryu Davis" Date: Sun, 29 Apr 2012 12:59:48 -0400 Subject: [PATCH] Slightly refactor threading tests --- test/test_collection.py | 4 ++-- test/test_gridfs.py | 7 +++---- test/test_threads.py | 20 ++++++++++---------- test/utils.py | 7 +++++++ 4 files changed, 22 insertions(+), 16 deletions(-) diff --git a/test/test_collection.py b/test/test_collection.py index cb3fc5d83..f706066c3 100644 --- a/test/test_collection.py +++ b/test/test_collection.py @@ -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() diff --git a/test/test_gridfs.py b/test/test_gridfs.py index 58014de54..7566e6c89 100644 --- a/test/test_gridfs.py +++ b/test/test_gridfs.py @@ -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")) diff --git a/test/test_threads.py b/test/test_threads.py index 030543348..ec22ffc34 100644 --- a/test/test_threads.py +++ b/test/test_threads.py @@ -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): diff --git a/test/utils.py b/test/utils.py index 026846395..743f88f10 100644 --- a/test/utils.py +++ b/test/utils.py @@ -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