From d578e23b2b9e6ca9d1a0fa02215284a81a4644cf Mon Sep 17 00:00:00 2001 From: Mia Bennett Date: Sun, 2 Aug 2026 16:26:49 +0930 Subject: [PATCH] fix(spec/events): only count the signals this example caused `#notify_change ignores the stale room-mailbox echo but never the master copy` asserts exact signal counts, but its stub recorded every POST to staff/event/changed, including ones it did not cause. Signals are emitted from spawned fibres, so an earlier example's signal can still be in flight when this example installs its stub, and is then counted as ours. A full suite run leaks three such signals: they are visible in the log as `Unhandled exception in spawn: Real HTTP connections are disabled` whenever they land somewhere without a matching stub. Landing here instead takes the count from 1 to 2: Failure/Error: captured_bodies.size.should eq 1 Expected: 1 got: 2 The example passes in isolation and on the seed CI reported, because whether a stray lands inside its window is a matter of timing. It surfaced now because PPT-2640 added a further sixteen examples that emit signals from this controller. Record only signals carrying this example's own dedicated system id, and stand in a foreign signal deliberately so the isolation is asserted rather than left to timing. --- spec/controllers/events_spec.cr | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/spec/controllers/events_spec.cr b/spec/controllers/events_spec.cr index a8f47113..22750940 100644 --- a/spec/controllers/events_spec.cr +++ b/spec/controllers/events_spec.cr @@ -1655,14 +1655,20 @@ describe Events, tags: ["event"] do WebMock.stub(:post, "#{ENV["PLACE_URI"]}/api/engine/v2/signal?channel=staff/guest/attending") .to_return(body: "") + system_id = "sys-mirror-echo-test" + + # Signals are emitted from spawned fibres, so one belonging to an earlier + # example can still be in flight when this one installs its stub and be + # captured here. This example asserts on exact counts, so it records only + # the signals it caused -- identified by its own dedicated system. captured_bodies = [] of String WebMock.stub(:post, "#{ENV["PLACE_URI"]}/api/engine/v2/signal?channel=staff/event/changed") .to_return do |request| - captured_bodies << (request.body.try(&.gets_to_end) || "") + body = request.body.try(&.gets_to_end) || "" + captured_bodies << body if JSON.parse(body)["system_id"]?.try(&.as_s?) == system_id HTTP::Client::Response.new(200, body: "") end - system_id = "sys-mirror-echo-test" PlaceOS::Model::ControlSystem.find?(system_id).try(&.delete) test_system = PlaceOS::Model::Generator.control_system test_system.id = system_id @@ -1707,6 +1713,12 @@ describe Events, tags: ["event"] do first["event_start"].as_i64.should eq thu first["previous_event_start"].as_i64.should eq wed + # A signal from an earlier example's spawned fibre can still be in flight + # and land here. Stand one in deliberately: the counts below are only + # meaningful if a signal for another system is not mistaken for ours. + HTTP::Client.post("#{ENV["PLACE_URI"]}/api/engine/v2/signal?channel=staff/event/changed", + body: %({"system_id": "sys-unrelated-example", "event_id": "evt-unrelated"})) + # 2) Room copy lags and reports the OLD time (Wed): the stale echo that must # now be ignored. client.post("#{EVENTS_BASE}/notify/updated/#{system_id}/#{room_event_id}",