Patched only time in eventlet and gevent unit tests
This commit is contained in:
@@ -44,6 +44,16 @@ def is_gevent_monkey_patched():
|
|||||||
return socket.socket is gevent.socket.socket
|
return socket.socket is gevent.socket.socket
|
||||||
|
|
||||||
|
|
||||||
|
def is_gevent_time_monkey_patched():
|
||||||
|
import gevent.monkey
|
||||||
|
return "time" in gevent.monkey.saved
|
||||||
|
|
||||||
|
|
||||||
|
def is_eventlet_time_monkey_patched():
|
||||||
|
import eventlet
|
||||||
|
return eventlet.patcher.is_monkey_patched('time')
|
||||||
|
|
||||||
|
|
||||||
def is_monkey_patched():
|
def is_monkey_patched():
|
||||||
return is_gevent_monkey_patched() or is_eventlet_monkey_patched()
|
return is_gevent_monkey_patched() or is_eventlet_monkey_patched()
|
||||||
|
|
||||||
|
|||||||
@@ -16,13 +16,20 @@
|
|||||||
import os
|
import os
|
||||||
import select
|
import select
|
||||||
import socket
|
import socket
|
||||||
import thread
|
try:
|
||||||
import Queue
|
import thread
|
||||||
|
import Queue
|
||||||
|
import __builtin__
|
||||||
|
except:
|
||||||
|
import _thread as thread
|
||||||
|
import queue as Queue
|
||||||
|
import builtins
|
||||||
|
|
||||||
import threading
|
import threading
|
||||||
import __builtin__
|
|
||||||
import ssl
|
import ssl
|
||||||
import time
|
import time
|
||||||
|
import eventlet
|
||||||
|
from imp import reload
|
||||||
|
|
||||||
def eventlet_un_patch_all():
|
def eventlet_un_patch_all():
|
||||||
"""
|
"""
|
||||||
@@ -34,4 +41,7 @@ def eventlet_un_patch_all():
|
|||||||
for to_unpatch in modules_to_unpatch:
|
for to_unpatch in modules_to_unpatch:
|
||||||
reload(to_unpatch)
|
reload(to_unpatch)
|
||||||
|
|
||||||
|
def restore_saved_module(module):
|
||||||
|
reload(module)
|
||||||
|
del eventlet.patcher.already_patched[module.__name__]
|
||||||
|
|
||||||
|
|||||||
@@ -19,8 +19,10 @@ except ImportError:
|
|||||||
import unittest # noqa
|
import unittest # noqa
|
||||||
|
|
||||||
from tests.unit.io.utils import submit_and_wait_for_completion, TimerCallback
|
from tests.unit.io.utils import submit_and_wait_for_completion, TimerCallback
|
||||||
from tests import is_eventlet_monkey_patched
|
from tests import is_eventlet_time_monkey_patched, is_gevent_time_monkey_patched
|
||||||
|
from tests.unit.io.eventlet_utils import restore_saved_module
|
||||||
import time
|
import time
|
||||||
|
from eventlet import monkey_patch
|
||||||
|
|
||||||
try:
|
try:
|
||||||
from cassandra.io.eventletreactor import EventletConnection
|
from cassandra.io.eventletreactor import EventletConnection
|
||||||
@@ -29,12 +31,25 @@ except ImportError:
|
|||||||
|
|
||||||
|
|
||||||
class EventletTimerTest(unittest.TestCase):
|
class EventletTimerTest(unittest.TestCase):
|
||||||
|
need_unpatch = False
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def setUpClass(cls):
|
||||||
|
if is_eventlet_time_monkey_patched():
|
||||||
|
return # no dynamic patching if we have eventlet applied
|
||||||
|
if EventletConnection is not None:
|
||||||
|
if not is_gevent_time_monkey_patched():
|
||||||
|
cls.need_unpatch = True
|
||||||
|
monkey_patch(time=True)
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def tearDownClass(cls):
|
||||||
|
if cls.need_unpatch:
|
||||||
|
restore_saved_module(time)
|
||||||
|
|
||||||
def setUp(self):
|
def setUp(self):
|
||||||
if EventletConnection is None:
|
if not is_eventlet_time_monkey_patched():
|
||||||
raise unittest.SkipTest("Eventlet libraries not available")
|
raise unittest.SkipTest("Can't test gevent without monkey patching")
|
||||||
if not is_eventlet_monkey_patched():
|
|
||||||
raise unittest.SkipTest("Can't test eventlet without monkey patching")
|
|
||||||
EventletConnection.initialize_reactor()
|
EventletConnection.initialize_reactor()
|
||||||
|
|
||||||
def test_multi_timer_validation(self, *args):
|
def test_multi_timer_validation(self, *args):
|
||||||
|
|||||||
@@ -19,12 +19,12 @@ except ImportError:
|
|||||||
|
|
||||||
import time
|
import time
|
||||||
from tests.unit.io.utils import submit_and_wait_for_completion, TimerCallback
|
from tests.unit.io.utils import submit_and_wait_for_completion, TimerCallback
|
||||||
from tests import is_gevent_monkey_patched, is_eventlet_monkey_patched
|
from tests import is_gevent_time_monkey_patched, is_eventlet_monkey_patched
|
||||||
|
|
||||||
try:
|
try:
|
||||||
from cassandra.io.geventreactor import GeventConnection
|
from cassandra.io.geventreactor import GeventConnection
|
||||||
import gevent.monkey
|
import gevent.monkey
|
||||||
from gevent_utils import gevent_un_patch_all
|
from tests.unit.io.gevent_utils import restore_saved_module
|
||||||
except ImportError:
|
except ImportError:
|
||||||
GeventConnection = None # noqa
|
GeventConnection = None # noqa
|
||||||
|
|
||||||
@@ -38,17 +38,17 @@ class GeventTimerTest(unittest.TestCase):
|
|||||||
if is_eventlet_monkey_patched():
|
if is_eventlet_monkey_patched():
|
||||||
return # no dynamic patching if we have eventlet applied
|
return # no dynamic patching if we have eventlet applied
|
||||||
if GeventConnection is not None:
|
if GeventConnection is not None:
|
||||||
if not is_gevent_monkey_patched():
|
if not is_gevent_time_monkey_patched():
|
||||||
cls.need_unpatch = True
|
cls.need_unpatch = True
|
||||||
gevent.monkey.patch_all()
|
gevent.monkey.patch_time()
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def tearDownClass(cls):
|
def tearDownClass(cls):
|
||||||
if cls.need_unpatch:
|
if cls.need_unpatch:
|
||||||
gevent_un_patch_all()
|
restore_saved_module("time")
|
||||||
|
|
||||||
def setUp(self):
|
def setUp(self):
|
||||||
if not is_gevent_monkey_patched():
|
if not is_gevent_time_monkey_patched():
|
||||||
raise unittest.SkipTest("Can't test gevent without monkey patching")
|
raise unittest.SkipTest("Can't test gevent without monkey patching")
|
||||||
GeventConnection.initialize_reactor()
|
GeventConnection.initialize_reactor()
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user