Skip to content

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

test: add retries and sleep for test (#16883)

rebelice77fea6cd7518
diff --git a/backend/plugin/db/mssql/sync_spatial_testcontainer_test.go b/backend/plugin/db/mssql/sync_spatial_testcontainer_test.go
index fae05dfbe4..c2a253a5f6 100644
--- a/backend/plugin/db/mssql/sync_spatial_testcontainer_test.go
+++ b/backend/plugin/db/mssql/sync_spatial_testcontainer_test.go
@@ -9,6 +9,7 @@ import (
"time"
_ "github.com/microsoft/go-mssqldb"
+ "github.com/pkg/errors"
"github.com/stretchr/testify/require"
"github.com/bytebase/bytebase/backend/common/testcontainer"
@@ -16,6 +17,31 @@ import (
"github.com/bytebase/bytebase/backend/plugin/db"
)
+// syncDBSchemaWithRetry attempts to sync the database schema with retry logic for transient errors
+func syncDBSchemaWithRetry(ctx context.Context, driver *Driver, maxRetries int) (*storepb.DatabaseSchemaMetadata, error) {
+ var lastErr error
+ for i := 0; i < maxRetries; i++ {
+ metadata, err := driver.SyncDBSchema(ctx)
+ if err == nil {
+ return metadata, nil
+ }
+
+ // Check if it's a connection error (EOF or timeout)
+ if strings.Contains(err.Error(), "EOF") || strings.Contains(err.Error(), "i/o timeout") {
+ lastErr = err
+ if i < maxRetries-1 {
+ // Wait before retry with exponential backoff
+ waitTime := time.Duration(i+1) * 500 * time.Millisecond
+ time.Sleep(waitTime)
+ continue
+ }
+ }
+ // For non-transient errors, return immediately
+ return nil, err
+ }
+ return nil, errors.Wrapf(lastErr, "failed after %d retries", maxRetries)
+}
+
func TestSyncSpatialIndexWithTestcontainer(t *testing.T) {
ctx := context.Background()
container := testcontainer.GetTestMSSQLContainer(ctx, t)
@@ -600,8 +626,8 @@ WITH (
GO
`,
validate: func(t *testing.T, driver *Driver, _ string) {
- // Sync database schema
- metadata, err := driver.SyncDBSchema(ctx)
+ // Sync database schema with retry logic for transient errors
+ metadata, err := syncDBSchemaWithRetry(ctx, driver, 3)
require.NoError(t, err)
require.NotNil(t, metadata)
@@ -765,6 +791,11 @@ GO
mssqlDriver, ok := driver.(*Driver)
require.True(t, ok)
+ // For the problematic test case, add a delay to allow metadata propagation
+ if tc.name == "sync_spatial_indexes_with_regular_indexes" {
+ time.Sleep(1 * time.Second)
+ }
+
// Run validation
tc.validate(t, mssqlDriver, databaseName)
})