Skip to content

test(im): close the lifecycle test database and keep its name unique - #3433

Merged
lyingbug merged 2 commits into
Tencent:mainfrom
ankit373:lifecycle-followup
Sep 20, 2026
Merged

lyingbug merged 2 commits into
Tencent:mainfrom
ankit373:lifecycle-followup

Conversation

@ankit373

Copy link
Copy Markdown
Contributor

Follow-up to #3415, which was closed for regressing repeated test execution. Thanks @lyingbug for the precise report; the reproduction was exact.

What went wrong

#3415 changed the lifecycle database DSN from a wall-clock timestamp to t.Name() to stop two tests started in the same tick sharing one in-memory database. That fixed the collision but broke repeats: t.Name() is stable across -count iterations in one process, and newLifecycleTestDB never closed the sql.DB. A cache=shared in-memory database lives as long as one connection to it stays open, so the schema outlived the iteration that created it and the next CREATE TABLE hit table im_channels already exists.

Reproduced with the reported command before changing anything:

go test ./internal/im -run '^TestNewLifecycleTestDBIsolatesConcurrentTests$' -count=2 -timeout=90s

The fix

Both parts are needed:

  1. t.Cleanup closes the sql.DB immediately after opening, so the database is released with the test. Registered inside the helper right after gorm.Open, so the caller's own cleanups (t.Cleanup(svc.Stop)) are registered later and therefore run first, while the database is still usable.
  2. A process-wide sequence number in the name. Closing alone is not sufficient: a single test that calls the helper twice holds both databases open at once, before any cleanup runs. TestNewLifecycleTestDBIsUsableTwiceInOneProcess is exactly that shape.

Tests

Added the repeated-run check you asked for, plus one asserting the connection is actually closed after a subtest ends.

Each half is mutation-checked to confirm it is load-bearing rather than decorative:

removed failing test
the t.Cleanup close TestNewLifecycleTestDBClosesItsConnection
the sequence suffix TestNewLifecycleTestDBIsUsableTwiceInOneProcess

Verified green: -count=1 and -count=3, both under -race, plus go build ./..., go vet, and gofmt.

On the original miss

My verification for #3415 ran the package as several separate go test processes rather than one process with -count>1, which is precisely the case that cannot surface this. That is the check I should have run, and it is now encoded in the suite so it cannot regress silently again.

ankit373 and others added 2 commits September 18, 2026 19:31
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 Tencent#3198

Signed-off-by: Ankit Jha <jhaankit373@gmail.com>
Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
Review on Tencent#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.
@lyingbug
lyingbug merged commit 110134f into Tencent:main Sep 20, 2026
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants