Skip to content

Database governance built for humans and agents — controlling changes and access across every major database.

test(mssql): parallelize testcontainer tests (#18877)

Danny Xu670999107447
diff --git a/backend/plugin/schema/mssql/generate_migration_testcontainer_test.go b/backend/plugin/schema/mssql/generate_migration_testcontainer_test.go
index 8405802187..76cb9e2d87 100644
--- a/backend/plugin/schema/mssql/generate_migration_testcontainer_test.go
+++ b/backend/plugin/schema/mssql/generate_migration_testcontainer_test.go
@@ -27,6 +27,8 @@ import (
// TestGenerateMigrationWithTestcontainer tests the generate migration function
// by applying migrations and rollback to verify the schema can be restored.
+//
+//nolint:tparallel
func TestGenerateMigrationWithTestcontainer(t *testing.T) {
if testing.Short() {
t.Skip("Skipping MSSQL testcontainer test in short mode")
@@ -52,11 +54,11 @@ func TestGenerateMigrationWithTestcontainer(t *testing.T) {
Started: true,
})
require.NoError(t, err)
- defer func() {
+ t.Cleanup(func() {
if err := container.Terminate(ctx); err != nil {
t.Logf("failed to terminate container: %s", err)
}
- }()
+ })
// Get connection details
host, err := container.Host(ctx)
@@ -2375,11 +2377,13 @@ GO
}
for _, testCase := range testCases {
+ testCase := testCase // Capture range variable
t.Run(testCase.name, func(t *testing.T) {
+ t.Parallel()
// Step 1: Execute 5-step workflow
portInt, err := strconv.Atoi(port.Port())
require.NoError(t, err)
- err = executeFiveStepWorkflow(ctx, host, portInt, testCase.initialSchema, testCase.migrationDDL)
+ err = executeFiveStepWorkflow(ctx, host, portInt, testCase.name, testCase.initialSchema, testCase.migrationDDL)
require.NoError(t, err, "Failed 5-step workflow for test case: %s", testCase.description)
})
}
@@ -2391,7 +2395,7 @@ GO
// 3. Generate rollback DDL via generate_migration
// 4. Execute rollback DDL, get schema result C via syncDBSchema
// 5. Compare schema results A and C to verify they are identical
-func executeFiveStepWorkflow(ctx context.Context, host string, port int, initialSchema, migrationDDL string) error {
+func executeFiveStepWorkflow(ctx context.Context, host string, port int, testName, initialSchema, migrationDDL string) error {
// Create driver instance
driverInstance := &mssqldb.Driver{}
@@ -2417,8 +2421,8 @@ func executeFiveStepWorkflow(ctx context.Context, host string, port int, initial
}
defer driver.Close(ctx)
- // Create test database with unique name
- testDB := fmt.Sprintf("test_db_%d_%d", time.Now().Unix(), time.Now().UnixNano()%1000000)
+ // Create test database with unique name based on test case
+ testDB := fmt.Sprintf("test_%s_%d", strings.ReplaceAll(strings.ReplaceAll(testName, " ", "_"), "-", "_"), time.Now().UnixNano()%1000000)
if _, err := driver.Execute(ctx, fmt.Sprintf("CREATE DATABASE [%s]", testDB), db.ExecuteOptions{CreateDatabase: true}); err != nil {
return errors.Wrap(err, "failed to create test database")
}
diff --git a/backend/plugin/schema/mssql/get_database_definition_testcontainer_test.go b/backend/plugin/schema/mssql/get_database_definition_testcontainer_test.go
index a257515cf2..4f361a72c2 100644
--- a/backend/plugin/schema/mssql/get_database_definition_testcontainer_test.go
+++ b/backend/plugin/schema/mssql/get_database_definition_testcontainer_test.go
@@ -6,7 +6,6 @@ import (
"strconv"
"strings"
"testing"
- "time"
_ "github.com/microsoft/go-mssqldb"
"github.com/pkg/errors"
@@ -19,10 +18,11 @@ import (
"github.com/bytebase/bytebase/backend/plugin/schema"
)
+//nolint:tparallel
func TestGetDatabaseDefinitionWithTestcontainer(t *testing.T) {
ctx := context.Background()
container := testcontainer.GetTestMSSQLContainer(ctx, t)
- defer container.Close(ctx)
+ t.Cleanup(func() { container.Close(ctx) })
host := container.GetHost()
port := container.GetPort()
@@ -403,8 +403,11 @@ GO
}
for _, tc := range testCases {
+ tc := tc // Capture range variable
t.Run(tc.name, func(t *testing.T) {
- databaseName := fmt.Sprintf("test_%d", time.Now().Unix())
+ t.Parallel() // Safe to parallelize - shared container, unique databases per test
+ // Use test name for database name - each test case has a unique name
+ databaseName := fmt.Sprintf("test_%s", strings.ReplaceAll(tc.name, " ", "_"))
// Create a new driver instance for this test
driverInstance := &mssqldb.Driver{}
@@ -466,7 +469,7 @@ GO
require.NotEmpty(t, definitionX)
// Step 3: Create a new database and run the definition X
- newDatabaseName := fmt.Sprintf("test_copy_%d", time.Now().Unix())
+ newDatabaseName := fmt.Sprintf("test_copy_%s", strings.ReplaceAll(tc.name, " ", "_"))
// Reconnect to master to create new database
testDriver.Close(ctx)
diff --git a/backend/plugin/schema/mssql/get_database_metadata_testcontainer_test.go b/backend/plugin/schema/mssql/get_database_metadata_testcontainer_test.go
index 968863e0c0..4458302b6f 100644
--- a/backend/plugin/schema/mssql/get_database_metadata_testcontainer_test.go
+++ b/backend/plugin/schema/mssql/get_database_metadata_testcontainer_test.go
@@ -6,7 +6,6 @@ import (
"strconv"
"strings"
"testing"
- "time"
_ "github.com/microsoft/go-mssqldb"
"github.com/stretchr/testify/require"
@@ -17,10 +16,11 @@ import (
mssqldb "github.com/bytebase/bytebase/backend/plugin/db/mssql"
)
+//nolint:tparallel
func TestGetDatabaseMetadataWithTestcontainer(t *testing.T) {
ctx := context.Background()
container := testcontainer.GetTestMSSQLContainer(ctx, t)
- defer container.Close(ctx)
+ t.Cleanup(func() { container.Close(ctx) })
host := container.GetHost()
port := container.GetPort()
@@ -652,8 +652,10 @@ GO
}
for _, tc := range testCases {
+ tc := tc // Capture range variable
t.Run(tc.name, func(t *testing.T) {
- databaseName := fmt.Sprintf("test_%d", time.Now().Unix())
+ t.Parallel() // Safe to parallelize - shared container, unique databases per test
+ databaseName := fmt.Sprintf("test_%s", strings.ReplaceAll(tc.name, " ", "_"))
// Create driver instance
driverInstance := &mssqldb.Driver{}