Repository navigation
Conversation
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>.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Before
A Rust test that calls the NIF API cannot run, and fails to link on Linux:
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 aharness = falsetest target runs it in anerlnode:A panic reports its message and location, and a segfault fails only that test:
Porting ten
rustler_testscases 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_testprovides the attribute,receive, and the in-BEAM runtime, backed byrustler_beam_test_macrosandrustler_beam_test_runner. The runner does not depend onrustler. A crate putsrustler_beam_test_runner::main!();intests/beam.rswithharness = false, andlibtest-mimicprovides standardcargo testoutput, filters,--ignored,--list, and nextest support.The runner builds
--lib --features beam_testin its own directory with the test binary's profile, including--release. It startserlwith 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, anif_manifestfeature makesinit!callcodegen_runtime::write_manifestinnif_initto write the module and NIF list only to a new file atRUSTLER_NIF_MANIFESTwithcreate_new. Without the feature,nif_initis unchanged. The driver gets both from one refusederlang:load_nif/2call, 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_infois always0, 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_democovers round trips, a resource, send and receive, and ten ported cases, and CI runs it on Ubuntu.