Repository navigation
test(im): close the lifecycle test database and keep its name unique - #3433
Merged
Merged
Conversation
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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-countiterations in one process, andnewLifecycleTestDBnever closed thesql.DB. Acache=sharedin-memory database lives as long as one connection to it stays open, so the schema outlived the iteration that created it and the nextCREATE TABLEhittable im_channels already exists.Reproduced with the reported command before changing anything:
The fix
Both parts are needed:
t.Cleanupcloses thesql.DBimmediately after opening, so the database is released with the test. Registered inside the helper right aftergorm.Open, so the caller's own cleanups (t.Cleanup(svc.Stop)) are registered later and therefore run first, while the database is still usable.TestNewLifecycleTestDBIsUsableTwiceInOneProcessis 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:
t.CleanupcloseTestNewLifecycleTestDBClosesItsConnectionTestNewLifecycleTestDBIsUsableTwiceInOneProcessVerified green:
-count=1and-count=3, both under-race, plusgo build ./...,go vet, andgofmt.On the original miss
My verification for #3415 ran the package as several separate
go testprocesses 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.