Commit fd1abac7 authored by Brad Fitzpatrick's avatar Brad Fitzpatrick

time: fix race

Fixes #4622

R=golang-dev, dave, dvyukov
CC=golang-dev
https://golang.org/cl/7103046
parent 6c3736a5
// Copyright 2013 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package time
import (
"sync"
)
func ResetLocalOnceForTest() {
localOnce = sync.Once{}
localLoc = Location{}
}
func ForceUSPacificForTesting() {
ResetLocalOnceForTest()
localOnce.Do(initTestingZone)
}
......@@ -6,7 +6,7 @@ package time
func init() {
// force US/Pacific for time zone tests
localOnce.Do(initTestingZone)
ForceUSPacificForTesting()
}
var Interrupt = interrupt
......
......@@ -261,8 +261,8 @@ func (t Time) abs() uint64 {
// extracting both return values from a single zone lookup.
func (t Time) locabs() (name string, offset int, abs uint64) {
l := t.loc
if l == nil {
l = &utcLoc
if l == nil || l == &localLoc {
l = l.get()
}
// Avoid function call if we hit the local time cache.
sec := t.sec + internalToUnix
......
......@@ -1227,6 +1227,22 @@ func TestParseDurationRoundTrip(t *testing.T) {
}
}
// golang.org/issue/4622
func TestLocationRace(t *testing.T) {
ResetLocalOnceForTest() // reset the Once to trigger the race
c := make(chan string, 1)
go func() {
c <- Now().String()
}()
Now().String()
<-c
Sleep(100 * Millisecond)
// Back to Los Angeles for subsequent tests:
ForceUSPacificForTesting()
}
var (
t Time
u int64
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment