Skip to content

Commit 0c80f99

Browse files
committed
test(windows): redirect now/nowTime to time.Now() in tests
On Windows, var now points to GetSystemTimePreciseAsFileTime() via a direct Windows syscall. This syscall bypasses testing/synctest's fake clock, which only intercepts time.Now(). As a result, synctest-wrapped tests fail on Windows: span durations read the real wall clock (a few µs) instead of the fake 2 ms advance, and span-age strings diverge between the test and the code under test. Add an init() in time_windows_test.go that overrides now and nowTime to call time.Now() for the test binary. Production code keeps its high-precision syscall; tests gain fake-clock compatibility without any change to production behavior. Signed-off-by: Kemal Akkoyun <kemal.akkoyun@datadoghq.com>
1 parent ee27909 commit 0c80f99

File tree

1 file changed

+11
-0
lines changed

1 file changed

+11
-0
lines changed

ddtrace/tracer/time_windows_test.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,21 @@ package tracer
77

88
import (
99
"testing"
10+
"time"
1011

1112
"github.com/stretchr/testify/assert"
1213
)
1314

15+
func init() {
16+
// Override now and nowTime to use time.Now() in tests. Production code
17+
// calls GetSystemTimePreciseAsFileTime() directly for higher precision, but
18+
// that syscall bypasses testing/synctest's fake clock. time.Now() is
19+
// intercepted by synctest, so this makes the fake clock work correctly on
20+
// Windows without changing production behavior.
21+
now = func() int64 { return time.Now().UnixNano() }
22+
nowTime = func() time.Time { return time.Now() }
23+
}
24+
1425
func BenchmarkNormalTimeNow(b *testing.B) {
1526
for n := 0; n < b.N; n++ {
1627
lowPrecisionNow()

0 commit comments

Comments
 (0)