@@ -41,10 +41,11 @@ defmodule Commanded.Event.BatchResetEventHandlerTest do
4141 test "should be reset when starting from `:current`" do
4242 stream_uuid = UUID . uuid4 ( )
4343
44- # Ignored initial events
44+ # Ignored initial events - add these BEFORE starting the handler
4545 initial_events = [ % BankAccountOpened { account_number: "ACC123" , initial_balance: 1_000 } ]
4646 :ok = EventStore . append_to_stream ( BankApp , stream_uuid , 0 , to_event_data ( initial_events ) )
4747
48+ # Start handler with :current - should ignore existing events
4849 handler = start_supervised! ( { BankAccountBatchHandler , start_from: :current } )
4950
5051 Wait . until ( fn ->
@@ -53,8 +54,27 @@ defmodule Commanded.Event.BatchResetEventHandlerTest do
5354
5455 :ok = BankAccountBatchHandler . change_prefix ( "PREF_" )
5556
57+ # Use a more robust reset approach with proper synchronization
58+ ref = Process . monitor ( handler )
5659 send ( handler , :reset )
5760
61+ # Wait for the reset message to be processed
62+ receive do
63+ { :DOWN , ^ ref , :process , ^ handler , _reason } ->
64+ # Handler died, this shouldn't happen
65+ flunk ( "Handler died during reset" )
66+ after
67+ 100 -> :ok # Reset message should be processed by now
68+ end
69+
70+ Process . demonitor ( ref )
71+
72+ # Ensure reset completed and accounts are cleared
73+ Wait . until ( fn ->
74+ assert BankAccountBatchHandler . current_accounts ( ) == [ ]
75+ end )
76+
77+ # Add new event after reset
5878 new_event = [ % BankAccountOpened { account_number: "ACC1234" , initial_balance: 1_000 } ]
5979 :ok = EventStore . append_to_stream ( BankApp , stream_uuid , 1 , to_event_data ( new_event ) )
6080
0 commit comments