shahryarjb

shahryarjb

Exited problem in testing GenServer which handles Mnesia in its state

Hi, I created some Unit test to test my GenServer which handles Mnesia in its state, and I need to run some function before each test like delete Mnesia Data on my disk, but I get error like this in each function or GenServer call back which has Mnesia function on it self

Error:

     ** (exit) exited in: GenServer.call(MishkaDatabase.Cache.MnesiaToken, {:get_token_by_user_id, "61095b8c-b172-42c6-8759-b64cacaee32d"}, 5000)
         ** (EXIT) normal

The test I used:

  def delete_old_tokens(_context) do
    MnesiaToken.delete_all_tokens() # this is `handle_cast`
  end

  setup :delete_old_tokens

  test "Get code with code" do
      %{} = assert MnesiaToken.get_token_by_user_id(Ecto.UUID.generate)
  end

now this is my GenServer code:

  def get_token_by_user_id(user_id) do
    GenServer.call(__MODULE__, {:get_token_by_user_id, user_id}, @timeout)
  end
  @impl true
  def handle_call({:get_token_by_user_id, user_id}, _from, state) do
    token_selected = Mnesia.transaction(fn ->
      Mnesia.select(Token, [{{Token, :"$1", :"$2", :"$3", :"$4", :"$5", :"$6"}, [{:"==", :"$2", "#{user_id}"}], [:"$$"]}])
    end)
    |> case do
      {:atomic, []} -> %{}
      {:atomic, data} -> data
      _ -> %{}
    end

    {:reply, token_selected, state}
  end

and my def to delete all Mnesia data before each test

  def delete_all_tokens() do
    GenServer.cast(__MODULE__, {:delete_all_tokens})
  end

  @impl true
  def handle_cast({:delete_all_tokens}, state) do
    Mnesia.transaction(fn ->
      Mnesia.select(Token, [{{Token, :"$1", :"$2", :"$3", :"$4", :"$5", :"$6"}, [], [:"$$"]}])
    end)
    |> case do
      {:atomic, []} -> {:noreply, state}
      {:atomic, data} ->
        Enum.map(data, fn [id, _user_id, _token, _access_expires_in, _create_time, _os] ->
          Mnesia.dirty_delete(Token, id)
        end)
        {:noreply, state}
      _ -> {:noreply, state}
    end
  end

I think this problem is started when the GenServer cant stay to get answer then I use big timeout, unfortunately this didn’t help me.

more problem

I cant create a loop before each test to check whether we have data on mnesia disk or not. it shows me same error like top, for example create check all the token on disk

  def get_all_token() do
    GenServer.call(__MODULE__, {:get_all_token})
  end
  def handle_call({:get_all_token}, _from, state) do
    Mnesia.transaction(fn ->
      Mnesia.select(Token, [{{Token, :"$1", :"$2", :"$3", :"$4", :"$5", :"$6"}, [], [:"$$"]}])
    end)
    |> case do
      {:atomic, []} -> {:reply, %{}, state}
      {:atomic, data} -> {:reply, data, state}
      _ -> {:reply, %{}, state}
    end
  end

again we have a Mnesia checker into GenServer call back and it doesn’t want to answer us

it should be noted I don’t want to save a copy of disk mnesia on my GenServer state

Please help me to create some test for this
Thanks

First Post!

shahryarjb

shahryarjb

I think , Mnesia does not create this error, and this is ‍‍genserver fault which should different unit test, I couldn’t fix this error with many solutions were written in forum

Where Next?

Popular in Questions Top

JorisKok
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
New
joaquinalcerro
Hi there, I am working with Ecto-Postgresql and I need to call all of the records from a specific table but the table has 40,000 record...
New
_russellb
I want to try my hand at web scraping. What tools/libraries do I need to use. I’m hoping to turn this into something professional so don’...
New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New
Jim
As a follow up to my earlier question: I have the code compiling and running but not getting a successful login from the rest server. ...
New
alice
Hey, Just curious what are the main benefits of Elixir compared to Clojure? When is Elixir more useful than Clojure and vice versa? Th...
New
vonH
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
New
fireproofsocks
Forgive me if this is obvious, but how does one delete a database record WITHOUT selecting it first? https://hexdocs.pm/ecto/Ecto.Repo.h...
New
hpopp
To simplify some tasks at work, I wrote and published this package yesterday. It’s a simple macro that enables Access behaviour on struct...
New
9mm
I am constructing a JSON object (map) and I need to conditionally set a field. I’m trying to write proper elixir-way code… and I’m at a l...
New

Other popular topics Top

rms.mrcs
Hi, I need to transform a list of numbers into a map where the keys are the indexes and the values are the original values of the list....
New
TunkShif
This post is an instruction guide to help you setup your Neovim for Elixir development from scratch. It includes general information on h...
273 38985 115
New
senggen
Erlang/OTP 25 [erts-13.2.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] 15:22:35.803 [error] gen_event {lager_file_backend...
New
sergio
In Ruby, I can go: User.find_by(email: "foobar@email.com").update(email: "hello@email.com") How can I do something similar in Elixir? ...
New
joaquinalcerro
Hi there, I am working with Ecto-Postgresql and I need to call all of the records from a specific table but the table has 40,000 record...
New
New
nsuchy
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
New
lk-geimfari
What is most correct way to open, read and parse JSON file with poison? For example if we have example.json file in root of some projec...
New
danschultzer
None of the current solutions worked well for me, so I went ahead and built a user management system from scratch. This project took far...
548 27727 240
New
siddhant3030
Hi, I have to write a raw query for one of my project. But till now I have used ecto queries and don’t have much experience writing raw ...
New

We're in Beta

About us Mission Statement