test: PacketConsumer batch sizing branches
This commit is contained in:
parent
0f700188f2
commit
b6f34b9cb3
1 changed files with 61 additions and 0 deletions
|
|
@ -40,6 +40,67 @@ defmodule Aprsme.PacketConsumerTest do
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
describe "handle_events/3 batch sizing" do
|
||||||
|
test "partial batch just accumulates and re-schedules" do
|
||||||
|
{:consumer, state, _} = PacketConsumer.init(subscribe_to: [])
|
||||||
|
Process.cancel_timer(state.timer)
|
||||||
|
state = %{state | timer: nil, batch_size: 100, max_batch_size: 1000}
|
||||||
|
|
||||||
|
events = [%{sender: "BATCH-1", raw_packet: "x", received_at: DateTime.utc_now()}]
|
||||||
|
|
||||||
|
assert {:noreply, [], new_state} = PacketConsumer.handle_events(events, nil, state)
|
||||||
|
assert new_state.batch_length == 1
|
||||||
|
assert length(new_state.batch) == 1
|
||||||
|
end
|
||||||
|
|
||||||
|
test "full batch is processed (flushes state)" do
|
||||||
|
{:consumer, state, _} = PacketConsumer.init(subscribe_to: [])
|
||||||
|
Process.cancel_timer(state.timer)
|
||||||
|
|
||||||
|
# batch_size=1 so a single event triggers full-batch handling.
|
||||||
|
state = %{state | timer: nil, batch_size: 1, max_batch_size: 1000}
|
||||||
|
|
||||||
|
events = [
|
||||||
|
%{
|
||||||
|
sender: "FULL-1",
|
||||||
|
base_callsign: "FULL",
|
||||||
|
ssid: "1",
|
||||||
|
raw_packet: "FULL-1>APRS:>",
|
||||||
|
received_at: DateTime.utc_now()
|
||||||
|
}
|
||||||
|
]
|
||||||
|
|
||||||
|
assert {:noreply, [], new_state} = PacketConsumer.handle_events(events, nil, state)
|
||||||
|
assert new_state.batch_length == 0
|
||||||
|
assert new_state.batch == []
|
||||||
|
Process.cancel_timer(new_state.timer)
|
||||||
|
end
|
||||||
|
|
||||||
|
test "oversized batch processes up to max and carries remainder" do
|
||||||
|
{:consumer, state, _} = PacketConsumer.init(subscribe_to: [])
|
||||||
|
Process.cancel_timer(state.timer)
|
||||||
|
|
||||||
|
# batch_size=100, max_batch_size=2, so a 3-event burst is "oversized".
|
||||||
|
state = %{state | timer: nil, batch_size: 100, max_batch_size: 2}
|
||||||
|
|
||||||
|
events =
|
||||||
|
for i <- 1..3 do
|
||||||
|
%{
|
||||||
|
sender: "OS-#{i}",
|
||||||
|
base_callsign: "OS",
|
||||||
|
ssid: to_string(i),
|
||||||
|
raw_packet: "OS-#{i}>APRS:>",
|
||||||
|
received_at: DateTime.utc_now()
|
||||||
|
}
|
||||||
|
end
|
||||||
|
|
||||||
|
assert {:noreply, [], new_state} = PacketConsumer.handle_events(events, nil, state)
|
||||||
|
# Carryover holds the remaining 1 packet past the max.
|
||||||
|
assert new_state.batch_length == 1
|
||||||
|
Process.cancel_timer(new_state.timer)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
describe "handle_info(:process_batch, state)" do
|
describe "handle_info(:process_batch, state)" do
|
||||||
test "empty batch just restarts the timer" do
|
test "empty batch just restarts the timer" do
|
||||||
{:consumer, state, _} = PacketConsumer.init(subscribe_to: [])
|
{:consumer, state, _} = PacketConsumer.init(subscribe_to: [])
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue