diff options
| author | Jonathan Amsterdam <jba@google.com> | 2021-02-12 07:25:21 -0500 |
|---|---|---|
| committer | Jonathan Amsterdam <jba@google.com> | 2021-02-12 21:24:08 +0000 |
| commit | f4412b432a3b3c74fd2ab59e0f3530a934c6557c (patch) | |
| tree | 7e60a20d71c37c3aeb023743fd3190a8e3500c4d /internal/postgres/insert_module_test.go | |
| parent | 443deaea06ee256d48681a37ece9c621b08313f7 (diff) | |
| download | go-x-pkgsite-f4412b432a3b3c74fd2ab59e0f3530a934c6557c.tar.xz | |
internal/postgres: parallelize tests
Run tests in parallel on separate databases.
With four DBs, the time to run all the package's tests is cut in about
half, from 50s to 25s (the time varies from run to run).
Instead of a global variable holding a single DB, we set up a channel
populated with DBs. Each test receives from the channel to get a DB to
use, and sends it to the channel when done.
Because a test may have to wait to get a DB, we can't set an
individual timeout for some tests because acquiring the DB can take
some time. Individual test timeouts aren't really important, because
`go test` will time out the entire run after 10 minutes anyway.
The only test that can't be run in parallel is TestSearch, because it
reads metrics before and after each sub-test to compute a delta,
thereby implicitly assuming that no other Search calls are running in
parallel.
Change-Id: Icb35dbd2f146e27d82d4eef81343cf9725252155
Reviewed-on: https://go-review.googlesource.com/c/pkgsite/+/291449
Trust: Jonathan Amsterdam <jba@google.com>
Run-TryBot: Jonathan Amsterdam <jba@google.com>
Reviewed-by: Julie Qiu <julie@golang.org>
Diffstat (limited to 'internal/postgres/insert_module_test.go')
| -rw-r--r-- | internal/postgres/insert_module_test.go | 70 |
1 files changed, 46 insertions, 24 deletions
diff --git a/internal/postgres/insert_module_test.go b/internal/postgres/insert_module_test.go index f9db63cf..893f368c 100644 --- a/internal/postgres/insert_module_test.go +++ b/internal/postgres/insert_module_test.go @@ -30,8 +30,8 @@ import ( ) func TestInsertModule(t *testing.T) { - ctx, cancel := context.WithTimeout(context.Background(), testTimeout*2) - defer cancel() + t.Parallel() + ctx := context.Background() for _, test := range []struct { name string @@ -76,18 +76,19 @@ func TestInsertModule(t *testing.T) { }, } { t.Run(test.name, func(t *testing.T) { - defer ResetTestDB(testDB, t) + testDB, release := acquire(t) + defer release() MustInsertModule(ctx, t, testDB, test.module) // Test that insertion of duplicate primary key won't fail. MustInsertModule(ctx, t, testDB, test.module) - checkModule(ctx, t, test.module) + checkModule(ctx, t, testDB, test.module) }) } } -func checkModule(ctx context.Context, t *testing.T, want *internal.Module) { - got, err := testDB.GetModuleInfo(ctx, want.ModulePath, want.Version) +func checkModule(ctx context.Context, t *testing.T, db *DB, want *internal.Module) { + got, err := db.GetModuleInfo(ctx, want.ModulePath, want.Version) if err != nil { t.Fatal(err) } @@ -96,7 +97,7 @@ func checkModule(ctx context.Context, t *testing.T, want *internal.Module) { } for _, wantu := range want.Units { - got, err := testDB.GetUnit(ctx, &wantu.UnitMeta, internal.AllFields) + got, err := db.GetUnit(ctx, &wantu.UnitMeta, internal.AllFields) if err != nil { t.Fatal(err) } @@ -119,12 +120,13 @@ func checkModule(ctx context.Context, t *testing.T, want *internal.Module) { } func TestInsertModuleLicenseCheck(t *testing.T) { - ctx, cancel := context.WithTimeout(context.Background(), testTimeout) - defer cancel() - + t.Parallel() + ctx := context.Background() for _, bypass := range []bool{false, true} { t.Run(fmt.Sprintf("bypass=%t", bypass), func(t *testing.T) { - defer ResetTestDB(testDB, t) + testDB, release := acquire(t) + defer release() + var db *DB if bypass { db = NewBypassingLicenseCheck(testDB.db) @@ -174,6 +176,9 @@ func TestInsertModuleLicenseCheck(t *testing.T) { } func TestUpsertModule(t *testing.T) { + t.Parallel() + testDB, release := acquire(t) + defer release() ctx, cancel := context.WithTimeout(context.Background(), testTimeout) defer cancel() @@ -188,12 +193,12 @@ func TestUpsertModule(t *testing.T) { MustInsertModule(ctx, t, testDB, m) // The changes should have been saved. - checkModule(ctx, t, m) + checkModule(ctx, t, testDB, m) } func TestInsertModuleErrors(t *testing.T) { - ctx, cancel := context.WithTimeout(context.Background(), testTimeout*2) - defer cancel() + t.Parallel() + ctx := context.Background() testCases := []struct { name string @@ -259,7 +264,9 @@ func TestInsertModuleErrors(t *testing.T) { for _, test := range testCases { t.Run(test.name, func(t *testing.T) { - defer ResetTestDB(testDB, t) + testDB, release := acquire(t) + defer release() + if _, err := testDB.InsertModule(ctx, test.module); !errors.Is(err, test.wantWriteErr) { t.Errorf("error: %v, want write error: %v", err, test.wantWriteErr) } @@ -268,9 +275,11 @@ func TestInsertModuleErrors(t *testing.T) { } func TestInsertModuleNewCoverage(t *testing.T) { + t.Parallel() + testDB, release := acquire(t) + defer release() ctx, cancel := context.WithTimeout(context.Background(), testTimeout) defer cancel() - defer ResetTestDB(testDB, t) m := sample.DefaultModule() newCoverage := licensecheck.Coverage{ @@ -305,9 +314,11 @@ func TestInsertModuleNewCoverage(t *testing.T) { } func TestPostgres_ReadAndWriteModuleOtherColumns(t *testing.T) { + t.Parallel() // Verify that InsertModule correctly populates the columns in the versions // table that are not in the ModuleInfo struct. - defer ResetTestDB(testDB, t) + testDB, release := acquire(t) + defer release() ctx := context.Background() type other struct { @@ -339,7 +350,9 @@ func TestPostgres_ReadAndWriteModuleOtherColumns(t *testing.T) { } func TestLatestVersion(t *testing.T) { - defer ResetTestDB(testDB, t) + t.Parallel() + testDB, release := acquire(t) + defer release() ctx := context.Background() for _, mod := range []struct { @@ -408,7 +421,9 @@ func TestLatestVersion(t *testing.T) { } func TestLatestVersion_PreferIncompatibleOverPrerelease(t *testing.T) { - defer ResetTestDB(testDB, t) + t.Parallel() + testDB, release := acquire(t) + defer release() ctx := context.Background() for _, mod := range []struct { @@ -451,9 +466,11 @@ func TestLatestVersion_PreferIncompatibleOverPrerelease(t *testing.T) { } func TestDeleteModule(t *testing.T) { + t.Parallel() + testDB, release := acquire(t) + defer release() ctx, cancel := context.WithTimeout(context.Background(), testTimeout) defer cancel() - defer ResetTestDB(testDB, t) v := sample.DefaultModule() @@ -493,11 +510,13 @@ func TestDeleteModule(t *testing.T) { } func TestPostgres_NewerAlternative(t *testing.T) { + t.Parallel() // Verify that packages are not added to search_documents if the module has a newer // alternative version. + testDB, release := acquire(t) + defer release() ctx, cancel := context.WithTimeout(context.Background(), testTimeout) defer cancel() - defer ResetTestDB(testDB, t) const ( modulePath = "example.com/Mod" @@ -518,11 +537,12 @@ func TestPostgres_NewerAlternative(t *testing.T) { } func TestMakeValidUnicode(t *testing.T) { + t.Parallel() + testDB, release := acquire(t) + defer release() ctx, cancel := context.WithTimeout(context.Background(), testTimeout) defer cancel() - defer ResetTestDB(testDB, t) - db := testDB.Underlying() if _, err := db.Exec(ctx, `CREATE TABLE IF NOT EXISTS valid_unicode (contents text)`); err != nil { @@ -556,11 +576,13 @@ func TestMakeValidUnicode(t *testing.T) { } func TestLock(t *testing.T) { + t.Parallel() // Verify that two transactions cannot both hold the same lock, but that every one // that wants the lock eventually gets it. + testDB, release := acquire(t) + defer release() ctx, cancel := context.WithTimeout(context.Background(), testTimeout) defer cancel() - defer ResetTestDB(testDB, t) db := testDB.Underlying() |
