Daily bump.
[official-gcc.git] / libgo / go / time / internal_test.go
blob87fdd3216fa7cc213e1ec83ae30043466c582c4d
1 // Copyright 2011 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
5 package time
7 import (
8 "errors"
9 "runtime"
12 func init() {
13 // force US/Pacific for time zone tests
14 ForceUSPacificForTesting()
17 var Interrupt = interrupt
18 var DaysIn = daysIn
20 func empty(now int64, arg interface{}) {}
22 // Test that a runtimeTimer with a duration so large it overflows
23 // does not cause other timers to hang.
25 // This test has to be in internal_test.go since it fiddles with
26 // unexported data structures.
27 func CheckRuntimeTimerOverflow() error {
28 // We manually create a runtimeTimer to bypass the overflow
29 // detection logic in NewTimer: we're testing the underlying
30 // runtime.addtimer function.
31 r := &runtimeTimer{
32 when: nano() + (1<<63 - 1),
33 f: empty,
34 arg: nil,
36 startTimer(r)
38 timeout := 100 * Millisecond
39 if runtime.GOOS == "windows" {
40 // Allow more time for gobuilder to succeed.
41 timeout = Second
44 // Start a goroutine that should send on t.C before the timeout.
45 t := NewTimer(1)
47 defer func() {
48 // Subsequent tests won't work correctly if we don't stop the
49 // overflow timer and kick the timer proc back into service.
51 // The timer proc is now sleeping and can only be awoken by
52 // adding a timer to the *beginning* of the heap. We can't
53 // wake it up by calling NewTimer since other tests may have
54 // left timers running that should have expired before ours.
55 // Instead we zero the overflow timer duration and start it
56 // once more.
57 stopTimer(r)
58 t.Stop()
59 r.when = 0
60 startTimer(r)
61 }()
63 // Try to receive from t.C before the timeout. It will succeed
64 // iff the previous sleep was able to finish. We're forced to
65 // spin and yield after trying to receive since we can't start
66 // any more timers (they might hang due to the same bug we're
67 // now testing).
68 stop := Now().Add(timeout)
69 for {
70 select {
71 case <-t.C:
72 return nil // It worked!
73 default:
74 if Now().After(stop) {
75 return errors.New("runtime timer stuck: overflow in addtimer")
77 runtime.Gosched()