| 1 |
package utils |
| 2 |
|
| 3 |
import ( |
| 4 |
"sync" |
| 5 |
"testing" |
| 6 |
"time" |
| 7 |
|
| 8 |
"github.com/stretchr/testify/require" |
| 9 |
) |
| 10 |
|
| 11 |
var timerTestMu sync.Mutex |
| 12 |
|
| 13 |
func checkTimeStamp(tb testing.TB, expectedCurrent, actualCurrent uint32) { |
| 14 |
tb.Helper() |
| 15 |
// Test with buffer of ±2 seconds for CI environment tolerance. The |
| 16 |
// failure message is built only on failure: boxing require.True's |
| 17 |
// variadic args allocated on every call, which made the fiber_asserted |
| 18 |
// benchmark's B/op metric nondeterministic. |
| 19 |
if actualCurrent < expectedCurrent-2 || actualCurrent > expectedCurrent+2 { |
| 20 |
tb.Fatalf("Expected timestamp %d (±2s), got %d (diff: %d)", |
| 21 |
expectedCurrent, actualCurrent, int64(actualCurrent)-int64(expectedCurrent)) |
| 22 |
} |
| 23 |
} |
| 24 |
|
| 25 |
func Test_TimeStampUpdater(t *testing.T) { |
| 26 |
timerTestMu.Lock() |
| 27 |
defer timerTestMu.Unlock() |
| 28 |
|
| 29 |
StartTimeStampUpdater() |
| 30 |
defer StopTimeStampUpdater() |
| 31 |
|
| 32 |
now := uint32(time.Now().Unix()) |
| 33 |
|
| 34 |
// Give it a moment to initialize |
| 35 |
time.Sleep(100 * time.Millisecond) |
| 36 |
|
| 37 |
ts := Timestamp() |
| 38 |
require.True(t, ts >= now-1 && ts <= now+1, |
| 39 |
"Initial timestamp should be within ±1 second of current time. Expected: %d±1, got: %d (diff: %d)", |
| 40 |
now, ts, int64(ts)-int64(now)) |
| 41 |
|
| 42 |
// Wait for next update |
| 43 |
time.Sleep(1100 * time.Millisecond) |
| 44 |
|
| 45 |
ts2 := Timestamp() |
| 46 |
require.Greater(t, ts2, ts, |
| 47 |
"Timestamp should have updated after 1+ seconds. Initial: %d, after 1s: %d", |
| 48 |
ts, ts2) |
| 49 |
|
| 50 |
currentTime := uint32(time.Now().Unix()) |
| 51 |
require.True(t, ts2 >= now && ts2 <= currentTime+1, |
| 52 |
"Updated timestamp should be between test start (%d) and current time (%d), got: %d", |
| 53 |
now, currentTime, ts2) |
| 54 |
} |
| 55 |
|
| 56 |
func Test_StopTimeStampUpdater(t *testing.T) { |
| 57 |
timerTestMu.Lock() |
| 58 |
defer timerTestMu.Unlock() |
| 59 |
|
| 60 |
StartTimeStampUpdater() |
| 61 |
|
| 62 |
// Get initial timestamp |
| 63 |
time.Sleep(100 * time.Millisecond) |
| 64 |
initial := Timestamp() |
| 65 |
|
| 66 |
StopTimeStampUpdater() |
| 67 |
|
| 68 |
// Verify it stops updating |
| 69 |
time.Sleep(2 * time.Second) |
| 70 |
final := Timestamp() |
| 71 |
currentTime := uint32(time.Now().Unix()) |
| 72 |
require.Equal(t, initial, final, |
| 73 |
"Timestamp should not change after stopping updater. Stopped at: %d, checked at: %d (wall time: %d)", |
| 74 |
initial, final, currentTime) |
| 75 |
} |
| 76 |
|
| 77 |
func Benchmark_CalculateTimestamp(b *testing.B) { |
| 78 |
timerTestMu.Lock() |
| 79 |
defer timerTestMu.Unlock() |
| 80 |
StartTimeStampUpdater() |
| 81 |
defer StopTimeStampUpdater() |
| 82 |
|
| 83 |
b.Run("fiber", func(bb *testing.B) { |
| 84 |
bb.ReportAllocs() |
| 85 |
bb.ResetTimer() |
| 86 |
for n := 0; n < bb.N; n++ { |
| 87 |
_ = Timestamp() |
| 88 |
} |
| 89 |
}) |
| 90 |
|
| 91 |
b.Run("default", func(bb *testing.B) { |
| 92 |
bb.ReportAllocs() |
| 93 |
bb.ResetTimer() |
| 94 |
for n := 0; n < bb.N; n++ { |
| 95 |
_ = uint32(time.Now().Unix()) |
| 96 |
} |
| 97 |
}) |
| 98 |
|
| 99 |
// Simplified asserted benchmarks - measure the realistic cost including validation |
| 100 |
b.Run("fiber_asserted", func(bb *testing.B) { |
| 101 |
bb.ReportAllocs() |
| 102 |
bb.ResetTimer() |
| 103 |
for n := 0; n < bb.N; n++ { |
| 104 |
res := Timestamp() |
| 105 |
expected := uint32(time.Now().Unix()) |
| 106 |
checkTimeStamp(bb, expected, res) |
| 107 |
} |
| 108 |
}) |
| 109 |
|
| 110 |
b.Run("default_asserted", func(bb *testing.B) { |
| 111 |
bb.ReportAllocs() |
| 112 |
bb.ResetTimer() |
| 113 |
for n := 0; n < bb.N; n++ { |
| 114 |
expected := uint32(time.Now().Unix()) |
| 115 |
res := uint32(time.Now().Unix()) |
| 116 |
checkTimeStamp(bb, expected, res) |
| 117 |
} |
| 118 |
}) |
| 119 |
} |
| 120 |
|
| 121 |
func Test_StartStopRepeat(t *testing.T) { |
| 122 |
timerTestMu.Lock() |
| 123 |
defer timerTestMu.Unlock() |
| 124 |
|
| 125 |
StartTimeStampUpdater() |
| 126 |
StopTimeStampUpdater() |
| 127 |
StartTimeStampUpdater() |
| 128 |
defer StopTimeStampUpdater() |
| 129 |
|
| 130 |
time.Sleep(50 * time.Millisecond) |
| 131 |
ts := Timestamp() |
| 132 |
require.NotZero(t, ts) |
| 133 |
} |