github.com/m3db/m3@v1.5.1-0.20231129193456-75a402aa583b/src/x/clock/offset_clock_test.go (about) 1 // Copyright (c) 2021 Uber Technologies, Inc. 2 // 3 // Permission is hereby granted, free of charge, to any person obtaining a copy 4 // of this software and associated documentation files (the "Software"), to deal 5 // in the Software without restriction, including without limitation the rights 6 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 7 // copies of the Software, and to permit persons to whom the Software is 8 // furnished to do so, subject to the following conditions: 9 // 10 // The above copyright notice and this permission notice shall be included in 11 // all copies or substantial portions of the Software. 12 // 13 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 18 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 19 // THE SOFTWARE. 20 21 package clock 22 23 import ( 24 "testing" 25 "time" 26 27 "github.com/stretchr/testify/assert" 28 ) 29 30 func TestOffsetClockNow(t *testing.T) { 31 OneYearFromNow := time.Now().Add(365 * 24 * time.Hour).Truncate(time.Nanosecond) 32 33 initialSeedTime := time.Now() 34 35 advanceByOneSec := func() time.Time { 36 initialSeedTime = initialSeedTime.Add(1 * time.Second) 37 return initialSeedTime 38 } 39 40 tests := []struct { 41 name string 42 offsetTime time.Time 43 expected []time.Time 44 }{ 45 { 46 name: "past", 47 offsetTime: time.Unix(1614245284, 0), 48 expected: []time.Time{ 49 time.Unix(1614245285, 0), 50 time.Unix(1614245286, 0), 51 time.Unix(1614245287, 0), 52 }, 53 }, 54 { 55 name: "initial unix time", 56 offsetTime: time.Unix(0, 0), 57 expected: []time.Time{ 58 time.Unix(1, 0), 59 time.Unix(2, 0), 60 time.Unix(3, 0), 61 }, 62 }, 63 { 64 name: "future", 65 offsetTime: OneYearFromNow, 66 expected: []time.Time{ 67 OneYearFromNow.Add(1 * time.Second), 68 OneYearFromNow.Add(2 * time.Second), 69 OneYearFromNow.Add(3 * time.Second), 70 }, 71 }, 72 } 73 for _, tt := range tests { 74 t.Run(tt.name, func(t *testing.T) { 75 sut := NewOffsetClock(tt.offsetTime, advanceByOneSec) 76 for _, expected := range tt.expected { 77 actual := sut.Now() 78 assert.Equal(t, expected.UnixNano(), actual.UnixNano()) 79 } 80 }) 81 } 82 } 83 84 func TestNewOffsetClockFromTimeDelta(t *testing.T) { 85 nowFn := func() time.Time { 86 return time.Date(2021, 1, 1, 0, 0, 0, 0, time.Local) 87 } 88 89 tests := []struct { 90 name string 91 timeDelta time.Duration 92 expected time.Time 93 }{ 94 { 95 name: "positive offset", 96 timeDelta: time.Hour, 97 expected: time.Date(2021, 1, 1, 1, 0, 0, 0, time.Local), 98 }, 99 { 100 name: "negative offset", 101 timeDelta: -24 * time.Hour, 102 expected: time.Date(2020, 12, 31, 0, 0, 0, 0, time.Local), 103 }, 104 } 105 106 for _, tt := range tests { 107 t.Run(tt.name, func(t *testing.T) { 108 assert.Equal(t, tt.expected, NewOffsetClockFromTimeDelta(tt.timeDelta, nowFn).Now()) 109 }) 110 } 111 }