Skip to content

Commit 38aa5c5

Browse files
author
José Valim
committed
Add Node.start/3 and Node.stop/0, closes #2406
1 parent 4f93dae commit 38aa5c5

File tree

3 files changed

+37
-0
lines changed

3 files changed

+37
-0
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
* Enhancements
66
* [ExUnit] The test process now exits with `:shutdown` reason
7+
* [Node] Add `Node.start/3` and `Node.stop/0`
78
* [Task] Log when tasks crash
89

910
* Bug fixes

lib/elixir/lib/node.ex

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,31 @@ defmodule Node do
1010

1111
@type t :: node
1212

13+
@doc """
14+
Turns a non-distributed node into a distributed node.
15+
16+
This functionality starts the `:net_kernel` and other
17+
related processes.
18+
"""
19+
@spec start(node, :longnames | :shortnames, non_neg_integer) ::
20+
{:ok, pid} | {:error, term}
21+
def start(name, type \\ :longnames, tick_time \\ 15000) do
22+
:net_kernel.start([name, type, tick_time])
23+
end
24+
25+
@doc """
26+
Turns a distributed node into a non-distributed node.
27+
28+
For other nodes in the network, this is the same as the node going down.
29+
Only possible when the node was started with `Node.start/3`, otherwise
30+
returns `{:error, :not_allowed}`. Returns `{:error, :not_found}` if the
31+
local node is not alive.
32+
"""
33+
@spec stop() :: :ok | {:error, term}
34+
def stop() do
35+
:net_kernel.stop()
36+
end
37+
1338
@doc """
1439
Returns the current node.
1540

lib/elixir/test/elixir/node_test.exs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
Code.require_file "test_helper.exs", __DIR__
2+
3+
defmodule NodeTest do
4+
use ExUnit.Case
5+
6+
test "start/3 and stop/0" do
7+
assert Node.stop == {:error, :not_found}
8+
assert {:ok, _} = Node.start(:hello, :shortnames, 15000)
9+
assert Node.stop() == :ok
10+
end
11+
end

0 commit comments

Comments
 (0)