]> Cypherpunks repositories - gostls13.git/commitdiff
time: fix race
authorBrad Fitzpatrick <bradfitz@golang.org>
Mon, 14 Jan 2013 22:09:42 +0000 (14:09 -0800)
committerBrad Fitzpatrick <bradfitz@golang.org>
Mon, 14 Jan 2013 22:09:42 +0000 (14:09 -0800)
Fixes #4622

R=golang-dev, dave, dvyukov
CC=golang-dev
https://golang.org/cl/7103046

src/pkg/time/export_test.go [new file with mode: 0644]
src/pkg/time/internal_test.go
src/pkg/time/time.go
src/pkg/time/time_test.go

diff --git a/src/pkg/time/export_test.go b/src/pkg/time/export_test.go
new file mode 100644 (file)
index 0000000..130ca8f
--- /dev/null
@@ -0,0 +1,19 @@
+// 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)
+}
index b753896d775e132093f3ba474084c120b47236b4..918a9f33be6f8629088337e9099876be2263aa70 100644 (file)
@@ -6,7 +6,7 @@ package time
 
 func init() {
        // force US/Pacific for time zone tests
-       localOnce.Do(initTestingZone)
+       ForceUSPacificForTesting()
 }
 
 var Interrupt = interrupt
index 190cc37ddbd97397423dba16d5459500d2eed507..d291672af10a42a05fc47433d2b4a40884944083 100644 (file)
@@ -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
index 0224fed4bdfcb88355051fc9e10551ba8536701b..a8953aefd33c666d2d222e66ab73243db8044fde 100644 (file)
@@ -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