Skip to content

Commit dcc1639

Browse files
committed
Standardarize assert timings for wakeble poll and consume
1 parent b91eaa1 commit dcc1639

File tree

1 file changed

+21
-20
lines changed

1 file changed

+21
-20
lines changed

tests/test_Consumer.py

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,8 @@
1414
from tests.common import TestConsumer
1515

1616
# Timing constants for wakeable poll/consume pattern tests
17-
WAKEABLE_POLL_LONGER_TIMEOUT_MIN = 0.2 # Minimum timeout for longer operations (seconds)
18-
WAKEABLE_POLL_LONGER_TIMEOUT_MAX = 2.0 # Maximum timeout for longer operations (seconds)
19-
WAKEABLE_POLL_SHORTER_TIMEOUT_MIN = 0.01 # Minimum timeout for shorter operations (seconds)
20-
WAKEABLE_POLL_SHORTER_TIMEOUT_MAX = 0.5 # Maximum timeout for shorter operations (seconds)
17+
WAKEABLE_POLL_TIMEOUT_MIN = 0.2 # Minimum timeout (seconds)
18+
WAKEABLE_POLL_TIMEOUT_MAX = 2.0 # Maximum timeout (seconds)
2119

2220

2321
def send_sigint_after_delay(delay_seconds):
@@ -741,7 +739,7 @@ def test_calculate_chunk_timeout_utility_function():
741739
elapsed = time.time() - start
742740

743741
assert msg is None, "Assertion 2 failed: Expected None (timeout)"
744-
assert WAKEABLE_POLL_LONGER_TIMEOUT_MIN <= elapsed <= WAKEABLE_POLL_LONGER_TIMEOUT_MAX, \
742+
assert WAKEABLE_POLL_TIMEOUT_MIN <= elapsed <= WAKEABLE_POLL_TIMEOUT_MAX, \
745743
f"Assertion 2 failed: Timeout took {elapsed:.2f}s, expected ~1.0s"
746744
consumer2.close()
747745

@@ -759,7 +757,8 @@ def test_calculate_chunk_timeout_utility_function():
759757
elapsed = time.time() - start
760758

761759
assert msg is None, "Assertion 3 failed: Expected None (timeout)"
762-
assert 0.25 <= elapsed <= 0.45, f"Assertion 3 failed: Timeout took {elapsed:.2f}s, expected ~0.35s"
760+
assert WAKEABLE_POLL_TIMEOUT_MIN <= elapsed <= WAKEABLE_POLL_TIMEOUT_MAX, \
761+
f"Assertion 3 failed: Timeout took {elapsed:.2f}s, expected ~0.35s"
763762
consumer3.close()
764763

765764
# Assertion 4: Very short timeout (< 200ms chunk size)
@@ -896,7 +895,7 @@ def test_check_signals_between_chunks_utility_function():
896895
elapsed = time.time() - start
897896

898897
assert msg is None, "Assertion 3 failed: Expected None (timeout), no signal should not interrupt"
899-
assert WAKEABLE_POLL_LONGER_TIMEOUT_MIN <= elapsed <= WAKEABLE_POLL_LONGER_TIMEOUT_MAX, \
898+
assert WAKEABLE_POLL_TIMEOUT_MIN <= elapsed <= WAKEABLE_POLL_TIMEOUT_MAX, \
900899
f"Assertion 3 failed: No signal timeout took {elapsed:.2f}s, expected ~0.5s"
901900
consumer3.close()
902901

@@ -919,9 +918,8 @@ def test_check_signals_between_chunks_utility_function():
919918
consumer4.poll() # Infinite timeout
920919
except KeyboardInterrupt:
921920
elapsed = time.time() - start
922-
# Should interrupt quickly after signal (within one chunk period)
923-
# Signal sent at 0.6s, should interrupt by ~0.8s (0.6 + 0.2)
924-
assert 0.6 <= elapsed <= 0.9, f"Assertion 4 failed: Every chunk check took {elapsed:.2f}s, expected 0.6-0.9s"
921+
assert WAKEABLE_POLL_TIMEOUT_MIN <= elapsed <= WAKEABLE_POLL_TIMEOUT_MAX, \
922+
f"Assertion 4 failed: Every chunk check took {elapsed:.2f}s, expected {WAKEABLE_POLL_TIMEOUT_MIN}-{WAKEABLE_POLL_TIMEOUT_MAX}s"
925923
consumer4.close()
926924

927925
# Assertion 5: Signal check works during finite timeout
@@ -998,8 +996,8 @@ def test_wakeable_poll_utility_functions_interaction():
998996
# Signal check should happen every chunk
999997
# Signal sent at 0.6s, should interrupt after that
1000998
msg = (f"Assertion 2 failed: Multiple chunks interaction took "
1001-
f"{elapsed:.2f}s, expected {WAKEABLE_POLL_LONGER_TIMEOUT_MIN}-{WAKEABLE_POLL_LONGER_TIMEOUT_MAX}s")
1002-
assert WAKEABLE_POLL_LONGER_TIMEOUT_MIN <= elapsed <= WAKEABLE_POLL_LONGER_TIMEOUT_MAX, msg
999+
f"{elapsed:.2f}s, expected {WAKEABLE_POLL_TIMEOUT_MIN}-{WAKEABLE_POLL_TIMEOUT_MAX}s")
1000+
assert WAKEABLE_POLL_TIMEOUT_MIN <= elapsed <= WAKEABLE_POLL_TIMEOUT_MAX, msg
10031001
# Verify chunking was happening (elapsed should be close to signal time + one chunk)
10041002
assert elapsed >= 0.6, f"Assertion 2 failed: Should wait for signal at 0.6s, but interrupted at {elapsed:.2f}s"
10051003
consumer2.close()
@@ -1078,8 +1076,8 @@ def test_wakeable_poll_interruptibility_and_messages():
10781076
elapsed = time.time() - start
10791077
# Signal sent at 0.6s, should interrupt after that
10801078
msg = (f"Assertion 3 failed: Multiple chunks interrupt took "
1081-
f"{elapsed:.2f}s, expected {WAKEABLE_POLL_LONGER_TIMEOUT_MIN}-{WAKEABLE_POLL_LONGER_TIMEOUT_MAX}s")
1082-
assert WAKEABLE_POLL_LONGER_TIMEOUT_MIN <= elapsed <= WAKEABLE_POLL_LONGER_TIMEOUT_MAX, msg
1079+
f"{elapsed:.2f}s, expected {WAKEABLE_POLL_TIMEOUT_MIN}-{WAKEABLE_POLL_TIMEOUT_MAX}s")
1080+
assert WAKEABLE_POLL_TIMEOUT_MIN <= elapsed <= WAKEABLE_POLL_TIMEOUT_MAX, msg
10831081
consumer3.close()
10841082

10851083
# Assertion 4: No signal - timeout works normally
@@ -1096,7 +1094,8 @@ def test_wakeable_poll_interruptibility_and_messages():
10961094
elapsed = time.time() - start
10971095

10981096
assert msg is None, "Assertion 4 failed: Expected None (timeout), no signal should not interrupt"
1099-
assert 0.4 <= elapsed <= 0.6, f"Assertion 4 failed: Normal timeout took {elapsed:.2f}s, expected ~0.5s"
1097+
assert WAKEABLE_POLL_TIMEOUT_MIN <= elapsed <= WAKEABLE_POLL_TIMEOUT_MAX, \
1098+
f"Assertion 4 failed: Normal timeout took {elapsed:.2f}s, expected ~0.5s"
11001099
consumer4.close()
11011100

11021101

@@ -1150,7 +1149,7 @@ def test_wakeable_poll_edge_cases():
11501149
elapsed = time.time() - start
11511150

11521151
assert msg is None, "Assertion 3 failed: Short timeout with no messages should return None"
1153-
assert WAKEABLE_POLL_SHORTER_TIMEOUT_MIN <= elapsed <= WAKEABLE_POLL_SHORTER_TIMEOUT_MAX, \
1152+
assert WAKEABLE_POLL_TIMEOUT_MIN <= elapsed <= WAKEABLE_POLL_TIMEOUT_MAX, \
11541153
f"Assertion 3 failed: Short timeout took {elapsed:.2f}s, expected ~0.1s"
11551154
consumer3.close()
11561155

@@ -1245,8 +1244,8 @@ def test_wakeable_consume_interruptibility_and_messages():
12451244
elapsed = time.time() - start
12461245
# Signal sent at 0.6s, should interrupt after that
12471246
msg = (f"Assertion 3 failed: Multiple chunks interrupt took "
1248-
f"{elapsed:.2f}s, expected {WAKEABLE_POLL_LONGER_TIMEOUT_MIN}-{WAKEABLE_POLL_LONGER_TIMEOUT_MAX}s")
1249-
assert WAKEABLE_POLL_LONGER_TIMEOUT_MIN <= elapsed <= WAKEABLE_POLL_LONGER_TIMEOUT_MAX, msg
1247+
f"{elapsed:.2f}s, expected {WAKEABLE_POLL_TIMEOUT_MIN}-{WAKEABLE_POLL_TIMEOUT_MAX}s")
1248+
assert WAKEABLE_POLL_TIMEOUT_MIN <= elapsed <= WAKEABLE_POLL_TIMEOUT_MAX, msg
12501249
consumer3.close()
12511250

12521251
# Assertion 4: No signal - timeout works normally, returns empty list
@@ -1264,7 +1263,8 @@ def test_wakeable_consume_interruptibility_and_messages():
12641263

12651264
assert isinstance(msglist, list), "Assertion 4 failed: consume() should return a list"
12661265
assert len(msglist) == 0, f"Assertion 4 failed: Expected empty list (timeout), got {len(msglist)} messages"
1267-
assert 0.4 <= elapsed <= 0.6, f"Assertion 4 failed: Normal timeout took {elapsed:.2f}s, expected ~0.5s"
1266+
assert WAKEABLE_POLL_TIMEOUT_MIN <= elapsed <= WAKEABLE_POLL_TIMEOUT_MAX, \
1267+
f"Assertion 4 failed: Normal timeout took {elapsed:.2f}s, expected ~0.5s"
12681268
consumer4.close()
12691269

12701270
# Assertion 5: num_messages=0 returns empty list immediately
@@ -1370,7 +1370,8 @@ def test_wakeable_consume_edge_cases():
13701370

13711371
assert isinstance(msglist, list), "Assertion 5 failed: consume() should return a list"
13721372
assert len(msglist) == 0, "Assertion 5 failed: Short timeout with no messages should return empty list"
1373-
assert 0.05 <= elapsed <= 0.2, f"Assertion 5 failed: Short timeout took {elapsed:.2f}s, expected ~0.1s"
1373+
assert WAKEABLE_POLL_TIMEOUT_MIN <= elapsed <= WAKEABLE_POLL_TIMEOUT_MAX, \
1374+
f"Assertion 5 failed: Short timeout took {elapsed:.2f}s, expected ~0.1s"
13741375
consumer5.close()
13751376

13761377
# Assertion 6: Very short timeout (less than chunk size) works

0 commit comments

Comments
 (0)