Skip to content

Add rustler_beam_test to run Rust tests in a BEAM node - #789

Closed
rubas wants to merge 18 commits into
rusterlium:masterfrom
rubas:beam-test-harness
Closed

rubas wants to merge 18 commits into
rusterlium:masterfrom
rubas:beam-test-harness

Conversation

@rubas

@rubas rubas commented Nov 15, 2026

Copy link
Copy Markdown
Contributor

Before

A Rust test that calls the NIF API cannot run, and fails to link on Linux:

error: linking with `cc` failed: exit status: 1
  = note: mold: error: undefined symbol: enif_free_env
          mold: error: undefined symbol: enif_inspect_iolist_as_binary
          mold: error: undefined symbol: enif_make_copy

Each test needs a NIF, an Elixir stub, and an ExUnit test.

Problem

Cargo runs test binaries without the BEAM. Stubbing enif_* outside it means keeping over a hundred functions in step with OTP.

After

#[rustler_beam_test::test] compiles a test into the NIF library, and a harness = false test target runs it in an erl node:

#[rustler_beam_test::test]
fn a_message_to_self_arrives(env: Env) -> Receive {
    env.send(&env.pid(), "hello").unwrap();
    receive(|_env, message| assert_eq!(message.decode::<&str>().unwrap(), "hello"))
}
running 26 tests
test ported::owned_environments                  ... ok
test tests::a_resource_reads_back_from_its_term  ... ok
test tests::a_message_to_self_arrives            ... ok
test result: ok. 24 passed; 0 failed; 2 ignored; 0 measured; 0 filtered out

A panic reports its message and location, and a segfault fails only that test:

---- tests::fails_on_purpose ----
panicked in the BEAM at rustler_tests/native/beam_test_demo/src/tests.rs:170:5:
assertion `left == right` failed: the BEAM lost our number
  left: 1
 right: 2

---- tests::crashes_on_purpose ----
the BEAM node died while it ran this test (signal: 11 (SIGSEGV) (core dumped))

Porting ten rustler_tests cases cut 238 lines of NIFs, Elixir stubs, and ExUnit to 164 lines of Rust.

This replaces #787 because the test target links no Rustler code. With [lib] test = false, plain #[test] functions in the library do not run, and tests that need no BEAM use #[rustler_beam_test::test] and run in the node too.

Technical notes

rustler_beam_test provides the attribute, receive, and the in-BEAM runtime, backed by rustler_beam_test_macros and rustler_beam_test_runner. The runner does not depend on rustler. A crate puts rustler_beam_test_runner::main!(); in tests/beam.rs with harness = false, and libtest-mimic provides standard cargo test output, filters, --ignored, --list, and nextest support.

The runner builds --lib --features beam_test in its own directory with the test binary's profile, including --release. It starts erl with the library path in -extra. The driver connects back over loopback TCP and proves itself with a random token so another local process cannot feed the runner results. It then loads the library, lists the tests, and runs each one in a fresh BEAM process.

In rustler, a nif_manifest feature makes init! call codegen_runtime::write_manifest in nif_init to write the module and NIF list only to a new file at RUSTLER_NIF_MANIFEST with create_new. Without the feature, nif_init is unchanged. The driver gets both from one refused erlang:load_nif/2 call, compiles the stub module, and deletes the manifest after loading.

#[ignore] and #[should_panic] work, in both #[should_panic(expected = "...")] and #[should_panic = "..."] forms. The node checks #[should_panic] itself, so a crash cannot pass a #[should_panic] test. When the node dies with several tests in flight, the runner reruns each one alone on a new node so only the culprit fails.

This runs on Linux only so far. The nested build ignores outer --features, load_info is always 0, and the runner does not capture output printed inside the node. A test also cannot call a #[rustler::nif] function because the macro nests it inside its wrapper.

rustler_tests/native/beam_test_demo covers round trips, a resource, send and receive, and ten ported cases, and CI runs it on Ubuntu.

rubas added 17 commits November 15, 2026 12:05
erlang:load_nif/2 accepts a library only from the module that the library
names, and only when that module defines each NIF. A test runner that loads a
library into a throwaway node knows neither. With the nif_manifest feature and
RUSTLER_NIF_MANIFEST set, nif_init writes both to that file, so one refused
call of erlang:load_nif/2 tells the runner what module to compile.
#[rustler_beam_test::test] registers a test as a NIF of the library, behind
the feature beam_test. rustler_beam_test_runner is the harness of a test
target: it builds the library with that feature, starts one erl node, has it
load the library and list the tests, and asks it to run each test in a process
of its own. The test target links no Rustler code, so it needs no BEAM
symbols.
The node stopped a moment after the test binary, so cargo nextest reported
a leaked process in some runs.
The attribute put a constant NAME next to the test function, so a test that
used an item NAME of its own module got the name of its NIF instead.
#[rustler_beam_test::test] handed its test to a second macro, __test!, which
reached rustler through re-exports. The attribute now writes the NIF itself
against the rustler of the library, as #[rustler::nif] does, so a test always
lands in the inventory that rustler::init! reads.
…ifest

The driver called each test NIF once with `describe` to learn whether it is
ignored, and sent the runner its own list of tests. The attribute now puts
`ignored` into the NIF name, and the driver sends back the manifest that the
library wrote, so the runner finds the tests in it and the driver knows
nothing about them until it runs one.
The reader thread owned the process, so the runner kept the thread's join
handle in an Option to wait for the node at exit. The node now holds the
process, and both the reader and the exit wait on it. erl also needs no
-noshell next to -noinput, which implies it.
It also names the forms of #[should_panic] that the attribute takes, since
#[should_panic = "..."] is not one of them.
rustler_beam_test turned on the default features of rustler, so a library
that picks an older NIF version, for an older OTP, got its tests built for
NIF 2.17, which such an OTP refuses to load. It now asks only for the lowest
version that rustler builds with, and the library's own choice wins.
A test named nif, env, or argv did not compile, because the NIF that runs it
used those names and hid the test. The NIF now takes its name from the test,
its arguments are hygienic, and it passes a test that takes an Env as it is.
fs::write followed a link at the path and truncated any file there, so the
variable could make a library overwrite a file of the user. The manifest now
goes only into a file that the library creates.
cargo test --release ran the tests against a dev build of the library. The
runner now reads the profile from the directory of its binary and builds the
library in it.
The runner sent the library path to whatever connected first to its port,
so another local process could take the node's place, report verdicts, or
take the port over and hand erl a library of its choice.

- The library path goes to erl in its arguments, which also keeps a path
  that is not ASCII intact under a non-UTF-8 locale.
- The node first sends a random token from its environment, and the runner
  turns away any peer that does not.
- An erl that the runner lets go of is killed and waited for, and a crashing
  node writes no erl_crash.dump.
- The driver halts when the runner has gone, instead of crashing on a failed
  send or connect, and reports any class of exception while it loads.
The attribute rejected the form that names the expected text after an equals
sign, which a #[test] accepts.
cargo test builds in the profile test, which can differ from dev, and writes
it into the same directory debug.
A manifest that a failed load left behind kept its name of PID and counter,
so a later run with the same PID, as in a container with a cached target
directory, read the old manifest. Each node now names its files with random
bytes, apart from its token.
Cargo 1.100 builds test binaries in <build-dir>/<profile>/build/<pkg>/<hash>/out
instead of <target-dir>/<profile>/deps, so the runner read the hash as the
profile and cargo stopped with `profile ... is not defined`. Cargo gives an
integration test <root>/tmp for its data in either layout, and builds it below
<root>/<profile>, so the runner now builds the library in <root>/tmp/beam_test
and takes the profile from the binary's path below <root>.
@rubas rubas closed this by deleting the head repository Nov 23, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant