Skip to content

Commit

Permalink
Merge pull request #108113 from cockroachdb/blathers/backport-release…
Browse files Browse the repository at this point in the history
…-23.1-108094
  • Loading branch information
pav-kv committed Aug 3, 2023
2 parents 9011b54 + 24d368a commit 0476a36
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 2 deletions.
2 changes: 2 additions & 0 deletions pkg/BUILD.bazel
Expand Up @@ -671,6 +671,7 @@ ALL_TESTS = [
"//pkg/util/timeofday:timeofday_test",
"//pkg/util/timetz:timetz_test",
"//pkg/util/timeutil/pgdate:pgdate_test",
"//pkg/util/timeutil/ptp:ptp_test",
"//pkg/util/timeutil:timeutil_test",
"//pkg/util/tochar:tochar_test",
"//pkg/util/tracing/collector:collector_test",
Expand Down Expand Up @@ -2320,6 +2321,7 @@ GO_TARGETS = [
"//pkg/util/timeutil/pgdate:pgdate",
"//pkg/util/timeutil/pgdate:pgdate_test",
"//pkg/util/timeutil/ptp:ptp",
"//pkg/util/timeutil/ptp:ptp_test",
"//pkg/util/timeutil:timeutil",
"//pkg/util/timeutil:timeutil_test",
"//pkg/util/tochar:tochar",
Expand Down
18 changes: 17 additions & 1 deletion pkg/util/timeutil/ptp/BUILD.bazel
@@ -1,4 +1,4 @@
load("@io_bazel_rules_go//go:def.bzl", "go_library")
load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_test")

go_library(
name = "ptp",
Expand Down Expand Up @@ -59,3 +59,19 @@ go_library(
"//conditions:default": [],
}),
)

go_test(
name = "ptp_test",
srcs = ["ptp_clock_linux_test.go"],
args = ["-test.timeout=295s"],
embed = [":ptp"],
deps = select({
"@io_bazel_rules_go//go/platform:android": [
"//pkg/util/timeutil",
],
"@io_bazel_rules_go//go/platform:linux": [
"//pkg/util/timeutil",
],
"//conditions:default": [],
}),
)
7 changes: 6 additions & 1 deletion pkg/util/timeutil/ptp/ptp_clock_linux.go
Expand Up @@ -79,5 +79,10 @@ func (p Clock) Now() time.Time {
panic(err)
}

return timeutil.Unix(int64(ts.tv_sec)*1e9, int64(ts.tv_nsec))
return timeutil.Unix(int64(ts.tv_sec), int64(ts.tv_nsec))
}

// realtime returns a clock using the system CLOCK_REALTIME device. For testing.
func realtime() Clock {
return Clock{clockDeviceID: uintptr(C.CLOCK_REALTIME)}
}
30 changes: 30 additions & 0 deletions pkg/util/timeutil/ptp/ptp_clock_linux_test.go
@@ -0,0 +1,30 @@
// Copyright 2023 The Cockroach Authors.
//
// Use of this software is governed by the Business Source License
// included in the file licenses/BSL.txt.
//
// As of the Change Date specified in that file, in accordance with
// the Business Source License, use of this software will be governed
// by the Apache License, Version 2.0, included in the file
// licenses/APL.txt.

//go:build linux
// +build linux

package ptp

import (
"testing"
"time"

"github.com/cockroachdb/cockroach/pkg/util/timeutil"
)

// TestClockNow sanity checks that Clock.Now() sourced from the CLOCK_REALTIME
// device returns time close to timeutil.Now(). This ensures that the conversion
// from the time returned by a clock device to Go's time.Time is correct.
func TestClockNow(t *testing.T) {
if got, want := realtime().Now(), timeutil.Now(); want.Sub(got).Abs() > 10*time.Second {
t.Errorf("clock mismatch: got %v; timeutil says %v", got, want)
}
}

0 comments on commit 0476a36

Please sign in to comment.