* test(im): give each lifecycle test its own sqlite database
newLifecycleTestDB derived its in-memory SQLite DSN from
time.Now().UnixNano() alone. Two tests whose helper calls land in the
same wall-clock tick get the same DSN, and since the database is
cache=shared, the second test's CREATE TABLE collides with the
first's ("table im_channels already exists"). A shared database also
lets one test observe rows created by another.
Every other in-memory test database in this repo names itself from
t.Name() instead (tenant_api_key_test.go, memory/service_test.go,
and others); this helper was the only one still using the wall clock.
Switch to the same convention: t.Name() is unique per test function,
which is all that's needed here since none of the lifecycle tests
call the helper from subtests.
Added TestNewLifecycleTestDBIsolatesConcurrentTests, which runs the
seed and the isolation check as actual parallel subtests rather than
sequentially, closer to the real collision scenario than a plain
sequential pair. Confirmed it fails with the old DSN scheme (fixed
name, forcing the collision) and passes with the fix; ran the full
package 8 times as separate `go test` invocations, matching how the
race is actually triggered, with -race and no failures.
Fixes#3198
Signed-off-by: Ankit Jha <jhaankit373@gmail.com>
Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
* test(im): close the lifecycle test database and keep its name unique
Review on #3415: switching the DSN to t.Name() fixed the collision between
tests started in the same tick but broke repeated runs. t.Name() is stable
across -count iterations in one process, and newLifecycleTestDB never closed
the sql.DB, so a shared-cache in-memory database outlived the iteration that
created it and the next CREATE TABLE failed with "table im_channels already
exists".
Reproduced with the reported command:
go test ./internal/im -run '^TestNewLifecycleTestDBIsolatesConcurrentTests$' -count=2
Two changes, both needed:
- t.Cleanup closes the sql.DB immediately after opening, so the database is
released with the test. Registered before the caller's own cleanups, which
therefore still run first and can use the database while stopping a service.
- A process-wide sequence number in the name, so a single test calling the
helper twice gets two databases; those overlap before any cleanup runs, so
closing alone is not sufficient.
Added the repeated-run check that was asked for, plus one asserting the
connection is actually closed. Verified each half is load-bearing: dropping the
cleanup fails TestNewLifecycleTestDBClosesItsConnection, dropping the sequence
fails TestNewLifecycleTestDBIsUsableTwiceInOneProcess. Package is green at
-count=1 and -count=3 under -race.
---------
Signed-off-by: Ankit Jha <jhaankit373@gmail.com>
Co-authored-by: Claude Sonnet 5 <noreply@anthropic.com>