From a3344da6ff9a8c1d2f3ae71e7a332633b6cf5cd5 Mon Sep 17 00:00:00 2001 From: Yordis Prieto Date: Fri, 29 Aug 2025 21:00:36 -0400 Subject: [PATCH] chore: enhance reset batch event handler test to verify account clearance after reset Signed-off-by: Yordis Prieto --- test/event/reset_batch_event_handler_test.exs | 23 ++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) 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))