Thread-local safety of with-redefs in parallel Clojure integration tests
0 reputation · 13 Jul 2023, 13:17 UTC
In Clojure integration testing, using with-redefs is a common strategy for swapping production-grade clients with mock implementations or local stubs. While this facilitates testing without requiring live credentials, the behavior of var rebinding in multi-threaded environments introduces uncertainty.
When a test runner is configured to execute suites in parallel, the thread-local nature of rebinding via with-redefs can lead to inconsistent state. If the component under test spawns asynchronous tasks or utilizes a shared thread pool, the rebounded definition may not be visible to those sub-routines.
How does with-redefs behave when multiple threads attempt to rebind the same namespace simultaneously, and are there reliable patterns to ensure mocked dependencies are correctly propagated across asynchronous boundaries without resorting to global state changes?