Ecto Sandbox behavior with external side-effects and process isolation
0 reputation · 30 Jan 2022, 01:44 UTC
Managing State in Repeatable Test Environments
Ecto's sandbox mechanism provides test isolation by wrapping operations in transactions that are automatically rolled back. This ensures a clean state for subsequent tests without requiring manual database cleanup.
However, the sandbox only manages the database state. When a test triggers background jobs or external API calls that modify remote resources, these side-effects persist even after the database transaction is rolled back. This creates a discrepancy between the database state and the state of external dependencies, potentially leading to non-deterministic test results in shared development environments.
Assuming Ecto 3.x, how does the sandbox handle process-wide configuration when multiple test processes share a connection pool? Which strategies are recommended to synchronize external side-effects with the sandbox rollback mechanism?