-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtimecache_test.go
More file actions
144 lines (117 loc) · 3.58 KB
/
Copy pathtimecache_test.go
File metadata and controls
144 lines (117 loc) · 3.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
// Copyright 2026 Byterio
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package timecache
import (
"sync"
"testing"
"time"
"github.com/stretchr/testify/require"
)
var timerTestMu sync.Mutex
func checkTimeStamp(tb testing.TB, expectedCurrent, actualCurrent uint32) {
tb.Helper()
// Test with buffer of ±2 seconds for CI environment tolerance.
require.True(tb, actualCurrent >= expectedCurrent-2 && actualCurrent <= expectedCurrent+2,
"Expected timestamp %d (±2s), got %d (diff: %d)", expectedCurrent, actualCurrent, int64(actualCurrent)-int64(expectedCurrent))
}
func Test_Updater(t *testing.T) {
timerTestMu.Lock()
defer timerTestMu.Unlock()
f := New()
f.Start()
defer f.Stop()
now := uint32(time.Now().Unix())
time.Sleep(100 * time.Millisecond)
ts := f.Timestamp()
require.True(t, ts >= now-1 && ts <= now+1,
"Initial timestamp should be within ±1 second of current time. Expected: %d±1, got: %d (diff: %d)",
now, ts, int64(ts)-int64(now))
time.Sleep(1100 * time.Millisecond)
ts2 := f.Timestamp()
require.Greater(t, ts2, ts,
"Timestamp should have updated after 1+ seconds. Initial: %d, after 1s: %d",
ts, ts2)
currentTime := uint32(time.Now().Unix())
require.True(t, ts2 >= now && ts2 <= currentTime+1,
"Updated timestamp should be between test start (%d) and current time (%d), got: %d",
now, currentTime, ts2)
}
func Test_StopUpdater(t *testing.T) {
timerTestMu.Lock()
defer timerTestMu.Unlock()
f := New()
f.Start()
// Get initial timestamp.
time.Sleep(100 * time.Millisecond)
initial := f.Timestamp()
f.Stop()
// Verify it stops updating.
time.Sleep(2 * time.Second)
final := f.Timestamp()
currentTime := uint32(time.Now().Unix())
require.Equal(t, initial, final,
"Timestamp should not change after stopping updater. Stopped at: %d, checked at: %d (wall time: %d)",
initial, final, currentTime)
}
func Test_StartStopRepeat(t *testing.T) {
timerTestMu.Lock()
defer timerTestMu.Unlock()
f := New()
f.Start()
f.Stop()
f.Start()
defer f.Stop()
time.Sleep(50 * time.Millisecond)
ts := f.Timestamp()
require.NotZero(t, ts)
}
func Benchmark_CalculateTimestamp(b *testing.B) {
timerTestMu.Lock()
defer timerTestMu.Unlock()
f := New()
f.Start()
defer f.Stop()
b.Run("timecache", func(bb *testing.B) {
bb.ReportAllocs()
bb.ResetTimer()
for n := 0; n < bb.N; n++ {
_ = f.Timestamp()
}
})
b.Run("default", func(bb *testing.B) {
bb.ReportAllocs()
bb.ResetTimer()
for n := 0; n < bb.N; n++ {
_ = uint32(time.Now().Unix())
}
})
b.Run("timecache_asserted", func(bb *testing.B) {
bb.ReportAllocs()
bb.ResetTimer()
for n := 0; n < bb.N; n++ {
res := f.Timestamp()
expected := uint32(time.Now().Unix())
checkTimeStamp(bb, expected, res)
}
})
b.Run("default_asserted", func(bb *testing.B) {
bb.ReportAllocs()
bb.ResetTimer()
for n := 0; n < bb.N; n++ {
expected := uint32(time.Now().Unix())
res := uint32(time.Now().Unix())
checkTimeStamp(bb, expected, res)
}
})
}