PYTHON-724 Remove greenlet- and gevent-specific client tests.

This commit is contained in:
A. Jesse Jiryu Davis 2014-07-15 16:52:24 -04:00
parent 4ab4a979f8
commit be5b4aeff9
5 changed files with 51 additions and 115 deletions

View File

@ -18,10 +18,9 @@
# each method requires running setUp, which takes about 30 seconds to bring up
# a replica set. Thus each method asserts everything we want to assert for a
# given replica-set configuration.
import os
import os
import time
from time import sleep
import ha_tools
@ -91,7 +90,7 @@ class HATestCase(unittest.TestCase):
ha_tools.kill_all_members()
ha_tools.nodes.clear()
ha_tools.routers.clear()
sleep(1) # Let members really die.
time.sleep(1) # Let members really die.
class TestDirectConnection(HATestCase):
@ -205,7 +204,7 @@ class TestPassiveAndHidden(HATestCase):
utils.assertReadFromAll(self, self.c, passives, mode)
ha_tools.kill_members(ha_tools.get_passives(), 2)
sleep(2 * MONITOR_INTERVAL)
time.sleep(2 * MONITOR_INTERVAL)
utils.assertReadFrom(self, self.c, self.c.primary, SECONDARY_PREFERRED)
def tearDown(self):
@ -235,7 +234,7 @@ class TestMonitorRemovesRecoveringMember(HATestCase):
secondary, recovering_secondary = secondaries
ha_tools.set_maintenance(recovering_secondary, True)
sleep(2 * MONITOR_INTERVAL)
time.sleep(2 * MONITOR_INTERVAL)
for mode in SECONDARY, SECONDARY_PREFERRED:
# Don't read from recovering member
@ -282,7 +281,7 @@ class TestTriggeredRefresh(HATestCase):
# Wait for the immediate refresh to complete - we're not waiting for
# the periodic refresh, which has been disabled
sleep(1)
time.sleep(1)
for c in self.c_find_one, self.c_count:
self.assertFalse(c.secondaries)
@ -300,14 +299,14 @@ class TestTriggeredRefresh(HATestCase):
ha_tools.stepdown_primary()
# Make sure the stepdown completes
sleep(1)
time.sleep(1)
# Trigger a refresh
self.assertRaises(AutoReconnect, c_find_one.test.test.find_one)
# Wait for the immediate refresh to complete - we're not waiting for
# the periodic refresh, which has been disabled
sleep(1)
time.sleep(1)
# We've detected the stepdown
self.assertTrue(
@ -336,7 +335,7 @@ class TestHealthMonitor(HATestCase):
for _ in xrange(30):
if c.primary and c.primary != primary:
return True
sleep(1)
time.sleep(1)
return False
killed = ha_tools.kill_primary()
@ -355,11 +354,11 @@ class TestHealthMonitor(HATestCase):
if c.secondaries != secondaries:
return True
sleep(1)
time.sleep(1)
return False
killed = ha_tools.kill_secondary()
sleep(2 * MONITOR_INTERVAL)
time.sleep(2 * MONITOR_INTERVAL)
self.assertTrue(bool(len(killed)))
self.assertEqual(primary, c.primary)
self.assertTrue(readers_changed())
@ -378,7 +377,7 @@ class TestHealthMonitor(HATestCase):
# Wait for new primary
patience_seconds = 30
for _ in xrange(patience_seconds):
sleep(1)
time.sleep(1)
rs_state = c._MongoReplicaSetClient__rs_state
if rs_state.writer and rs_state.writer != primary:
if ha_tools.get_primary():
@ -417,7 +416,7 @@ class TestWritesWithFailover(HATestCase):
# Wait past pool's check interval, so it throws an error from
# get_socket().
sleep(1)
time.sleep(1)
# Verify that we only raise AutoReconnect, not some other error,
# while we wait for new primary.
@ -428,7 +427,7 @@ class TestWritesWithFailover(HATestCase):
# No error, found primary.
break
except AutoReconnect:
sleep(.01)
time.sleep(.01)
else:
self.fail("Couldn't connect to new primary")
@ -645,7 +644,7 @@ class TestReadPreference(HATestCase):
killed = ha_tools.kill_primary()
# Let monitor notice primary's gone
sleep(2 * MONITOR_INTERVAL)
time.sleep(2 * MONITOR_INTERVAL)
# PRIMARY
assertReadFrom(None, PRIMARY)
@ -683,13 +682,13 @@ class TestReadPreference(HATestCase):
ha_tools.wait_for_primary()
ha_tools.kill_members([unpartition_node(secondary)], 2)
sleep(5)
time.sleep(5)
ha_tools.wait_for_primary()
self.assertTrue(MongoClient(
unpartition_node(primary), read_preference=PRIMARY_PREFERRED
).admin.command('ismaster')['ismaster'])
sleep(2 * MONITOR_INTERVAL)
time.sleep(2 * MONITOR_INTERVAL)
# PRIMARY
assertReadFrom(primary, PRIMARY)
@ -844,7 +843,7 @@ class TestReplicaSetAuth(HATestCase):
ha_tools.kill_members(['%s:%d' % primary], 2)
# Let monitor notice primary's gone
sleep(2 * MONITOR_INTERVAL)
time.sleep(2 * MONITOR_INTERVAL)
self.assertFalse(primary == self.c.primary)
# Make sure we can still authenticate
@ -959,7 +958,7 @@ class TestReplicaSetRequest(HATestCase):
# Fail over
ha_tools.kill_primary()
sleep(5)
time.sleep(5)
patience_seconds = 60
for _ in range(patience_seconds):
@ -972,7 +971,7 @@ class TestReplicaSetRequest(HATestCase):
except ConnectionFailure:
pass
sleep(1)
time.sleep(1)
else:
self.fail("Problem with test: No new primary after %s seconds"
% patience_seconds)
@ -1062,7 +1061,7 @@ class TestShipOfTheseus(HATestCase):
except (ConnectionFailure, OperationFailure):
pass
sleep(1)
time.sleep(1)
else:
self.fail("Couldn't recover from reconfig")
@ -1071,23 +1070,23 @@ class TestShipOfTheseus(HATestCase):
if ha_tools.get_primary() and len(ha_tools.get_secondaries()) == 4:
break
sleep(1)
time.sleep(1)
else:
self.fail("New secondaries didn't join")
ha_tools.kill_members([primary, secondary1], 9)
sleep(5)
time.sleep(5)
# Wait for primary.
for _ in xrange(30):
if ha_tools.get_primary() and len(ha_tools.get_secondaries()) == 2:
break
sleep(1)
time.sleep(1)
else:
self.fail("No failover")
sleep(2 * MONITOR_INTERVAL)
time.sleep(2 * MONITOR_INTERVAL)
# No error.
find_one()
@ -1104,7 +1103,7 @@ class TestShipOfTheseus(HATestCase):
# Should be able to reconnect to set even though original seed
# list is useless. Use SECONDARY so we don't have to wait for
# the election, merely for the client to detect members are up.
sleep(2 * MONITOR_INTERVAL)
time.sleep(2 * MONITOR_INTERVAL)
find_one(read_preference=SECONDARY)
# Kill new members and switch back to original two members.
@ -1123,12 +1122,12 @@ class TestShipOfTheseus(HATestCase):
except ConnectionFailure:
pass
sleep(1)
time.sleep(1)
else:
self.fail("Original members didn't become secondaries")
# Should be able to reconnect to set again.
sleep(2 * MONITOR_INTERVAL)
time.sleep(2 * MONITOR_INTERVAL)
find_one(read_preference=SECONDARY)

View File

@ -29,16 +29,14 @@ from bson.tz_util import utc
from pymongo.mongo_client import MongoClient
from pymongo.database import Database
from pymongo.pool import SocketInfo
from pymongo import auth, thread_util
from pymongo import auth
from pymongo.errors import (AutoReconnect,
ConfigurationError,
ConnectionFailure,
InvalidName,
OperationFailure,
PyMongoError)
from test import (db_user,
db_pwd,
client_context,
from test import (client_context,
connection_string,
host,
pair,
@ -148,7 +146,6 @@ class TestClient(IntegrationTest, TestRequestMixin):
self.assertIsInstance(c.is_primary, bool)
self.assertIsInstance(c.is_mongos, bool)
self.assertIsInstance(c.max_pool_size, int)
self.assertIsInstance(c.use_greenlets, bool)
self.assertIsInstance(c.nodes, frozenset)
self.assertIsInstance(c.auto_start_request, bool)
self.assertEqual(dict, c.get_document_class())
@ -213,13 +210,6 @@ class TestClient(IntegrationTest, TestRequestMixin):
self.assertEqual(self.client.port, port)
self.assertEqual(set([(host, port)]), self.client.nodes)
def test_use_greenlets(self):
self.assertFalse(MongoClient(host, port).use_greenlets)
if thread_util.have_gevent:
self.assertTrue(
MongoClient(
host, port, use_greenlets=True).use_greenlets)
def test_database_names(self):
self.client.pymongo_test.test.save({"dummy": u("object")})
self.client.pymongo_test_mike.test.save({"dummy": u("object")})
@ -1028,9 +1018,7 @@ class TestClientLazyConnectBadSeeds(IntegrationTest):
client = collection.database.connection
self.assertEqual(0, len(client.nodes))
lazy_client_trial(
reset, connect, test,
self._get_client, use_greenlets=False)
lazy_client_trial(reset, connect, test, self._get_client)
class TestClientLazyConnectOneGoodSeed(
@ -1058,9 +1046,7 @@ class TestClientLazyConnectOneGoodSeed(
def test(collection):
self.assertEqual(NTHREADS, collection.count())
lazy_client_trial(
reset, insert, test,
self._get_client, use_greenlets=False)
lazy_client_trial(reset, insert, test, self._get_client)
class TestMongoClientFailover(IntegrationTest):

View File

@ -14,13 +14,13 @@
"""Test built in connection-pooling with threads."""
import gc
import random
import socket
import sys
import threading
import time
from multiprocessing import Process, Pipe
import gc
from pymongo import MongoClient
from pymongo.errors import ConfigurationError, ConnectionFailure, \
ExceededMaxWaiters
@ -557,6 +557,11 @@ class TestPooling(_TestPoolingBase):
if sys.platform == "win32":
raise SkipTest("Can't test forking on Windows")
try:
from multiprocessing import Process, Pipe
except ImportError:
raise SkipTest("No multiprocessing module")
coll = self.c.pymongo_test.test
coll.remove()
coll.insert({'_id': 1})

View File

@ -31,7 +31,7 @@ from bson.son import SON
from bson.tz_util import utc
from pymongo.read_preferences import ReadPreference, Secondary, Nearest
from pymongo.mongo_replica_set_client import MongoReplicaSetClient
from pymongo.mongo_replica_set_client import _partition_node, have_gevent
from pymongo.mongo_replica_set_client import _partition_node
from pymongo.database import Database
from pymongo.pool import SocketInfo
from pymongo.errors import (AutoReconnect,
@ -124,7 +124,6 @@ class TestReplicaSetClient(TestReplicaSetClientBase, TestRequestMixin):
self.assertIsInstance(c.is_mongos, bool)
self.assertIsInstance(c.max_pool_size, int)
self.assertIsInstance(c.use_greenlets, bool)
self.assertIsInstance(c.auto_start_request, bool)
self.assertIsInstance(c.tz_aware, bool)
self.assertIsInstance(c.max_bson_size, int)
@ -272,14 +271,6 @@ class TestReplicaSetClient(TestReplicaSetClientBase, TestRequestMixin):
self.assertEqual(c.max_bson_size, 4194304)
c.close()
def test_use_greenlets(self):
self.assertFalse(
MongoReplicaSetClient(pair, replicaSet=self.name).use_greenlets)
if have_gevent:
self.assertTrue(MongoReplicaSetClient(
pair, replicaSet=self.name, use_greenlets=True).use_greenlets)
def test_get_db(self):
client = client_context.rs_client
@ -1175,17 +1166,6 @@ class TestReplicaSetClientLazyConnect(
client.pymongo_test.test_collection.find_one()
# Test concurrent access to a lazily-connecting RS client, with Gevent.
class TestReplicaSetClientLazyConnectGevent(
TestReplicaSetClientBase,
_TestLazyConnectMixin):
use_greenlets = True
@classmethod
def setUpClass(cls):
TestReplicaSetClientBase.setUpClass()
class TestReplicaSetClientLazyConnectBadSeeds(
TestReplicaSetClientBase,
_TestLazyConnectMixin):

View File

@ -23,20 +23,12 @@ import threading
from pymongo import MongoClient, MongoReplicaSetClient
from pymongo.errors import AutoReconnect
from pymongo.pool import NO_REQUEST, NO_SOCKET_YET, SocketInfo
from test import (SkipTest,
client_context,
from test import (client_context,
db_user,
db_pwd)
from test.version import Version
try:
import gevent
has_gevent = True
except ImportError:
has_gevent = False
def get_client(*args, **kwargs):
client = MongoClient(*args, **kwargs)
if client_context.auth_enabled and kwargs.get("_connect", True):
@ -428,7 +420,7 @@ NTRIALS = 5
NTHREADS = 10
def run_threads(collection, target, use_greenlets):
def run_threads(collection, target):
"""Run a target function in many threads.
target is a function taking a Collection and an integer.
@ -436,24 +428,17 @@ def run_threads(collection, target, use_greenlets):
threads = []
for i in range(NTHREADS):
bound_target = my_partial(target, collection, i)
if use_greenlets:
threads.append(gevent.Greenlet(run=bound_target))
else:
threads.append(threading.Thread(target=bound_target))
threads.append(threading.Thread(target=bound_target))
for t in threads:
t.start()
for t in threads:
t.join(30)
if use_greenlets:
# bool(Greenlet) is True if it's alive.
assert not t
else:
assert not t.isAlive()
assert not t.isAlive()
def lazy_client_trial(reset, target, test, get_client, use_greenlets):
def lazy_client_trial(reset, target, test, get_client):
"""Test concurrent operations on a lazily-connecting client.
`reset` takes a collection and resets it for the next trial.
@ -464,9 +449,6 @@ def lazy_client_trial(reset, target, test, get_client, use_greenlets):
`test` takes the lazily-connecting collection and asserts a
post-condition to prove `target` succeeded.
"""
if use_greenlets and not has_gevent:
raise SkipTest('Gevent not installed')
collection = client_context.client.pymongo_test.test
# Make concurrency bugs more likely to manifest.
@ -482,11 +464,9 @@ def lazy_client_trial(reset, target, test, get_client, use_greenlets):
try:
for i in range(NTRIALS):
reset(collection)
lazy_client = get_client(_connect=False,
use_greenlets=use_greenlets)
lazy_client = get_client(_connect=False)
lazy_collection = lazy_client.pymongo_test.test
run_threads(lazy_collection, target, use_greenlets)
run_threads(lazy_collection, target)
test(lazy_collection)
finally:
@ -503,11 +483,7 @@ class _TestLazyConnectMixin(object):
Inherit from this class and from unittest.TestCase, and override
_get_client(self, **kwargs), for testing a lazily-connecting
client, i.e. a client initialized with _connect=False.
Set use_greenlets = True to test with Gevent.
"""
use_greenlets = False
NTRIALS = 5
NTHREADS = 10
@ -521,9 +497,7 @@ class _TestLazyConnectMixin(object):
def test(collection):
self.assertEqual(NTHREADS, collection.count())
lazy_client_trial(
reset, insert, test,
self._get_client, self.use_greenlets)
lazy_client_trial(reset, insert, test, self._get_client)
def test_save(self):
def reset(collection):
@ -535,9 +509,7 @@ class _TestLazyConnectMixin(object):
def test(collection):
self.assertEqual(NTHREADS, collection.count())
lazy_client_trial(
reset, save, test,
self._get_client, self.use_greenlets)
lazy_client_trial(reset, save, test, self._get_client)
def test_update(self):
def reset(collection):
@ -551,9 +523,7 @@ class _TestLazyConnectMixin(object):
def test(collection):
self.assertEqual(NTHREADS, collection.find_one()['i'])
lazy_client_trial(
reset, update, test,
self._get_client, self.use_greenlets)
lazy_client_trial(reset, update, test, self._get_client)
def test_remove(self):
def reset(collection):
@ -566,9 +536,7 @@ class _TestLazyConnectMixin(object):
def test(collection):
self.assertEqual(0, collection.count())
lazy_client_trial(
reset, remove, test,
self._get_client, self.use_greenlets)
lazy_client_trial(reset, remove, test, self._get_client)
def test_find_one(self):
results = []
@ -584,9 +552,7 @@ class _TestLazyConnectMixin(object):
def test(collection):
self.assertEqual(NTHREADS, len(results))
lazy_client_trial(
reset, find_one, test,
self._get_client, self.use_greenlets)
lazy_client_trial(reset, find_one, test, self._get_client)
def test_max_bson_size(self):
# Client should have sane defaults before connecting, and should update