From 1020ba095847e5090e90d5eba77966d6fcade7f3 Mon Sep 17 00:00:00 2001 From: Vladislav Sukhin Date: Wed, 24 Jul 2024 14:53:27 +0300 Subject: [PATCH] fix: parallel integration test Signed-off-by: Vladislav Sukhin --- .../sequence/mongo_integration_test.go | 76 +++++++++++++++++++ 1 file changed, 76 insertions(+) diff --git a/pkg/repository/sequence/mongo_integration_test.go b/pkg/repository/sequence/mongo_integration_test.go index 23a11d64345..03196cb657d 100644 --- a/pkg/repository/sequence/mongo_integration_test.go +++ b/pkg/repository/sequence/mongo_integration_test.go @@ -2,6 +2,8 @@ package sequence import ( "context" + "fmt" + "sync" "testing" "github.com/kubeshop/testkube/internal/config" @@ -68,3 +70,77 @@ func TestNewMongoRepository_GetNextExecutionNumber_Sequential_Integration(t *tes assert.Equal(t, tt.expectedValue, num) } } + +func TestNewMongoRepository_GetNextExecutionNumber_Parallel_Integration(t *testing.T) { + test.IntegrationTest(t) + + ctx := context.Background() + + client, err := mongo.Connect(ctx, options.Client().ApplyURI(cfg.APIMongoDSN)) + if err != nil { + t.Fatalf("error connecting to mongo: %v", err) + } + db := client.Database("sequence-mongo-repository-test") + t.Cleanup(func() { + db.Drop(ctx) + }) + + repo := NewMongoRepository(db) + + var tests = []struct { + expectedValue int32 + executionType ExecutionType + }{ + { + 1, + ExecutionTypeTest, + }, + { + 2, + ExecutionTypeTest, + }, + { + 1, + ExecutionTypeTestSuite, + }, + { + 2, + ExecutionTypeTestSuite, + }, + { + 1, + ExecutionTypeTestWorkflow, + }, + { + 2, + ExecutionTypeTestWorkflow, + }, + } + + var results sync.Map + var wg sync.WaitGroup + + for i := range tests { + wg.Add(1) + go func(executionType ExecutionType) { + defer wg.Done() + + num, err := repo.GetNextExecutionNumber(ctx, "name", executionType) + assert.NoError(t, err) + + results.Store(fmt.Sprintf("%s_%d", executionType, num), num) + }(tests[i].executionType) + } + + wg.Wait() + + for _, tt := range tests { + num, ok := results.Load(fmt.Sprintf("%s_%d", tt.executionType, tt.expectedValue)) + assert.Equal(t, true, ok) + + value, ok := num.(int32) + assert.Equal(t, true, ok) + + assert.Subset(t, []int32{1, 2}, []int32{value}) + } +}