Skip to content

Commit 4380c7b

Browse files
committed
Simplify test_subscription_skips_ack_nack_after_reconnection logic and assertions
1 parent b2c6c79 commit 4380c7b

File tree

1 file changed

+5
-27
lines changed

1 file changed

+5
-27
lines changed

packages/stompman/test_stompman/test_subscription.py

Lines changed: 5 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -377,59 +377,37 @@ async def close_connection_soon(client: stompman.Client) -> None:
377377
async def test_subscription_skips_ack_nack_after_reconnection(
378378
monkeypatch: pytest.MonkeyPatch, faker: faker.Faker, caplog: pytest.LogCaptureFixture
379379
) -> None:
380-
"""Test that subscriptions skip ACK/NACK frames after a reconnection using a real client."""
381380
subscription_id, destination, message_id, ack_id = faker.pystr(), faker.pystr(), faker.pystr(), faker.pystr()
382381
monkeypatch.setattr(stompman.subscription, "_make_subscription_id", mock.Mock(return_value=subscription_id))
383-
384-
# Create a message frame as if it was received before reconnection
385382
message_frame = build_dataclass(
386383
MessageFrame,
387384
headers={"destination": destination, "message-id": message_id, "subscription": subscription_id, "ack": ack_id},
388385
)
389-
390-
# Use a spying connection to capture frames
391386
connection_class, collected_frames = create_spying_connection(*get_read_frames_with_lifespan([message_frame]))
392-
393-
# Track if ack/nack frames were sent
394387
stored_message = None
395388

396389
async def track_ack_nack_frames(message: stompman.subscription.AckableMessageFrame) -> None:
397-
# Store reference to the message for later ACK/NACK after reconnection
398390
nonlocal stored_message
399391
stored_message = message
400-
# Don't send ack/nack here, we want to test sending them after reconnection
401-
# Add a small await to make this a proper async function
402392
await asyncio.sleep(0)
403393

404394
async with EnrichedClient(connection_class=connection_class) as client:
405-
# Subscribe to create a subscription with _subscription_reconnection_count = 0
406395
subscription = await client.subscribe_with_manual_ack(destination, track_ack_nack_frames)
407-
await asyncio.sleep(0) # Allow message processing
408-
409-
# Simulate connection loss/reconnection by clearing active connection state
410-
# This will increment _reconnection_count to 1
396+
await asyncio.sleep(0)
411397
client._connection_manager._clear_active_connection_state(build_dataclass(ConnectionLostError))
412-
413-
# Wait for reconnection
414398
await asyncio.sleep(0)
415399

416-
# Try to send ACK/NACK after reconnection - these should be skipped
417400
with caplog.at_level(logging.DEBUG, logger="stompman"):
418401
assert stored_message
419402
await stored_message.ack()
420403
await stored_message.nack()
421404

422405
await subscription.unsubscribe()
423406

424-
# Verify that no ACK or NACK frames were sent
425-
ack_frames = [frame for frame in collected_frames if isinstance(frame, AckFrame)]
426-
nack_frames = [frame for frame in collected_frames if isinstance(frame, NackFrame)]
427-
assert len(ack_frames) == 0, "No ACK frames should be sent after reconnection"
428-
assert len(nack_frames) == 0, "No NACK frames should be sent after reconnection"
429-
430-
# Verify that we logged the skipping messages
431-
assert any("connection changed since message was received" in msg.lower() for msg in caplog.messages), (
432-
"Should log message about connection change"
407+
assert not [one_frame for one_frame in collected_frames if isinstance(one_frame, AckFrame)]
408+
assert not [one_frame for one_frame in collected_frames if isinstance(one_frame, NackFrame)]
409+
assert any(
410+
"connection changed since message was received" in one_message.lower() for one_message in caplog.messages
433411
)
434412

435413

0 commit comments

Comments
 (0)