aeturnum

aeturnum

Confusing error when trying to start Genserver for multi-node testing

Hello,

I’m getting an odd error in a test I’m trying to write for what I imagined would be a simple multi-node test setup. Basically, in production we run a few different nodes and I’m trying to write some test to verify that the module works. Unfortunately, I ran into a problem where the GenServer (App.Cluster.TaskTests) was not running on the slave nodes and so I tried to start it. Here is where I ran into the odd error.

Here is the relevant part of my code (in a test function):

defmodule App.Cluster.TaskTests do
 use ExUnit.Case
 alias App.Cluster.Tasks

 setup_all do
   :net_kernel.monitor_nodes(true)
   :os.cmd('epmd -daemon')
   Node.start(:test@localhost, :shortnames) |> IO.inspect()

   children = ['child_1', 'child_2', 'child_3']

   for child <- children do
     IO.inspect(child)
     {:ok, node} = :slave.start_link(:localhost, child) |> IO.inspect()
     Node.spawn(node, Supervisor, :init, [[{App.Cluster.Tasks, {}}], strategy: :one_for_one]) |> IO.inspect()
   end

   on_exit(fn ->
     [node() | Node.list()]
     |> Enum.each(fn node -> Node.disconnect(node) end)
     :net_kernel.monitor_nodes(false)
   end)
 end

end

And here is the confusing error I get:

17:41:46.361 [error] Process #PID<37495.88.0> on node :child_1@localhost raised an exception
** (UndefinedFunctionError) function Supervisor.init/2 is undefined or private
    (elixir) Supervisor.init([{App.Cluster.Tasks, {}}], {:strategy, :one_for_one})

I get this error on each child, which of course causes any calls to the Tasks module to fail. I’m sure I’ve made some boneheaded mistake in setting this up (I’ve never worked with Nodes before), but I thought I would ask here and hope someone more experienced could spot my error.

Marked As Solved

aeturnum

aeturnum

I don’t know exactly what I was doing wrong in the code I posted, but I found an example of how to start up other nodes for tests in an older Elixir library that let me get my test working: swarm/cluster.ex at 4aee63d83ad5ee6ee095b38b3ff93a4dbb7c3400 · bitwalker/swarm · GitHub

Hopefully this will be helpful to anyone else in the same fix.

Also Liked

rvirding

rvirding

Creator of Erlang

I think the main problem is that you are using the Supervisor.init/2 function in the wrong way. If you check the docs here you will see it is basically intended to be used to return the supervisor spec in the init/1 callback function. This is called when you start a supervisor with Supervisor.start_link , you don’t explicitly spawn it yourself. This is true for all the OTP behaviours.

aceraxx

aceraxx

I ran into a similar issue while running tests in a similar environment.

What I think is happening is that the slave node is being started, but none of the modules from your master are loaded onto the code server for the slave. Here’s the blog post I read (in two parts) where I uncovered and figured out the solution: Intro: Slave Nodes and Remote Code Loading | by Sean Stavropoulos | Medium

Basically you have to point the slave node to the path where the .beam files live, so it can load it into its code server. This is done using :code.add_paths/1

Here’s what I did that seems to have solved the issue:

defmodule MyAppTest do

  use ExUnit.Case, async: true

  test "distributed test" do
    :net_kernel.start([:node1, :shortnames, 3000])
    {:ok, slave} = start_slave

    # rest of the test
  end

  defp start_slave do
    {:ok, hostname} = :inet.gethostname()
    {:ok, slave} = :slave.start(hostname, 'slave')
    :rpc.block_call(slave, :code, :add_paths, [:code.get_path])
    {:ok, slave}
  end
end

Where Next?

Popular in Questions Top

srinivasu
How to handle excepions in elixir? Suppose i have A, B, C ,D, E modules. and each module has get() function. A.get() method will call th...
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
tduccuong
Hi, is there any work on GUI with Elixir, that is similar to Electron/Javascript? My idea is to bundle Phoenix and BEAM into a single se...
New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New
script
If I have a string “1000 cfu/ml” . I want to remove the characters and / and space . So the string is like this "1000" What is the ...
New
mgjohns61585
Could someone help me? I'm making my first elixir program, number guessing game. I can't figure out how to convert the user's guess from ...
New
shahryarjb
Hello, I have map which I want to convert it to string like this: the map: %{last_name: "tavakkoli", name: "shahryar"} the string I ne...
New
baxterw3b
Hi guys, i’m new in the Elixir world, and i have to say, that i love it! i’m having some problem to understand anonymous functions with ...
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
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New

Other popular topics Top

shahryarjb
Hello, I get Persian date from my client and convert it to normal calendar like this: def jalali_string_to_miladi_english_number(persi...
New
lessless
I believe there are people here who are dealing with CSV files import on the daily basis, and since Excel is a really popular tool there ...
New
ycv005
I have followed this StackOverflow post to install the specific version of Erlang. And When I am running mix ecto.setup then getting fol...
New
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
polypush135
As many of you may have realized by now (sorry for all the posts here) I’ve been working on a db problem where I’m trying to aggregate a ...
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
msaraiva
Surface is an experimental library built on top of Phoenix LiveView and its new LiveComponent API that aims to provide a more declarative...
564 42633 214
New
chrisalley
ExUnit now has describe blocks which is a welcome addition coming from RSpec. In the docs, it states that nested hierarchies of describe ...
New

We're in Beta

About us Mission Statement