From ccc77a49bf16eb5d42193904d3aefaf5a52ec6fe Mon Sep 17 00:00:00 2001 From: Anton Litvinov Date: Fri, 22 Mar 2024 16:14:58 +0400 Subject: [PATCH] Test service Proposal on a data race Signed-off-by: Anton Litvinov --- core/service/pool_test.go | 43 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) diff --git a/core/service/pool_test.go b/core/service/pool_test.go index 93667792fd..27d435d5b6 100644 --- a/core/service/pool_test.go +++ b/core/service/pool_test.go @@ -22,8 +22,11 @@ import ( "errors" "net" "sync" + "sync/atomic" "testing" + "time" + "github.com/mysteriumnetwork/node/market" "github.com/mysteriumnetwork/node/mocks" "github.com/stretchr/testify/assert" ) @@ -71,6 +74,46 @@ func Test_Pool_Add(t *testing.T) { assert.Len(t, pool.instances, 1) } +func Test_Pool_DataRace(t *testing.T) { + service := &Instance{ + service: &mockService{}, + eventPublisher: mocks.NewEventBus(), + location: mockLocationResolver{}, + } + + active := new(atomic.Bool) + active.Store(true) + + var wg sync.WaitGroup + + wg.Add(2) + go func() { + var location market.Location + + defer wg.Done() + for i := 0; i < 100; i++ { + p := service.proposalWithCurrentLocation() + location = p.Location + + time.Sleep(1 * time.Millisecond) + } + active.Store(false) + _ = location + }() + go func() { + var proposal market.ServiceProposal + + defer wg.Done() + for active.Load() == true { + proposal = service.CopyProposal() + + time.Sleep(1 * time.Millisecond) + } + _ = proposal + }() + wg.Wait() +} + func Test_Pool_StopAllSuccess(t *testing.T) { instance := &Instance{ service: &mockService{},