github.com/mysteriumnetwork/node@v0.0.0-20240516044423-365054f76801/core/location/cache_test.go (about) 1 /* 2 * Copyright (C) 2017 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 location 19 20 import ( 21 "testing" 22 "time" 23 24 "github.com/stretchr/testify/assert" 25 26 "github.com/mysteriumnetwork/node/core/connection/connectionstate" 27 "github.com/mysteriumnetwork/node/core/location/locationstate" 28 ) 29 30 func TestCache_needsRefresh(t *testing.T) { 31 type fields struct { 32 lastFetched time.Time 33 expiry time.Duration 34 } 35 tests := []struct { 36 name string 37 fields fields 38 want bool 39 }{ 40 { 41 name: "returns true on zero time", 42 want: true, 43 fields: fields{}, 44 }, 45 { 46 name: "returns true if expired", 47 want: true, 48 fields: fields{ 49 lastFetched: time.Now().Add(time.Second * -69), 50 expiry: time.Minute * 1, 51 }, 52 }, 53 { 54 name: "returns false if updated recently", 55 want: false, 56 fields: fields{ 57 lastFetched: time.Now().Add(time.Second * -50), 58 expiry: time.Minute * 1, 59 }, 60 }, 61 } 62 for _, tt := range tests { 63 t.Run(tt.name, func(t *testing.T) { 64 c := &Cache{ 65 lastFetched: tt.fields.lastFetched, 66 expiry: tt.fields.expiry, 67 pub: mockPublisher{}, 68 } 69 if got := c.needsRefresh(); got != tt.want { 70 t.Errorf("Cache.needsRefresh() = %v, want %v", got, tt.want) 71 } 72 }) 73 } 74 } 75 76 type mockResolver struct { 77 called bool 78 errToReturn error 79 } 80 81 func (mr *mockResolver) DetectLocation() (locationstate.Location, error) { 82 mr.called = true 83 return locationstate.Location{}, mr.errToReturn 84 } 85 86 func (mr *mockResolver) DetectProxyLocation(_ int) (locationstate.Location, error) { 87 return mr.DetectLocation() 88 } 89 90 type mockPublisher struct{} 91 92 func (mp mockPublisher) Publish(topic string, data interface{}) {} 93 94 func TestCacheHandlesConnection_Connected(t *testing.T) { 95 r := &mockResolver{} 96 c := &Cache{ 97 expiry: time.Second * 1, 98 locationDetector: r, 99 pub: mockPublisher{}, 100 } 101 c.HandleConnectionEvent(connectionstate.AppEventConnectionState{State: connectionstate.Connected}) 102 assert.True(t, r.called) 103 } 104 105 func TestCacheHandlesConnection_NotConnected(t *testing.T) { 106 r := &mockResolver{} 107 c := &Cache{ 108 expiry: time.Second * 1, 109 locationDetector: r, 110 pub: mockPublisher{}, 111 } 112 c.HandleConnectionEvent(connectionstate.AppEventConnectionState{State: connectionstate.NotConnected}) 113 assert.True(t, r.called) 114 } 115 116 func TestCacheIgnoresOther(t *testing.T) { 117 r := &mockResolver{} 118 c := &Cache{ 119 expiry: time.Second * 1, 120 locationDetector: r, 121 pub: mockPublisher{}, 122 } 123 c.HandleConnectionEvent(connectionstate.AppEventConnectionState{State: connectionstate.Reconnecting}) 124 assert.False(t, r.called) 125 }