File tree Expand file tree Collapse file tree 4 files changed +55
-2
lines changed Expand file tree Collapse file tree 4 files changed +55
-2
lines changed Original file line number Diff line number Diff line change @@ -694,6 +694,7 @@ ALL_TESTS = [
694694 "//pkg/util/timeofday:timeofday_test" ,
695695 "//pkg/util/timetz:timetz_test" ,
696696 "//pkg/util/timeutil/pgdate:pgdate_test" ,
697+ "//pkg/util/timeutil/ptp:ptp_test" ,
697698 "//pkg/util/timeutil:timeutil_test" ,
698699 "//pkg/util/tochar:tochar_test" ,
699700 "//pkg/util/tracing/collector:collector_test" ,
@@ -2416,6 +2417,7 @@ GO_TARGETS = [
24162417 "//pkg/util/timeutil/pgdate:pgdate" ,
24172418 "//pkg/util/timeutil/pgdate:pgdate_test" ,
24182419 "//pkg/util/timeutil/ptp:ptp" ,
2420+ "//pkg/util/timeutil/ptp:ptp_test" ,
24192421 "//pkg/util/timeutil:timeutil" ,
24202422 "//pkg/util/timeutil:timeutil_test" ,
24212423 "//pkg/util/tochar:tochar" ,
Original file line number Diff line number Diff line change 1- load ("@io_bazel_rules_go//go:def.bzl" , "go_library" )
1+ load ("@io_bazel_rules_go//go:def.bzl" , "go_library" , "go_test" )
22
33go_library (
44 name = "ptp" ,
@@ -59,3 +59,19 @@ go_library(
5959 "//conditions:default" : [],
6060 }),
6161)
62+
63+ go_test (
64+ name = "ptp_test" ,
65+ srcs = ["ptp_clock_linux_test.go" ],
66+ args = ["-test.timeout=295s" ],
67+ embed = [":ptp" ],
68+ deps = select ({
69+ "@io_bazel_rules_go//go/platform:android" : [
70+ "//pkg/util/timeutil" ,
71+ ],
72+ "@io_bazel_rules_go//go/platform:linux" : [
73+ "//pkg/util/timeutil" ,
74+ ],
75+ "//conditions:default" : [],
76+ }),
77+ )
Original file line number Diff line number Diff line change @@ -79,5 +79,10 @@ func (p Clock) Now() time.Time {
7979 panic (err )
8080 }
8181
82- return timeutil .Unix (int64 (ts .tv_sec )* 1e9 , int64 (ts .tv_nsec ))
82+ return timeutil .Unix (int64 (ts .tv_sec ), int64 (ts .tv_nsec ))
83+ }
84+
85+ // realtime returns a clock using the system CLOCK_REALTIME device. For testing.
86+ func realtime () Clock {
87+ return Clock {clockDeviceID : uintptr (C .CLOCK_REALTIME )}
8388}
Original file line number Diff line number Diff line change 1+ // Copyright 2023 The Cockroach Authors.
2+ //
3+ // Use of this software is governed by the Business Source License
4+ // included in the file licenses/BSL.txt.
5+ //
6+ // As of the Change Date specified in that file, in accordance with
7+ // the Business Source License, use of this software will be governed
8+ // by the Apache License, Version 2.0, included in the file
9+ // licenses/APL.txt.
10+
11+ //go:build linux
12+ // +build linux
13+
14+ package ptp
15+
16+ import (
17+ "testing"
18+ "time"
19+
20+ "github.com/cockroachdb/cockroach/pkg/util/timeutil"
21+ )
22+
23+ // TestClockNow sanity checks that Clock.Now() sourced from the CLOCK_REALTIME
24+ // device returns time close to timeutil.Now(). This ensures that the conversion
25+ // from the time returned by a clock device to Go's time.Time is correct.
26+ func TestClockNow (t * testing.T ) {
27+ if got , want := realtime ().Now (), timeutil .Now (); want .Sub (got ).Abs () > 10 * time .Second {
28+ t .Errorf ("clock mismatch: got %v; timeutil says %v" , got , want )
29+ }
30+ }
You can’t perform that action at this time.
0 commit comments