Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Resetting between tests #177

Closed
frm opened this issue Jun 27, 2018 · 6 comments
Closed

Resetting between tests #177

frm opened this issue Jun 27, 2018 · 6 comments

Comments

@frm
Copy link
Contributor

frm commented Jun 27, 2018

I'm trying to reset the event store and commanded between tests.

I'm using the InMemory event store, currently using reset!/0 to reset its memory. However, I still have issues with tests failing due to subscriptions not being reset and process managers keeping their state between consecutive tests.

I've tried the option of restarting the application and commanded between each test but so far it hasn't worked (is it possible that the process managers are orphaned somehow?). I have also tried rebasing the feature/in-memory-reset branch and use it, but unsuccessful so far.

Errors included:

12:13:35.349 [error] GenServer {Commanded.Registration.LocalRegistry, {Commanded.Event.Handler, "users_projections_handler"}} terminating
** (stop) exited in: GenServer.call(Commanded.EventStore.Adapters.InMemory, {:subscribe_to_all_streams, %Commanded.EventStore.Adapters.InMemory.Subscription{last_seen_event_number: 0, name: "users_projections_handler", start_from: :origin, subscriber: #PID<0.682.0>}}, 5000)
    ** (EXIT) no process: the process is not alive or there's no process currently associated with the given name, possibly because its application isn't started
    (elixir) lib/gen_server.ex:824: GenServer.call/3
    (commanded) lib/commanded/event/handler.ex:417: Commanded.Event.Handler.subscribe_to_all_streams/1
    (commanded) lib/commanded/registration/registration.ex:368: Commanded.Event.Handler.handle_cast/2
    (stdlib) gen_server.erl:616: :gen_server.try_dispatch/4
    (stdlib) gen_server.erl:686: :gen_server.handle_msg/6
    (stdlib) proc_lib.erl:247: :proc_lib.init_p_do_apply/3
Last message: {:"$gen_cast", :subscribe_to_events}

12:13:35.355 [error] GenServer {Commanded.Registration.LocalRegistry, {Commanded.ProcessManagers.ProcessRouter, "workflow_member_registration"}} terminating
** (stop) exited in: GenServer.call(Commanded.EventStore.Adapters.InMemory, {:subscribe_to_all_streams, %Commanded.EventStore.Adapters.InMemory.Subscription{last_seen_event_number: 0, name: "workflow_member_registration", start_from: :origin, subscriber: #PID<0.705.0>}}, 5000)
    ** (EXIT) no process: the process is not alive or there's no process currently associated with the given name, possibly because its application isn't started
    (elixir) lib/gen_server.ex:824: GenServer.call/3
    (commanded) lib/commanded/process_managers/process_router.ex:189: Commanded.ProcessManagers.ProcessRouter.subscribe_to_all_streams/1
    (commanded) lib/commanded/registration/registration.ex:124: Commanded.ProcessManagers.ProcessRouter.handle_cast/2
    (stdlib) gen_server.erl:616: :gen_server.try_dispatch/4
    (stdlib) gen_server.erl:686: :gen_server.handle_msg/6
    (stdlib) proc_lib.erl:247: :proc_lib.init_p_do_apply/3
Last message: {:"$gen_cast", :subscribe_to_events}
@slashdotdash
Copy link
Member

You might find it easier to following the guidance on the Testing your application wiki page which uses the Postgres database and resets the storage after each test run.

@revati
Copy link

revati commented Sep 2, 2018

setup function in my data_case.ex

setup do
  {:ok, event_store_pid} = Commanded.EventStore.Adapters.InMemory.start_link()
  {:ok, _} = Application.ensure_all_started(:my_app)

  :ok = Ecto.Adapters.SQL.Sandbox.checkout(OpenGovernment.Repo)
  Ecto.Adapters.SQL.Sandbox.mode(OpenGovernment.Repo, {:shared, self()})

  on_exit(fn ->
    :ok = Application.stop(:my_app)

    Process.unlink(event_store_pid)
    Process.exit(event_store_pid, :shutdown)

    ref = Process.monitor(event_store_pid)
    assert_receive {:DOWN, ^ref, _, _, _}, 5_000
  end)

  :ok
end

when starting tests, i get

** (ArgumentError) argument error
    (stdlib) :ets.lookup_element(Ecto.Registry, nil, 3)
    (ecto) lib/ecto/registry.ex:18: Ecto.Registry.lookup/1
    (ecto) lib/ecto/adapters/sql/sandbox.ex:426: Ecto.Adapters.SQL.Sandbox.mode/2
    (elixir) lib/code.ex:677: Code.require_file/2
    (elixir) lib/enum.ex:737: Enum."-each/2-lists^foreach/1-0-"/2
    (elixir) lib/enum.ex:737: Enum.each/2
    (mix) lib/mix/tasks/test.ex:256: Mix.Tasks.Test.run/1
    (mix) lib/mix/task.ex:314: Mix.Task.run_task/3
    (mix) lib/mix/task.ex:348: Mix.Task.run_alias/3
    (mix) lib/mix/task.ex:277: Mix.Task.run/2
    (mix) lib/mix/project.ex:351: Mix.Project.in_project/4
    (elixir) lib/file.ex:1419: File.cd!/2
    (mix) lib/mix/task.ex:414: anonymous fn/4 in Mix.Task.recur/1
    (elixir) lib/enum.ex:1899: Enum."-reduce/3-lists^foldl/2-0-"/3
    (mix) lib/mix/task.ex:413: Mix.Task.recur/1
    (mix) lib/mix/project_stack.ex:141: Mix.ProjectStack.recur/1
    (mix) lib/mix/cli.ex:80: Mix.CLI.run_task/2

if i use --no-start as mentioned in documentation tests don't even run, and i get

** (ArgumentError) argument error
    (stdlib) :ets.lookup_element(Ecto.Registry, nil, 3)
    (ecto) lib/ecto/registry.ex:18: Ecto.Registry.lookup/1
    (ecto) lib/ecto/adapters/sql/sandbox.ex:426: Ecto.Adapters.SQL.Sandbox.mode/2
    (elixir) lib/code.ex:677: Code.require_file/2
    (elixir) lib/enum.ex:737: Enum."-each/2-lists^foreach/1-0-"/2

@slashdotdash
Copy link
Member

The in-memory event store page in the wiki has been updated with the approach used by Commanded to test event store integration (in Commanded.StorageCase).

@jfornoff
Copy link
Contributor

jfornoff commented Sep 3, 2018

The :ets.lookup_element(Ecto.Registry, nil, 3) is not caused by anything Commanded related, but rather that the Repo referenced in the Connection-Checkout is not started (at least that was it for our case). Just a heads-up @revati, maybe it saves you some time.

@frm
Copy link
Contributor Author

frm commented Sep 3, 2018

@slashdotdash the StorageCase solution was what I went with, however having to reset the app at times will give errors in different parts, such as consistency timeouts in handlers.

@slashdotdash
Copy link
Member

slashdotdash commented Sep 3, 2018

@fribmendes I configure ExUnit to capture the log so it only outputs logging statements when a test fails. This hides any irrelevant logged warnings or errors.

# config/test.exs
config :ex_unit, capture_log: true

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

4 participants