OpenCores
URL https://opencores.org/ocsvn/openrisc/openrisc/trunk

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [gnu-dev/] [or1k-gcc/] [libgo/] [go/] [time/] [sleep_test.go] - Blame information for rev 760

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 747 jeremybenn
// Copyright 2009 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.
4
 
5
package time_test
6
 
7
import (
8
        "errors"
9
        "fmt"
10
        "runtime"
11
        "sort"
12
        "sync/atomic"
13
        "testing"
14
        . "time"
15
)
16
 
17
func TestSleep(t *testing.T) {
18
        const delay = 100 * Millisecond
19
        go func() {
20
                Sleep(delay / 2)
21
                Interrupt()
22
        }()
23
        start := Now()
24
        Sleep(delay)
25
        duration := Now().Sub(start)
26
        if duration < delay {
27
                t.Fatalf("Sleep(%s) slept for only %s", delay, duration)
28
        }
29
}
30
 
31
// Test the basic function calling behavior. Correct queueing
32
// behavior is tested elsewhere, since After and AfterFunc share
33
// the same code.
34
func TestAfterFunc(t *testing.T) {
35
        i := 10
36
        c := make(chan bool)
37
        var f func()
38
        f = func() {
39
                i--
40
                if i >= 0 {
41
                        AfterFunc(0, f)
42
                        Sleep(1 * Second)
43
                } else {
44
                        c <- true
45
                }
46
        }
47
 
48
        AfterFunc(0, f)
49
        <-c
50
}
51
 
52
func TestAfterStress(t *testing.T) {
53
        stop := uint32(0)
54
        go func() {
55
                for atomic.LoadUint32(&stop) == 0 {
56
                        runtime.GC()
57
                        // Need to yield, because otherwise
58
                        // the main goroutine will never set the stop flag.
59
                        runtime.Gosched()
60
                }
61
        }()
62
        c := Tick(1)
63
        for i := 0; i < 100; i++ {
64
                <-c
65
        }
66
        atomic.StoreUint32(&stop, 1)
67
}
68
 
69
func BenchmarkAfterFunc(b *testing.B) {
70
        i := b.N
71
        c := make(chan bool)
72
        var f func()
73
        f = func() {
74
                i--
75
                if i >= 0 {
76
                        AfterFunc(0, f)
77
                } else {
78
                        c <- true
79
                }
80
        }
81
 
82
        AfterFunc(0, f)
83
        <-c
84
}
85
 
86
func BenchmarkAfter(b *testing.B) {
87
        for i := 0; i < b.N; i++ {
88
                <-After(1)
89
        }
90
}
91
 
92
func BenchmarkStop(b *testing.B) {
93
        for i := 0; i < b.N; i++ {
94
                NewTimer(1 * Second).Stop()
95
        }
96
}
97
 
98
func TestAfter(t *testing.T) {
99
        const delay = 100 * Millisecond
100
        start := Now()
101
        end := <-After(delay)
102
        if duration := Now().Sub(start); duration < delay {
103
                t.Fatalf("After(%s) slept for only %d ns", delay, duration)
104
        }
105
        if min := start.Add(delay); end.Before(min) {
106
                t.Fatalf("After(%s) expect >= %s, got %s", delay, min, end)
107
        }
108
}
109
 
110
func TestAfterTick(t *testing.T) {
111
        const (
112
                Delta = 100 * Millisecond
113
                Count = 10
114
        )
115
        t0 := Now()
116
        for i := 0; i < Count; i++ {
117
                <-After(Delta)
118
        }
119
        t1 := Now()
120
        d := t1.Sub(t0)
121
        target := Delta * Count
122
        if d < target*9/10 || d > target*30/10 {
123
                t.Fatalf("%d ticks of %s took %s, expected %s", Count, Delta, d, target)
124
        }
125
}
126
 
127
func TestAfterStop(t *testing.T) {
128
        AfterFunc(100*Millisecond, func() {})
129
        t0 := NewTimer(50 * Millisecond)
130
        c1 := make(chan bool, 1)
131
        t1 := AfterFunc(150*Millisecond, func() { c1 <- true })
132
        c2 := After(200 * Millisecond)
133
        if !t0.Stop() {
134
                t.Fatalf("failed to stop event 0")
135
        }
136
        if !t1.Stop() {
137
                t.Fatalf("failed to stop event 1")
138
        }
139
        <-c2
140
        select {
141
        case <-t0.C:
142
                t.Fatalf("event 0 was not stopped")
143
        case <-c1:
144
                t.Fatalf("event 1 was not stopped")
145
        default:
146
        }
147
        if t1.Stop() {
148
                t.Fatalf("Stop returned true twice")
149
        }
150
}
151
 
152
func TestAfterQueuing(t *testing.T) {
153
        // This test flakes out on some systems,
154
        // so we'll try it a few times before declaring it a failure.
155
        const attempts = 3
156
        err := errors.New("!=nil")
157
        for i := 0; i < attempts && err != nil; i++ {
158
                if err = testAfterQueuing(t); err != nil {
159
                        t.Logf("attempt %v failed: %v", i, err)
160
                }
161
        }
162
        if err != nil {
163
                t.Fatal(err)
164
        }
165
}
166
 
167
// For gccgo omit 0 for now because it can take too long to start the
168
var slots = []int{5, 3, 6, 6, 6, 1, 1, 2, 7, 9, 4, 8 /*0*/ }
169
 
170
type afterResult struct {
171
        slot int
172
        t    Time
173
}
174
 
175
func await(slot int, result chan<- afterResult, ac <-chan Time) {
176
        result <- afterResult{slot, <-ac}
177
}
178
 
179
func testAfterQueuing(t *testing.T) error {
180
        const (
181
                Delta = 100 * Millisecond
182
        )
183
        // make the result channel buffered because we don't want
184
        // to depend on channel queueing semantics that might
185
        // possibly change in the future.
186
        result := make(chan afterResult, len(slots))
187
 
188
        t0 := Now()
189
        for _, slot := range slots {
190
                go await(slot, result, After(Duration(slot)*Delta))
191
        }
192
        sort.Ints(slots)
193
        for _, slot := range slots {
194
                r := <-result
195
                if r.slot != slot {
196
                        return fmt.Errorf("after slot %d, expected %d", r.slot, slot)
197
                }
198
                dt := r.t.Sub(t0)
199
                target := Duration(slot) * Delta
200
                if dt < target-Delta/2 || dt > target+Delta*10 {
201
                        return fmt.Errorf("After(%s) arrived at %s, expected [%s,%s]", target, dt, target-Delta/2, target+Delta*10)
202
                }
203
        }
204
        return nil
205
}
206
 
207
func TestTimerStopStress(t *testing.T) {
208
        if testing.Short() {
209
                return
210
        }
211
        for i := 0; i < 100; i++ {
212
                go func(i int) {
213
                        timer := AfterFunc(2*Second, func() {
214
                                t.Fatalf("timer %d was not stopped", i)
215
                        })
216
                        Sleep(1 * Second)
217
                        timer.Stop()
218
                }(i)
219
        }
220
        Sleep(3 * Second)
221
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.