github.com/mysteriumnetwork/node@v0.0.0-20240516044423-365054f76801/identity/resident_country_test.go (about)

     1  /*
     2   * Copyright (C) 2021 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 identity
    19  
    20  import (
    21  	"os"
    22  	"sync"
    23  	"testing"
    24  	"time"
    25  
    26  	"github.com/mysteriumnetwork/node/config"
    27  
    28  	"github.com/mysteriumnetwork/node/core/location/locationstate"
    29  
    30  	"github.com/mysteriumnetwork/node/eventbus"
    31  	"github.com/stretchr/testify/assert"
    32  )
    33  
    34  func TestResidentEvent(t *testing.T) {
    35  	// given
    36  	configFileName := NewTempFileName(t)
    37  	defer os.Remove(configFileName)
    38  	err := config.Current.LoadUserConfig(configFileName)
    39  
    40  	bus := eventbus.New()
    41  	residentCountry := NewResidentCountry(bus, newMockLocationResolver("UK"))
    42  
    43  	atomic := &AtomicResidentCountryEvent{}
    44  	err = bus.Subscribe(AppTopicResidentCountry, func(e ResidentCountryEvent) {
    45  		atomic.Store(e)
    46  	})
    47  	assert.NoError(t, err)
    48  
    49  	//when
    50  	err = residentCountry.Save("0x123", "LT")
    51  
    52  	//then
    53  	assert.NoError(t, err)
    54  	eventually(t, func() bool {
    55  		return atomic.Load().ID == "0x123"
    56  	})
    57  	eventually(t, func() bool {
    58  		return atomic.Load().Country == "LT"
    59  	})
    60  }
    61  
    62  func NewTempFileName(t *testing.T) string {
    63  	file, err := os.CreateTemp("", "*")
    64  	assert.NoError(t, err)
    65  	return file.Name()
    66  }
    67  
    68  type mockLocationResolver struct{ country string }
    69  
    70  func newMockLocationResolver(country string) locationProvider {
    71  	return &mockLocationResolver{country: country}
    72  }
    73  
    74  func (mlr *mockLocationResolver) GetOrigin() locationstate.Location {
    75  	return locationstate.Location{Country: "LT"}
    76  }
    77  
    78  type AtomicResidentCountryEvent struct {
    79  	lock  sync.Mutex
    80  	value ResidentCountryEvent
    81  }
    82  
    83  func (a *AtomicResidentCountryEvent) Store(value ResidentCountryEvent) {
    84  	a.lock.Lock()
    85  	defer a.lock.Unlock()
    86  	a.value = value
    87  }
    88  
    89  func (a *AtomicResidentCountryEvent) Load() ResidentCountryEvent {
    90  	a.lock.Lock()
    91  	defer a.lock.Unlock()
    92  	return a.value
    93  }
    94  
    95  func eventually(t *testing.T, condition func() bool) {
    96  	assert.Eventually(t, condition, 5*time.Second, 500*time.Millisecond)
    97  }