github.com/mysteriumnetwork/node@v0.0.0-20240516044423-365054f76801/session/time_tracker_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 session 19 20 import ( 21 "testing" 22 "time" 23 24 "github.com/mysteriumnetwork/node/session/mbtime" 25 "github.com/stretchr/testify/assert" 26 ) 27 28 func Test_NotStartedTrackerElapsedReturnsZeroValue(t *testing.T) { 29 tt := NewTracker(func() mbtime.Time { return mbtime.New(10, 10) }) 30 31 assert.Equal(t, 0*time.Second, tt.Elapsed()) 32 } 33 34 func Test_ElapsedReturnsCorrectValue(t *testing.T) { 35 mockedClock := newMockedTime( 36 []mbtime.Time{ 37 mbtime.New(1, 0), 38 mbtime.New(4, 0), 39 }, 40 ) 41 tt := NewTracker(mockedClock) 42 43 tt.StartTracking() 44 elapsed := tt.Elapsed() 45 46 assert.Equal(t, 3*time.Second, elapsed) 47 } 48 49 func newMockedTime(timeValues []mbtime.Time) func() mbtime.Time { 50 count := 0 51 52 return func() mbtime.Time { 53 val := timeValues[count] 54 count = count + 1 55 return val 56 } 57 }