whitfin / local-cluster

Easy local cluster creation for Elixir to aid in unit testing
MIT License
226 stars 30 forks source link

Getting 'undefined function' error on simple test #26

Open willemodendaal opened 2 years ago

willemodendaal commented 2 years ago

I wrote a simplified version of a test based on the documentation, over here: https://github.com/whitfin/local-cluster#usage

However, when I run the test it fails (message not received), and I see the following error in stdout:

$ mix test
.
10:03:57.708 [error] Process #PID<20934.220.0> on node :"my-cluster1@127.0.0.1" raised an exception
** (UndefinedFunctionError) undefined function
    #Function<0.60679234/0 in ChatTest."test test node things"/1>()

10:03:57.708 [error] Process #PID<0.220.0> on node :"my-cluster1@127.0.0.1" raised an exception
** (UndefinedFunctionError) undefined function
    #Function<0.60679234/0 in ChatTest>()

  1) test test node things (ChatTest)
     test/chat_test.exs:5
     Assertion failed, no matching message after 100ms
     The process mailbox is empty.
     code: assert_receive :a_message_from_the_node
     stacktrace:
       test/chat_test.exs:14: (test)

Finished in 3.1 seconds (0.00s async, 3.1s sync)
1 doctest, 1 test, 1 failure

Randomized with seed 660329

My test looks as follows:

defmodule ChatTest do
  use ExUnit.Case
  doctest Chat

  test "test node things" do
    [n1, _n2, _n3] = LocalCluster.start_nodes("my-cluster", 3)

    caller = self()

    _n1_pid = Node.spawn(n1, fn ->
      send(caller, :a_message_from_the_node)
    end)

    assert_receive :a_message_from_the_node
  end
end

My test_helper.exs:

# start the current node as a manager
:ok = LocalCluster.start()

# start your application tree manually
Application.ensure_all_started(:my_app)

# run all tests!
ExUnit.start()

My mix.exs:

defmodule Chat.MixProject do
  use Mix.Project

  def project do
    [
      app: :chat,
      version: "0.1.0",
      elixir: "~> 1.14",
      start_permanent: Mix.env() == :prod,
      deps: deps(),
      aliases: [
        test: "test --no-start"
      ]
    ]
  end

  def application do
    [
      extra_applications: [:logger],
      mod: {Chat.Application, []}
    ]
  end

  defp deps do
    [
      {:local_cluster, "~> 1.2", only: [:test]}
    ]
  end
end

My elixir version:

$ elixir --version
Erlang/OTP 25 [erts-13.0.4] [source] [64-bit] [smp:10:10] [ds:10:10:10] [async-threads:1] [jit]

Elixir 1.14.0 (compiled with Erlang/OTP 25)

Anything obvious that I'm doing wrong? I get the same behavior when I use receive do ... instead of assert_receive When I delete the assert_receive line, I don't get the 'undefined function` error anymore.

NaseeraA commented 2 years ago

The documentation indicates that the test code is not automatically loaded into the cluster. Using the files option when starting the nodes will resolve the error.

image