Skip to content

Commit

Permalink
Test service Proposal for a data race #2
Browse files Browse the repository at this point in the history
Signed-off-by: Anton Litvinov <[email protected]>
  • Loading branch information
Zensey committed Mar 27, 2024
1 parent c5f45bf commit 15770e0
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 0 deletions.
4 changes: 4 additions & 0 deletions core/service/pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,10 @@ func (i *Instance) CopyProposal() market.ServiceProposal {
if err := copier.CopyWithOption(&proposal, i.Proposal, copier.Option{DeepCopy: true}); err != nil {
panic(err)
}
// workaround b/c of copier bug: it make empty slice instead of nil
if i.Proposal.Contacts == nil {
proposal.Contacts = nil
}

return proposal
}
65 changes: 65 additions & 0 deletions core/service/session_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/*
* Copyright (C) 2024 The "MysteriumNetwork/node" Authors.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

package service

import (
"sync"
"sync/atomic"
"testing"
"time"

"github.com/mysteriumnetwork/node/pb"
"github.com/mysteriumnetwork/node/trace"
)

func TestSession_DataRace(t *testing.T) {

service := &Instance{
location: mockLocationResolver{},
}

active := new(atomic.Bool)
active.Store(true)

var wg sync.WaitGroup
wg.Add(2)

go func() {
defer wg.Done()

for i := 0; i < 100; i++ {
session, _ := NewSession(service, &pb.SessionRequest{}, trace.NewTracer(""))
_ = session

time.Sleep(time.Millisecond)
}
active.Store(false)
}()

go func() {
defer wg.Done()

for active.Load() == true {
service.proposalWithCurrentLocation()

time.Sleep(time.Millisecond)
}
}()
wg.Wait()

}

0 comments on commit 15770e0

Please sign in to comment.