Cant reset Mnesia or a DynamicSupervisor in ExUnit setup for each test

Hello friends, this is my cleaner of Mnesia and Job terminator inside one of my test module, and I try to summarize the codes in another step.

My whole code:

  setup do
    Code.ensure_loaded?(PluginWorker)
    :ok = :mnesia.start()
    :mnesia.create_schema([node()])
    :mnesia.create_table(Queue, Queue.database_config())
    :mnesia.create_table(Event, Event.database_config())
    :ok = :mnesia.wait_for_tables([Queue, Event], 5000)
    :persistent_term.put(:event_status, "ready")
    MishkaInstaller.subscribe("event")

    Queue.new(%{
      worker: PluginWorker,
      error: %{type: :continuously, max_try: 5, delay: 0}
    })

    on_exit(fn ->
      :ok = MishkaInstaller.unsubscribe("event")
      :ok = MishkaInstaller.unsubscribe("queue:job")
      Job.terminate_worker(PluginWorker)
      Queue.delete(worker: PluginWorker)
      {:atomic, :ok} = :mnesia.clear_table(Queue)
      {:atomic, :ok} = :mnesia.clear_table(Event)
      :ok = :mnesia.delete_schema([node()])
      :stopped = :mnesia.stop()
    end)

    :ok
  end

As I summarize this I have 2 lines; the first one create inside Mnesia and the other terminate the job which is a PartitionSupervisor -> DynamicSupervisor

 Queue.new(%{
      worker: PluginWorker,
      error: %{type: :continuously, max_try: 5, delay: 0}
    })

#####
Job.terminate_worker(PluginWorker)

Now what is my problem; it terminates the job pid and returns the ok response, but again when I want to create I can see the job is not terminated!!

This is my terminate function

  def terminate(pid) do
    DynamicSupervisor.terminate_child(
      {:via, PartitionSupervisor, {MishkaJobWorkerSupervisor, self()}},
      pid
    )
  end

I can not be able to reset all things for each test what you suggest me for this?

by the way, I forced to change all dirty way of my mnesia to transaction to lock the data when is writing!! but the problem is my DynamicSupervisor.

Thank you in advance

Regarding resetting mnesia: This is how I run test with mnesia in ecto_qlc ecto_qlc/test/support/data_case.ex at main · Schultzer/ecto_qlc · GitHub I usually create a temp directory for the data. But I guess you could always test in memory with a random name for your schema, and lastly, you might be able to wrap the test in a transaction, akin to how ecto sandbox works.

1 Like

Thank you. I already thought about this and other things you said, but my first problem is that I can’t kill all the children of dynamic-supervisor or a child in unit test, unfortunately :face_with_spiral_eyes:.

this part of my code has error in unit test

Unfortunately, I didn’t think about this from the beginning due to my lack of experience in mnesia, and in the same way, I use mnesia internally, which is very dependent on the module I am using, and it is very difficult to change it with random names. All codes must be rewritten from scratch

You would need to stop it first ExUnit.Callbacks — ExUnit v1.16.3 and then exit the pids Process — Elixir v1.17.0-rc.1

1 Like

Yes, I am using it, but it creates another problem for me :)) for example I lost Phoenix PubSub in my tests. I am trying re-write whole my project.
I think that I directed my project forward very badly, and even if these problems are solved now, they may increase in the future.

I want to say thank you I got some idea from your code and mix with my code and fix the problem, for now!