From d9aca9f862baa204baf6b1f3dd59facb2137a8fd Mon Sep 17 00:00:00 2001 From: Anton Litvinov Date: Tue, 2 Apr 2024 16:20:38 +0400 Subject: [PATCH] Halt tests on data race Signed-off-by: Anton Litvinov --- ci/test/test.go | 14 +++-- .../pingpong/consumer_balance_tracker_test.go | 55 ++++++++++--------- 2 files changed, 39 insertions(+), 30 deletions(-) diff --git a/ci/test/test.go b/ci/test/test.go index 4e3e2366d5..fc4945d03c 100644 --- a/ci/test/test.go +++ b/ci/test/test.go @@ -29,8 +29,11 @@ func TestWithCoverage() error { if err != nil { return err } - args := append([]string{"test", "-race", "-timeout", "5m", "-cover", "-coverprofile", "coverage.txt", "-covermode", "atomic"}, packages...) - return sh.RunV("go", args...) + args := append([]string{"test", "-race", "-count=1", "-timeout", "5m", "-cover", "-coverprofile", "coverage.txt", "-covermode", "atomic"}, packages...) + + env := make(map[string]string) + env["GORACE"] = "halt_on_error=1" + return sh.RunWith(env, "go", args...) } // Test runs unit tests @@ -39,8 +42,11 @@ func Test() error { if err != nil { return err } - args := append([]string{"test", "-race", "-count=1", "-timeout", "5m"}, packages...) - return sh.RunV("go", args...) + args := append([]string{"test", "-race", "-timeout", "5m"}, packages...) + + env := make(map[string]string) + env["GORACE"] = "halt_on_error=1" + return sh.RunWith(env, "go", args...) } func unitTestPackages() ([]string, error) { diff --git a/session/pingpong/consumer_balance_tracker_test.go b/session/pingpong/consumer_balance_tracker_test.go index dc143e1df8..7d5f368aba 100644 --- a/session/pingpong/consumer_balance_tracker_test.go +++ b/session/pingpong/consumer_balance_tracker_test.go @@ -364,34 +364,37 @@ func TestConsumerBalanceTracker_InprogressUnregisteredBalanceReturnedWhenNoBount } func TestConsumerBalanceTracker_RecoverGrandTotalPromisedSettledIsBiggerThanPromissedNotOffChain(t *testing.T) { - id1 := identity.FromAddress("0x000000001") - grandTotalPromised := big.NewInt(10) - settledAmount := big.NewInt(11) - bus := eventbus.New() - mcts := NewConsumerTotalsStorage(bus) - bc := mockConsumerBalanceChecker{} - cfg := defaultCfg - cfg.LongSync.Interval = time.Millisecond * 300 - calc := newMockAddressProvider() - calc.addrToReturn = id1.ToCommonAddress() - mockBlockchainProvider := &mockBlockchainInfoProvider{} - - mockBlockchainProvider.AddConsumerChannelsHermes(1, id1.ToCommonAddress(), client.ConsumersHermes{ - Settled: big.NewInt(6), - }) + // make data race more likely to happen + for i := 0; i < 10; i++ { + id1 := identity.FromAddress("0x000000001") + grandTotalPromised := big.NewInt(10) + settledAmount := big.NewInt(11) + bus := eventbus.New() + mcts := NewConsumerTotalsStorage(bus) + bc := mockConsumerBalanceChecker{} + cfg := defaultCfg + cfg.LongSync.Interval = time.Millisecond * 300 + calc := newMockAddressProvider() + calc.addrToReturn = id1.ToCommonAddress() + mockBlockchainProvider := &mockBlockchainInfoProvider{} + + mockBlockchainProvider.AddConsumerChannelsHermes(1, id1.ToCommonAddress(), client.ConsumersHermes{ + Settled: big.NewInt(6), + }) - cbt := NewConsumerBalanceTracker(bus, &bc, mcts, &mockconsumerInfoGetter{grandTotalPromised, settledAmount}, &mockTransactor{}, &mockRegistrationStatusProvider{}, calc, mockBlockchainProvider, defaultCfg) + cbt := NewConsumerBalanceTracker(bus, &bc, mcts, &mockconsumerInfoGetter{grandTotalPromised, settledAmount}, &mockTransactor{}, &mockRegistrationStatusProvider{}, calc, mockBlockchainProvider, defaultCfg) - err := cbt.Subscribe(bus) - assert.NoError(t, err) - bus.Publish(identity.AppTopicIdentityUnlock, identity.AppEventIdentityUnlock{ - ChainID: 1, - ID: id1, - }) - assert.Eventually(t, func() bool { - savedBalance, _ := mcts.Get(1, id1, common.BigToAddress(big.NewInt(0))) - return savedBalance.Cmp(big.NewInt(6)) == 0 - }, defaultWaitTime, defaultWaitInterval) + err := cbt.Subscribe(bus) + assert.NoError(t, err) + bus.Publish(identity.AppTopicIdentityUnlock, identity.AppEventIdentityUnlock{ + ChainID: 1, + ID: id1, + }) + assert.Eventually(t, func() bool { + savedBalance, _ := mcts.Get(1, id1, common.BigToAddress(big.NewInt(0))) + return savedBalance.Cmp(big.NewInt(6)) == 0 + }, defaultWaitTime, defaultWaitInterval) + } } type mockConsumerBalanceChecker struct {