ExUnited v0.1.2 ExUnited.Spawn View Source

This module is used by ExUnited to spawn nodes for testing purposes.

ExUnited.Spawn uses the Elixir Port module for spawning and as it implements the GenServer behaviour it is able to store state containing information about the spawn nodes.

You will probably not talk to this module directly. Though you can of course try out things in the console.

Example

iex(1)> ExUnited.Spawn.start_link()
{:ok, #PID<0.198.0>}

iex(2)> Node.start(:"captain@127.0.0.1")
{:ok, #PID<0.200.0>}

iex(captain@127.0.0.1)3> ExUnited.Spawn.summon(:"bruce@127.0.0.1", env: [PORT: 5000], verbose: true)
iex(bruce@127.0.0.1)> Interactive Elixir (1.10.1) - press Ctrl+C to exit (type h() ENTER for help)
iex(bruce@127.0.0.1)1>
{#Port<0.8>,
 "iex --name bruce@127.0.0.1 --erl '-connect_all false' -S mix run -e 'Node.connect(:"captain@127.0.0.1")'",
 [{'PORT', '5000'}]}

iex(captain@127.0.0.1)4> Node.list()
[:"bruce@127.0.0.1"]

iex(captain@127.0.0.1)5> ExUnited.Spawn.legion()
%ExUnited.Spawn.State{
  color_index: 1,
  nodes: %{bruce: %{color: "38", node: :"bruce@127.0.0.1", port: #Port<0.8>}}
}

iex(captain@127.0.0.1)6> ExUnited.Spawn.kill_all()
:ok

Link to this section Summary

Functions

Returns a specification to start this module under a supervisor.

Kills and unregisters a spawned node identified by its node name or port.

Kills and unregisters all spawned nodes. If either of the nodes failed to be killed the return value will be :noop and elsewise the return value is :ok.

Returns a %ExUnited.Spawn.State{} containing all its spawned nodes.

Starts the spawn server. The gen server spawnes nodes and stores their references in its state.

Spawns a new node using the specified node name.

Link to this section Functions

Returns a specification to start this module under a supervisor.

See Supervisor.

Link to this function

kill(name_or_port)

View Source
kill(atom() | port()) :: :ok | :noop

Kills and unregisters a spawned node identified by its node name or port.

Link to this function

kill_all()

View Source
kill_all() :: :ok | :noop

Kills and unregisters all spawned nodes. If either of the nodes failed to be killed the return value will be :noop and elsewise the return value is :ok.

Returns a %ExUnited.Spawn.State{} containing all its spawned nodes.

iex(captain@127.0.0.1)8> ExUnited.Spawn.legion()
%ExUnited.Spawn.State{
  color_index: 4,
  nodes: %{
    bruce: %{color: nil, node: :"bruce@127.0.0.1", port: #Port<0.8>},
    clark: %{color: "214", node: :"clark@127.0.0.1", port: #Port<0.12>},
    peter: %{color: "38", node: :"peter@127.0.0.1", port: #Port<0.10>},
    steven: %{color: "112", node: :"steven@127.0.0.1", port: #Port<0.16>},
    tony: %{color: "199", node: :"tony@127.0.0.1", port: #Port<0.14>}
  }
}

See ExUnited.Spawn.State for more information.

Link to this function

start_link()

View Source
start_link() :: {:ok, pid()}

Starts the spawn server. The gen server spawnes nodes and stores their references in its state.

Link to this function

summon(name, opts \\ [])

View Source
summon(atom(), keyword()) ::
  {node(), port(), binary(), [{charlist(), charlist()}]} | :noop

Spawns a new node using the specified node name.

These options are supported:

  • :env - should be a keyword list containing the environment variables which will be used for the spawned node
  • :connect - if true a "fully connected" node will be spawned (see the erl -connect_all flag for more information). Defaults to false
  • :verbose - if true the STDOUT of the spawned node will be printed. Defaults to false

It returns a tuple conforming the following structure:

{node, port, command, env}

where:

  • node - the full nodename of the spawned node (the Node.self() value)
  • port - the corresponding Port reference of the spawned node
  • command - the command used for spawning the node
  • env - the list containing environment variables used spawning the node

Example

{node, port, command, env} = ExUnited.Spawn.summon(:"peter@127.0.0.1", [
  MIX_EXS: "/tmp/peter-mix.exs",
  verbose: true
])

If the name already seems to be registered then a :noop will be returned without spawning the node.