github.com/mysteriumnetwork/node@v0.0.0-20240516044423-365054f76801/core/service/pool_test.go (about) 1 /* 2 * Copyright (C) 2019 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 "encoding/json" 22 "errors" 23 "net" 24 "sync" 25 "sync/atomic" 26 "testing" 27 "time" 28 29 "github.com/mysteriumnetwork/node/market" 30 "github.com/mysteriumnetwork/node/mocks" 31 "github.com/stretchr/testify/assert" 32 ) 33 34 type mockService struct { 35 killErr error 36 } 37 38 type mockPublisher struct { 39 publishedTopic string 40 publishedData []interface{} 41 lock sync.Mutex 42 } 43 44 func (mockPublisher *mockPublisher) Publish(topic string, data interface{}) { 45 mockPublisher.lock.Lock() 46 defer mockPublisher.lock.Unlock() 47 mockPublisher.publishedTopic = topic 48 mockPublisher.publishedData = append(mockPublisher.publishedData, data) 49 } 50 51 func (mr *mockService) Serve(_ *Instance) error { 52 return nil 53 } 54 55 func (mr *mockService) Stop() error { 56 return mr.killErr 57 } 58 59 func (mr *mockService) ProvideConfig(_ string, _ json.RawMessage, _ *net.UDPConn) (*ConfigParams, error) { 60 return &ConfigParams{}, nil 61 } 62 63 func Test_Pool_NewPool(t *testing.T) { 64 pool := NewPool(mocks.NewEventBus()) 65 assert.Len(t, pool.instances, 0) 66 } 67 68 func Test_Pool_Add(t *testing.T) { 69 instance := &Instance{} 70 71 pool := NewPool(mocks.NewEventBus()) 72 pool.Add(instance) 73 74 assert.Len(t, pool.instances, 1) 75 } 76 77 func Test_Pool_DataRace(t *testing.T) { 78 service := &Instance{ 79 service: &mockService{}, 80 eventPublisher: mocks.NewEventBus(), 81 location: mockLocationResolver{}, 82 } 83 84 active := new(atomic.Bool) 85 active.Store(true) 86 87 var wg sync.WaitGroup 88 89 wg.Add(2) 90 go func() { 91 var location market.Location 92 93 defer wg.Done() 94 for i := 0; i < 100; i++ { 95 p := service.proposalWithCurrentLocation() 96 location = p.Location 97 98 time.Sleep(1 * time.Millisecond) 99 } 100 active.Store(false) 101 _ = location 102 }() 103 go func() { 104 var proposal market.ServiceProposal 105 106 defer wg.Done() 107 for active.Load() == true { 108 proposal = service.CopyProposal() 109 110 time.Sleep(1 * time.Millisecond) 111 } 112 _ = proposal 113 }() 114 wg.Wait() 115 } 116 117 func Test_Pool_StopAllSuccess(t *testing.T) { 118 instance := &Instance{ 119 service: &mockService{}, 120 eventPublisher: mocks.NewEventBus(), 121 } 122 123 pool := NewPool(mocks.NewEventBus()) 124 pool.Add(instance) 125 126 err := pool.StopAll() 127 assert.NoError(t, err) 128 } 129 130 func Test_Pool_StopDoesNotStop(t *testing.T) { 131 service := &mockService{killErr: errors.New("I dont want to stop")} 132 instance := &Instance{ID: "test id", service: service, eventPublisher: mocks.NewEventBus()} 133 134 pool := NewPool(mocks.NewEventBus()) 135 pool.Add(instance) 136 137 err := pool.Stop("test id") 138 assert.EqualError(t, err, "ErrorCollection(I dont want to stop)") 139 } 140 141 func Test_Pool_StopReturnsErrIfInstanceDoesNotExist(t *testing.T) { 142 pool := NewPool(mocks.NewEventBus()) 143 err := pool.Stop("something") 144 assert.Equal(t, ErrNoSuchInstance, err) 145 } 146 147 func Test_Pool_StopAllDoesNotStopOneInstance(t *testing.T) { 148 service := &mockService{killErr: errors.New("I dont want to stop")} 149 instance := &Instance{ID: "test id", service: service, eventPublisher: mocks.NewEventBus()} 150 151 pool := NewPool(mocks.NewEventBus()) 152 pool.Add(instance) 153 154 err := pool.StopAll() 155 assert.EqualError(t, err, "Some instances did not stop: ErrorCollection(I dont want to stop)") 156 }