diff --git a/test/event/reset_batch_event_handler_test.exs b/test/event/reset_batch_event_handler_test.exs index 368d13bb..1bf8dcbd 100644 --- a/test/event/reset_batch_event_handler_test.exs +++ b/test/event/reset_batch_event_handler_test.exs @@ -41,10 +41,11 @@ defmodule Commanded.Event.BatchResetEventHandlerTest do test "should be reset when starting from `:current`" do stream_uuid = UUID.uuid4() - # Ignored initial events + # Ignored initial events - add these BEFORE starting the handler initial_events = [%BankAccountOpened{account_number: "ACC123", initial_balance: 1_000}] :ok = EventStore.append_to_stream(BankApp, stream_uuid, 0, to_event_data(initial_events)) + # Start handler with :current - should ignore existing events handler = start_supervised!({BankAccountBatchHandler, start_from: :current}) Wait.until(fn -> @@ -53,8 +54,28 @@ defmodule Commanded.Event.BatchResetEventHandlerTest do :ok = BankAccountBatchHandler.change_prefix("PREF_") + # Use a more robust reset approach with proper synchronization + ref = Process.monitor(handler) send(handler, :reset) + # Wait for the reset message to be processed + receive do + {:DOWN, ^ref, :process, ^handler, _reason} -> + # Handler died, this shouldn't happen + flunk("Handler died during reset") + after + # Reset message should be processed by now + 100 -> :ok + end + + Process.demonitor(ref) + + # Ensure reset completed and accounts are cleared + Wait.until(fn -> + assert BankAccountBatchHandler.current_accounts() == [] + end) + + # Add new event after reset new_event = [%BankAccountOpened{account_number: "ACC1234", initial_balance: 1_000}] :ok = EventStore.append_to_stream(BankApp, stream_uuid, 1, to_event_data(new_event))