Skip to content

Commit 4021091

Browse files
committed
tests: handle unavailable dependencies in standalone reactor tests
Share optional reactor imports with the connection fixture so the driver's DependencyException skips only unavailable reactor variants. Keep other runtime and assertion failures visible, and cover both behaviors.
1 parent babdd72 commit 4021091

1 file changed

Lines changed: 34 additions & 19 deletions

File tree

‎tests/unit/test_management_reactors.py‎

Lines changed: 34 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,31 @@
1212
ManagementRuntime, ReactorChannel, execution_context)
1313

1414

15+
def reactor_module(reactor):
16+
try:
17+
return importlib.import_module('cassandra.io.' + reactor + 'reactor')
18+
except (ImportError, DependencyException) as exc:
19+
pytest.skip(str(exc))
20+
21+
22+
@pytest.mark.parametrize('failure', [ImportError, DependencyException])
23+
def test_unavailable_reactor_import_is_skipped(monkeypatch, failure):
24+
def unavailable(name):
25+
raise failure('reactor dependency unavailable')
26+
monkeypatch.setattr(importlib, 'import_module', unavailable)
27+
with pytest.raises(pytest.skip.Exception, match='reactor dependency unavailable'):
28+
reactor_module('libev')
29+
30+
31+
@pytest.mark.parametrize('failure', [RuntimeError, AssertionError])
32+
def test_reactor_import_errors_are_not_hidden(monkeypatch, failure):
33+
def broken(name):
34+
raise failure('supported reactor is broken')
35+
monkeypatch.setattr(importlib, 'import_module', broken)
36+
with pytest.raises(failure, match='supported reactor is broken'):
37+
reactor_module('asyncore')
38+
39+
1540
@pytest.fixture(autouse=True)
1641
def close_borrowed_channels(monkeypatch):
1742
channels = []
@@ -29,12 +54,7 @@ def tracked(self, *args, **kwargs):
2954

3055
@pytest.fixture(params=['asyncio', 'libev', 'asyncore'])
3156
def connection_class(request):
32-
try:
33-
module = importlib.import_module('cassandra.io.' + {
34-
'asyncio': 'asyncioreactor', 'libev': 'libevreactor',
35-
'asyncore': 'asyncorereactor'}[request.param])
36-
except (ImportError, DependencyException) as exc:
37-
pytest.skip(str(exc))
57+
module = reactor_module(request.param)
3858
return getattr(module, {'asyncio': 'AsyncioConnection',
3959
'libev': 'LibevConnection',
4060
'asyncore': 'AsyncoreConnection'}[request.param])
@@ -155,10 +175,7 @@ async def consume():
155175

156176

157177
def test_libev_dispatch_enqueued_during_idle_exit_is_not_lost():
158-
try:
159-
module = importlib.import_module('cassandra.io.libevreactor')
160-
except (ImportError, DependencyException) as exc:
161-
pytest.skip(str(exc))
178+
module = reactor_module('libev')
162179
loop = module.LibevLoop()
163180
native = loop._loop
164181
returned, release, first, second = Event(), Event(), Event(), Event()
@@ -188,7 +205,7 @@ def __getattr__(self, name):
188205

189206

190207
def test_libev_retired_runner_does_not_claim_replacement_work(monkeypatch):
191-
module = pytest.importorskip('cassandra.io.libevreactor')
208+
module = reactor_module('libev')
192209
loop = module.LibevLoop()
193210
native, drain = loop._loop, loop._drain_deferred
194211
retired, release_old, replacement, release_new, delivered = [Event() for _ in range(5)]
@@ -238,7 +255,7 @@ def completion():
238255

239256
@pytest.mark.parametrize('arming_path', ['acquire', 'keepalive'])
240257
def test_libev_keeper_arming_cannot_outlive_cleanup(monkeypatch, arming_path):
241-
module = pytest.importorskip('cassandra.io.libevreactor')
258+
module = reactor_module('libev')
242259
loop = module.LibevLoop()
243260
native = loop._management_keeper
244261
arming, resume, stopped, armed = [Event() for _ in range(4)]
@@ -379,7 +396,7 @@ def holder():
379396

380397
@pytest.mark.parametrize('reactor', ['libev', 'asyncore'])
381398
def test_startup_failure_does_not_acknowledge_concurrent_dispatch(monkeypatch, reactor):
382-
module = pytest.importorskip('cassandra.io.' + reactor + 'reactor')
399+
module = reactor_module(reactor)
383400
if reactor == 'asyncore':
384401
monkeypatch.setattr(module, '_dispatcher_map', {})
385402
class Dispatcher:
@@ -459,7 +476,7 @@ def __exit__(self, *args):
459476

460477

461478
def test_asyncore_dispatch_accepted_during_runner_failure_is_delivered(monkeypatch):
462-
module = pytest.importorskip('cassandra.io.asyncorereactor')
479+
module = reactor_module('asyncore')
463480
monkeypatch.setattr(module, '_dispatcher_map', {})
464481
entered, fail, delivered = Event(), Event(), Event()
465482
class Dispatcher:
@@ -497,7 +514,7 @@ def completion():
497514

498515
def test_asyncore_ordinary_connection_recovers_without_reviving_management(monkeypatch):
499516
import socket
500-
module = pytest.importorskip('cassandra.io.asyncorereactor')
517+
module = reactor_module('asyncore')
501518
monkeypatch.setattr(module, '_dispatcher_map', {})
502519
entered, fail, finish_failure, recovered = [Event() for _ in range(4)]
503520
class Dispatcher:
@@ -566,9 +583,7 @@ def close(self):
566583
@pytest.mark.parametrize('reactor', ['asyncio', 'libev', 'asyncore'])
567584
@pytest.mark.parametrize('count', [1, 130])
568585
def test_reactor_shutdown_delivers_already_accepted_callbacks(monkeypatch, reactor, count):
569-
module = pytest.importorskip('cassandra.io.' + {
570-
'asyncio': 'asyncioreactor', 'libev': 'libevreactor',
571-
'asyncore': 'asyncorereactor'}[reactor])
586+
module = reactor_module(reactor)
572587
entered, release, delivered, stopping = [Event() for _ in range(4)]
573588
if reactor == 'asyncore':
574589
monkeypatch.setattr(module, '_dispatcher_map', {})
@@ -655,7 +670,7 @@ def test_fork_reinitialization_closes_inherited_descriptors(monkeypatch, reactor
655670
from types import SimpleNamespace
656671
if not hasattr(os, 'fork'):
657672
pytest.skip('fork unavailable')
658-
module = pytest.importorskip('cassandra.io.' + reactor + 'reactor')
673+
module = reactor_module(reactor)
659674
connection = getattr(module, {'libev': 'LibevConnection',
660675
'asyncore': 'AsyncoreConnection'}[reactor])
661676
connection.initialize_reactor()

0 commit comments

Comments
 (0)