github.com/mysteriumnetwork/node@v0.0.0-20240516044423-365054f76801/core/service/session_test.go (about)

     1  /*
     2   * Copyright (C) 2024 The "MysteriumNetwork/node" Authors.
     3   *
     4   * This program is free software: you can redistribute it and/or modify
     5   * it under the terms of the GNU General Public License as published by
     6   * the free Software Foundation, either version 3 of the License, or
     7   * (at your option) any later version.
     8   *
     9   * This program is distributed in the hope that it will be useful,
    10   * but WITHOUT ANY WARRANTY; without even the implied warranty of
    11   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    12   * GNU General Public License for more details.
    13   *
    14   * You should have received a copy of the GNU General Public License
    15   * along with this program.  If not, see <http://www.gnu.org/licenses/>.
    16   */
    17  
    18  package service
    19  
    20  import (
    21  	"sync"
    22  	"sync/atomic"
    23  	"testing"
    24  	"time"
    25  
    26  	"github.com/mysteriumnetwork/node/pb"
    27  	"github.com/mysteriumnetwork/node/trace"
    28  )
    29  
    30  func TestSession_DataRace(t *testing.T) {
    31  
    32  	service := &Instance{
    33  		location: mockLocationResolver{},
    34  	}
    35  
    36  	active := new(atomic.Bool)
    37  	active.Store(true)
    38  
    39  	var wg sync.WaitGroup
    40  	wg.Add(2)
    41  
    42  	go func() {
    43  		defer wg.Done()
    44  
    45  		for i := 0; i < 100; i++ {
    46  			session, _ := NewSession(service, &pb.SessionRequest{}, trace.NewTracer(""))
    47  			_ = session
    48  
    49  			time.Sleep(time.Millisecond)
    50  		}
    51  		active.Store(false)
    52  	}()
    53  
    54  	go func() {
    55  		defer wg.Done()
    56  
    57  		for active.Load() == true {
    58  			service.proposalWithCurrentLocation()
    59  
    60  			time.Sleep(time.Millisecond)
    61  		}
    62  	}()
    63  	wg.Wait()
    64  
    65  }