Skip to content

Commit bdbfe02

Browse files
authored
Fix race condition in subscription tests (#1197)
* test: Avoid race with subscription and write. * Count updates correctly and do assertions after cleanup. * Make sure socket does not timeout during test
1 parent 32e1a90 commit bdbfe02

File tree

2 files changed

+10
-9
lines changed

2 files changed

+10
-9
lines changed

tiled/_tests/conftest.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -324,15 +324,16 @@ def tiled_websocket_context(tmpdir, redis_uri):
324324
],
325325
readable_storage=None,
326326
init_if_not_exists=True,
327+
# This uses shorter defaults than the production defaults. Nothing in
328+
# the test suite should be going on for more than ten minutes.
327329
cache_settings={
328330
"uri": redis_uri,
329-
"data_ttl": 60,
330-
"seq_ttl": 60,
331-
"socket_timeout": 10.0,
332-
"socket_connect_timeout": 10.0,
331+
"data_ttl": 600, # 10 minutes
332+
"seq_ttl": 600, # 10 minutes
333+
"socket_timeout": 600, # 10 minutes
334+
"socket_connect_timeout": 10,
333335
},
334336
)
335-
336337
app = build_app(
337338
tree,
338339
authentication=Authentication(single_user_api_key="secret"),

tiled/_tests/test_writing.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -435,14 +435,14 @@ def test_replace_metadata(tiled_websocket_context):
435435
def callback(subscription, data):
436436
"""Callback to collect received messages."""
437437
received.append(data)
438-
if len(received) >= 3: # 3 updates
438+
if len(received) >= 4: # 1 creation + 3 updates
439439
received_event.set()
440440

441441
# Create subscription for the streaming node with start=0
442442
subscription = Subscription(context=context, segments=[])
443443
subscription.add_callback(callback)
444444
# Start the subscription
445-
subscription.start_in_thread()
445+
subscription.start_in_thread(start=1)
446446
# Business Logic
447447
assert len(ac.metadata_revisions[:]) == 0
448448
ac.replace_metadata(metadata={"a": 1}) # update #1
@@ -460,11 +460,11 @@ def callback(subscription, data):
460460
ac.replace_metadata(metadata={"3": 1}, drop_revision=True) # update #3
461461
# Wait for all messages to be received
462462
assert received_event.wait(timeout=5.0), "Timeout waiting for messages"
463-
# Ensure each event generated a websocket response
464-
assert len(received) == 3
465463
# Clean up the subscription
466464
subscription.stop()
467465
assert subscription.closed
466+
# Ensure each event generated a websocket response
467+
assert len(received) == 4
468468

469469

470470
def test_drop_revision(tree):

0 commit comments

Comments
 (0)