-
Notifications
You must be signed in to change notification settings - Fork 85
/
fastclock_test.go
58 lines (53 loc) · 1.44 KB
/
fastclock_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
package regexp2
import (
"fmt"
"testing"
"time"
)
func init() {
//speed up testing by making the timeout clock 1ms instead of 100ms...
//bad for benchmark tests though
SetTimeoutCheckPeriod(time.Millisecond)
}
func TestDeadline(t *testing.T) {
for _, delay := range []time.Duration{
clockPeriod / 10,
clockPeriod,
clockPeriod * 5,
clockPeriod * 10,
} {
delay := delay // Make copy for parallel sub-test.
t.Run(fmt.Sprint(delay), func(t *testing.T) {
t.Parallel()
start := time.Now()
d := makeDeadline(delay)
if d.reached() {
t.Fatalf("deadline (%v) unexpectedly expired immediately", delay)
}
time.Sleep(delay / 2)
if d.reached() {
t.Fatalf("deadline (%v) expired too soon (after %v)", delay, time.Since(start))
}
time.Sleep(delay/2 + 2*clockPeriod) // Give clock time to tick
if !d.reached() {
t.Fatalf("deadline (%v) did not expire within %v", delay, time.Since(start))
}
})
}
}
func TestStopTimeoutClock(t *testing.T) {
// run a quick regex with a long timeout
// make sure the stop clock returns quickly
r := MustCompile(".", 0)
r.MatchTimeout = time.Second * 10
r.MatchString("a")
start := time.Now()
StopTimeoutClock()
stop := time.Now()
if want, got := clockPeriod*2, stop.Sub(start); want < got {
t.Errorf("Expected duration less than %v, got %v", want, got)
}
if want, got := false, fast.running; want != got {
t.Errorf("Expected isRunning to be %v, got %v", want, got)
}
}