Skip to content

Commit 9ec2990

Browse files
committed
internal/counter: fix TestNewFile test
The test checks whether a newly created counter file has the TimeEnd metadata value within expected range, by computing the time difference between the TimeEnd value and a test reference time computed at the beginning of the test. Unfortunately, the reference timestamp calculation used a different time zone than what's used in computing TimeEnd, and caused the test failure on some builders with some probabilities depending on builders' time zones. Here is the log from the test failure that shows this issue. --- FAIL: TestNewFile (0.00s) counter_test.go:177: GOOS linux GOARCH amd64 counter_test.go:233: now: 2023-07-08 00:00:00 +0000 UTC file: counter.test-go1.19.10-linux-amd64-2023-07-08.v1.count TimeBegin: 2023-07-08 00:00:00 +0800 CST TimeEnd: 2023-07-16 00:00:00 +0800 CST counter_test.go:234: 0: days = 7, want 7 < days <= 14 counter_test.go:233: now: 2023-07-08 00:00:00 +0000 UTC file: counter.test-go1.19.10-linux-amd64-2023-07-08.v1.count TimeBegin: 2023-07-08 00:00:00 +0800 CST TimeEnd: 2023-07-16 00:00:00 +0800 CST counter_test.go:234: 13: days = 7, want 7 < days <= 14 counter_test.go:233: now: 2023-07-08 00:00:00 +0000 UTC file: counter.test-go1.19.10-linux-amd64-2023-07-08.v1.count TimeBegin: 2023-07-08 00:00:00 +0800 CST TimeEnd: 2023-07-16 00:00:00 +0800 CST counter_test.go:234: 15: days = 7, want 7 < days <= 14 For golang/go#60970 Fixes golang/go#60966 Change-Id: I52936f5fe12c13f7ccf3c052cbbb9b403fcad332 Reviewed-on: https://go-review.googlesource.com/c/telemetry/+/508501 Run-TryBot: Hyang-Ah Hana Kim <[email protected]> Reviewed-by: Peter Weinberger <[email protected]> Run-TryBot: Heschi Kreinick <[email protected]> TryBot-Result: Gopher Robot <[email protected]>
1 parent cbe576d commit 9ec2990

File tree

1 file changed

+6
-4
lines changed

1 file changed

+6
-4
lines changed

internal/counter/counter_test.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -177,8 +177,10 @@ func TestNewFile(t *testing.T) {
177177
t.Logf("GOOS %s GOARCH %s", runtime.GOOS, runtime.GOARCH)
178178
setup(t)
179179
defer restore()
180-
year, month, day := time.Now().Date()
181-
now := time.Date(year, month, day, 0, 0, 0, 0, time.UTC)
180+
now := time.Now()
181+
year, month, day := now.Date()
182+
// preserve time location as done in (*file).filename.
183+
testStartTime := time.Date(year, month, day, 0, 0, 0, 0, now.Location())
182184

183185
// test that completely new files have dates well in the future
184186
// Try 20 times to get 20 different random numbers.
@@ -227,10 +229,10 @@ func TestNewFile(t *testing.T) {
227229
close(&f)
228230
t.Fatal(err)
229231
}
230-
days := (timeEnd.Sub(now)) / (24 * time.Hour)
232+
days := (timeEnd.Sub(testStartTime)) / (24 * time.Hour)
231233
if days <= 7 || days > 14 {
232234
timeBegin, _ := time.Parse(time.RFC3339, cf.Meta["TimeBegin"])
233-
t.Logf("now: %v file: %v TimeBegin: %v TimeEnd: %v", now, fi[0].Name(), timeBegin, timeEnd)
235+
t.Logf("testStartTime: %v file: %v TimeBegin: %v TimeEnd: %v", testStartTime, fi[0].Name(), timeBegin, timeEnd)
234236
t.Errorf("%d: days = %d, want 7 < days <= 14", i, days)
235237
}
236238
close(&f)

0 commit comments

Comments
 (0)