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