Ivan FB 851409aca4
test(e2e): event handler can re-enter the library with a new request
Adds a C++ e2e case proving re-entrancy: from inside an `on_echo_fired`
handler the consumer issues another request to the library, carrying data
taken from the event, and gets a correct response back.

The handler runs on the FFI thread with the event-registry lock held, so the
test documents and exercises the only safe shape: an *async* request. A
synchronous call from the handler would self-deadlock (the FFI thread is busy
running the handler), and add/removeEventListener would deadlock on the
registry lock. The async request merely queues on the FFI channel and is
drained once the handler returns; its future is moved out and resolved on the
main thread. A one-shot guard avoids the echo->event->echo storm (echo
re-fires the event). Timeouts turn any deadlock regression into a failure
rather than a hang.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-05-31 19:56:43 +02:00
..
2026-05-20 14:14:42 -03:00

C++ end-to-end tests

These tests validate that a Nim FFI library exported with nim-ffi's C++ codegen is usable from a real C++ consumer. They drive the my_timer example through its auto-generated my_timer.hpp bindings (constructor, sync method, async methods, complex types with optional fields, multiple contexts, error propagation, async pipelines, short-lived-thread stress, concurrent hammer) and assert the round-tripped values. The CrossLibrary test additionally loads examples/echo's echo.hpp alongside the timer to prove two independent nim-ffi libraries coexist in one process with no symbol clash and no shared global state.

Layout

The suite reuses the generated bindings instead of duplicating the Nim build glue:

  • CMakeLists.txtadd_subdirectorys both examples/timer/cpp_bindings (compiles libmy_timer, exposes my_timer_headers) and examples/echo/cpp_bindings (compiles libecho, exposes echo_headers). Fetches GoogleTest and registers tests with CTest via gtest_discover_tests.
  • test_timer_e2e.cpp — the test cases.

Running

# 1. Generate the C++ bindings for both example libraries
nimble genbindings_cpp        # → examples/timer/cpp_bindings/
nimble genbindings_cpp_echo   # → examples/echo/cpp_bindings/

# 2. Configure + build + run the tests
cmake -S tests/e2e/cpp -B tests/e2e/cpp/build
cmake --build tests/e2e/cpp/build
ctest --test-dir tests/e2e/cpp/build --output-on-failure