diff options
author | Pavlo Strokov <pstrokov@gitlab.com> | 2021-09-21 12:48:38 +0300 |
---|---|---|
committer | Patrick Steinhardt <psteinhardt@gitlab.com> | 2021-09-21 13:29:44 +0300 |
commit | 80312c4166c2f65b8f3e357b6ecf131023f8d324 (patch) | |
tree | e8a413a45b7542126a49b73059790da62e101cdf | |
parent | 858ab5adcc2996f16e958160f3f31fe519300bc8 (diff) |
test: Fix data race on the votes counter
The local variable votes used inside the transaction server
and in the test to verify amount of votes executed.
As code is executed in different goroutines we need to
protect access to the shared variable or use atomic instructions
to prevent data races.
-rw-r--r-- | internal/gitaly/service/repository/replicate_test.go | 15 |
1 files changed, 8 insertions, 7 deletions
diff --git a/internal/gitaly/service/repository/replicate_test.go b/internal/gitaly/service/repository/replicate_test.go index 5c07ccb11..9ec61609a 100644 --- a/internal/gitaly/service/repository/replicate_test.go +++ b/internal/gitaly/service/repository/replicate_test.go @@ -5,6 +5,7 @@ import ( "context" "os" "path/filepath" + "sync/atomic" "testing" "github.com/stretchr/testify/require" @@ -118,10 +119,10 @@ func testReplicateRepositoryTransactional(t *testing.T, ctx context.Context) { targetRepo := proto.Clone(sourceRepo).(*gitalypb.Repository) targetRepo.StorageName = cfg.Storages[1].Name - votes := 0 + votes := int32(0) txServer := testTransactionServer{ vote: func(request *gitalypb.VoteTransactionRequest) (*gitalypb.VoteTransactionResponse, error) { - votes++ + atomic.AddInt32(&votes, 1) return &gitalypb.VoteTransactionResponse{ State: gitalypb.VoteTransactionResponse_COMMIT, }, nil @@ -151,16 +152,16 @@ func testReplicateRepositoryTransactional(t *testing.T, ctx context.Context) { require.NoError(t, err) if featureflag.TxExtendedFileLocking.IsEnabled(ctx) { - require.Equal(t, 5, votes) + require.EqualValues(t, 5, atomic.LoadInt32(&votes)) } else { - require.Equal(t, 1, votes) + require.EqualValues(t, 1, atomic.LoadInt32(&votes)) } // We're now changing a reference in the source repository such that we can observe changes // in the target repo. gittest.Exec(t, cfg, "-C", sourceRepoPath, "update-ref", "refs/heads/master", "refs/heads/master~") - votes = 0 + atomic.StoreInt32(&votes, 0) // And the second invocation uses FetchInternalRemote. _, err = client.ReplicateRepository(ctx, &gitalypb.ReplicateRepositoryRequest{ @@ -170,9 +171,9 @@ func testReplicateRepositoryTransactional(t *testing.T, ctx context.Context) { require.NoError(t, err) if featureflag.TxExtendedFileLocking.IsEnabled(ctx) { - require.Equal(t, 6, votes) + require.EqualValues(t, 6, atomic.LoadInt32(&votes)) } else { - require.Equal(t, 2, votes) + require.EqualValues(t, 2, atomic.LoadInt32(&votes)) } } |