Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 46 additions & 0 deletions tests/common/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import _thread
import os
import threading
import time
import uuid

Expand Down Expand Up @@ -47,6 +48,20 @@ def send_sigint_after_delay(delay_seconds):
time.sleep(delay_seconds)
_thread.interrupt_main()

@staticmethod
def interrupt_main_after(delay_seconds):
"""Return a context manager that runs send_sigint_after_delay(delay_seconds) in a thread.

Wrap the blocking call to interrupt in it. The KeyboardInterrupt is always raised inside
the with block: if the block ends before the interrupt is sent, leaving the block waits
for it and raises AssertionError instead. A KeyboardInterrupt raised after the block, in
a later test or in pytest itself, would abort the whole test session.

Args:
delay_seconds: Delay in seconds, from entering the block, before interrupting
"""
return _InterruptMainAfter(delay_seconds)

# TODO KIP-932: broker_version() previously branched on
# use_group_protocol_consumer() to return '4.0.0' or '3.9.0'. It is now
# hardcoded to '4.2.0' because share groups require >=4.2.0.
Expand Down Expand Up @@ -124,6 +139,37 @@ def remove_forbidden_conf_group_protocol_consumer(conf):
del conf[prop]


class _InterruptMainAfter:
"""Context manager returned by TestUtils.interrupt_main_after()."""

def __init__(self, delay_seconds):
self._delay_seconds = delay_seconds
self._sent = threading.Event()

def _interrupt(self):
TestUtils.send_sigint_after_delay(self._delay_seconds)
self._sent.set()

def __enter__(self):
threading.Thread(target=self._interrupt, daemon=True).start()

def __exit__(self, exc_type, exc_value, traceback):
# Wait for the scheduled interrupt in every case, so that it is raised here and not later,
# in cleanup, in another test or in pytest, where it would abort the session. This includes
# a block that a different KeyboardInterrupt (a real Ctrl+C) ended first.
try:
self._sent.wait()
except KeyboardInterrupt:
pass
if exc_type is KeyboardInterrupt:
return False # Interrupted inside the block, as intended.
if exc_type is None:
raise AssertionError(
f"Not interrupted: the block ended before the interrupt sent after {self._delay_seconds}s"
)
return False


class TestConsumer(Consumer):
def __init__(self, conf=None, **kwargs):
TestUtils.update_conf_group_protocol(conf)
Expand Down
14 changes: 4 additions & 10 deletions tests/test_ShareConsumer.py
Original file line number Diff line number Diff line change
Expand Up @@ -588,13 +588,10 @@ def test_poll_interruptible_by_signal():
)
sc1.subscribe(['test-topic'])

interrupt_thread = threading.Thread(target=lambda: TestUtils.send_sigint_after_delay(0.4))
interrupt_thread.daemon = True
interrupt_thread.start()

interrupted = False
try:
sc1.poll(timeout=5.0) # 5s budget — interrupt should fire well before
with TestUtils.interrupt_main_after(0.4):
sc1.poll(timeout=5.0) # 5s budget — interrupt should fire well before
except KeyboardInterrupt:
interrupted = True
finally:
Expand All @@ -610,13 +607,10 @@ def test_poll_interruptible_by_signal():
)
sc2.subscribe(['test-topic'])

interrupt_thread = threading.Thread(target=lambda: TestUtils.send_sigint_after_delay(0.4))
interrupt_thread.daemon = True
interrupt_thread.start()

interrupted = False
try:
sc2.poll() # infinite timeout
with TestUtils.interrupt_main_after(0.4):
sc2.poll() # infinite timeout
except KeyboardInterrupt:
interrupted = True
finally:
Expand Down
Loading