Skip to content

Commit 20c1156

Browse files
committed
Document and test node functions
1 parent 63e5b5c commit 20c1156

File tree

4 files changed

+65
-12
lines changed

4 files changed

+65
-12
lines changed

CHANGELOG.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,13 @@
11
# Changelog
22

3+
## v0.22.0 - Unreleased
4+
5+
- The `gleam/erlang/process` module gains the `register`, `unregister`, and
6+
`named` functions.
7+
- The `gleam/erlang/node` module has been created with the `Node` and
8+
`ConnectError` types, and the `self`, `visible`, `connect`, `send`, and
9+
`to_atom` functions.
10+
311
## v0.21.0 - 2023-08-25
412

513
- The `gleam/erlang` module gains the `priv_directory` function.

src/gleam/erlang/node.gleam

Lines changed: 29 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,23 @@ pub type Node
44

55
type DoNotLeak
66

7-
// TODO: test
8-
// TODO: document
7+
/// Return the current node.
8+
///
99
@external(erlang, "erlang", "node")
1010
pub fn self() -> Node
1111

12-
// TODO: test
13-
// TODO: document
12+
/// Return a list of all visible nodes in the cluster, not including the current
13+
/// node.
14+
///
15+
/// The current node can be included by calling `self()` and prepending the
16+
/// result.
17+
///
18+
/// ```gleam
19+
/// let all_nodes = [node.self(), ..node.visible()]
20+
/// ```
21+
///
1422
@external(erlang, "erlang", "nodes")
15-
pub fn list() -> List(Node)
23+
pub fn visible() -> List(Node)
1624

1725
pub type ConnectError {
1826
/// Was unable to connect to the node.
@@ -22,13 +30,24 @@ pub type ConnectError {
2230
LocalNodeIsNotAlive
2331
}
2432

25-
// TODO: test
26-
// TODO: document
33+
// TODO: test unknown node
34+
// TODO: test successfully connecting
35+
/// Establish a connection to a node, so the nodes can send messages to each
36+
/// other and any other connected nodes.
37+
///
38+
/// Returns `Error(FailedToConnect)` if the node is not reachable.
39+
///
40+
/// Returns `Error(LocalNodeIsNotAlive)` if the local node is not alive, meaning
41+
/// it is not running in distributed mode.
42+
///
2743
@external(erlang, "gleam_erlang_ffi", "connect_node")
2844
pub fn connect(node: Atom) -> Result(Node, ConnectError)
2945

3046
// TODO: test
31-
// TODO: document
47+
/// Send a message to a named process on a given node.
48+
///
49+
/// These messages are untyped, like regular Erlang messages.
50+
///
3251
pub fn send(node: Node, name: Atom, message: message) -> Nil {
3352
raw_send(#(name, node), message)
3453
Nil
@@ -37,7 +56,7 @@ pub fn send(node: Node, name: Atom, message: message) -> Nil {
3756
@external(erlang, "erlang", "send")
3857
fn raw_send(receiver: #(Atom, Node), message: message) -> DoNotLeak
3958

40-
// TODO: test
41-
// TODO: document
59+
/// Convert a node to the atom of its name.
60+
///
4261
@external(erlang, "gleam_erlang_ffi", "identity")
4362
pub fn to_atom(node: Node) -> Atom

src/gleam_erlang_ffi.erl

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
insert_selector_handler/3, select/1, select/2, trap_exits/1, map_selector/2,
99
merge_selector/2, flush_messages/0, file_info/1, link_info/1,
1010
priv_directory/1, connect_node/1, register_process/2, unregister_process/1,
11-
process_named/1
11+
process_named/1, identity/1
1212
]).
1313

1414
-define(is_posix_error(Error),
@@ -234,7 +234,7 @@ connect_node(Node) ->
234234
case net_kernel:connect_node(Node) of
235235
true -> {ok, Node};
236236
false -> {error, failed_to_connect};
237-
ignored -> {error, local_node_is_not_online}
237+
ignored -> {error, local_node_is_not_alive}
238238
end.
239239

240240
register_process(Pid, Name) ->
@@ -258,3 +258,6 @@ process_named(Name) ->
258258
Pid when is_pid(Pid) -> {ok, Pid};
259259
_ -> {error, nil}
260260
end.
261+
262+
identity(X) ->
263+
X.

test/gleam/erlang/node_tests.gleam

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import gleam/erlang/node
2+
import gleam/erlang/atom
3+
4+
// TODO: Improve these tests by spawning a peer node.
5+
6+
pub fn self_test() {
7+
let a = node.self()
8+
let b = node.self()
9+
let assert True = a == b
10+
}
11+
12+
pub fn visible_test() {
13+
let assert [] = node.visible()
14+
}
15+
16+
pub fn connect_not_alive_test() {
17+
let name = atom.create_from_string("not_found@localhost")
18+
let assert Error(node.LocalNodeIsNotAlive) = node.connect(name)
19+
}
20+
21+
pub fn to_atom_test() {
22+
let assert "nonode@nohost" = atom.to_string(node.to_atom(node.self()))
23+
}

0 commit comments

Comments
 (0)