From 9111e6355a9acb07fb1dae207b6620ee0dcd592f Mon Sep 17 00:00:00 2001 From: Graham McIntire Date: Thu, 6 Aug 2026 17:57:07 -0500 Subject: [PATCH] fix: make spatial packet tests robust against :process_batch mailbox pollution Replace assert_receive with a receive_spatial_packet helper that ignores :process_batch messages. These are scheduled by handle_events via Process.send_after and can pollute the test process mailbox when tests run in the full test suite. --- test/aprsme/packet_consumer_test.exs | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/test/aprsme/packet_consumer_test.exs b/test/aprsme/packet_consumer_test.exs index 396a1b6..df52135 100644 --- a/test/aprsme/packet_consumer_test.exs +++ b/test/aprsme/packet_consumer_test.exs @@ -533,7 +533,7 @@ defmodule Aprsme.PacketConsumerTest do Process.sleep(50) # Should receive the packet via PubSub with object_name as sender - assert_receive {:spatial_packet, packet}, 1000 + packet = receive_spatial_packet() assert packet.sender == "CALGRY", "Expected sender to be object name CALGRY, got #{packet.sender}" assert packet.latitude == 51.044733 assert packet.longitude == -114.062019 @@ -573,7 +573,7 @@ defmodule Aprsme.PacketConsumerTest do Process.sleep(50) # Should receive the packet via PubSub with item_name as sender - assert_receive {:spatial_packet, packet}, 1000 + packet = receive_spatial_packet() assert packet.sender == "MyItem", "Expected sender to be item name MyItem, got #{packet.sender}" assert packet.latitude == 35.0 assert packet.longitude == -75.0 @@ -807,7 +807,7 @@ defmodule Aprsme.PacketConsumerTest do Process.sleep(50) # Should receive the packet via PubSub - assert_receive {:spatial_packet, packet}, 1000 + packet = receive_spatial_packet() assert packet.sender == "BROADCAST1" end @@ -940,7 +940,7 @@ defmodule Aprsme.PacketConsumerTest do # Wait for async broadcast task to complete Process.sleep(50) - assert_receive {:spatial_packet, packet}, 1000 + packet = receive_spatial_packet() assert packet.sender == "FALLBACK1" end end @@ -1003,4 +1003,15 @@ defmodule Aprsme.PacketConsumerTest do on_exit(fn -> SpatialPubSub.unregister_client(client_id) end) :ok end + + # Receives a spatial packet, ignoring :process_batch messages + # that may be in the mailbox from handle_events scheduling. + defp receive_spatial_packet(timeout \\ 2000) do + receive do + {:spatial_packet, packet} -> packet + :process_batch -> receive_spatial_packet(timeout) + after + timeout -> flunk("Expected {:spatial_packet, _} but did not receive one within #{timeout}ms") + end + end end