forked from dogmatiq/linger
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsleep_test.go
117 lines (95 loc) · 3.4 KB
/
sleep_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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
package linger_test
import (
"context"
"time"
. "github.com/dogmatiq/linger"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)
var _ = Describe("func Sleep()", func() {
It("sleeps for the first positive duration", func() {
start := time.Now()
err := Sleep(context.Background(), 0*time.Second, -1*time.Second, 10*time.Millisecond)
stop := time.Now()
elapsed := stop.Sub(start)
Expect(err).ShouldNot(HaveOccurred())
Expect(elapsed).To(BeNumerically(">=", 10*time.Millisecond))
})
It("returns 'immediately' if none of the durations are positive", func() {
start := time.Now()
err := Sleep(context.Background(), 0*time.Second, -1*time.Second)
stop := time.Now()
elapsed := stop.Sub(start)
Expect(err).ShouldNot(HaveOccurred())
Expect(elapsed).To(BeNumerically("<", 5*time.Millisecond))
})
It("returns early if the context is canceled", func() {
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Millisecond)
defer cancel()
start := time.Now()
err := Sleep(ctx, 100*time.Millisecond)
stop := time.Now()
elapsed := stop.Sub(start)
Expect(err).To(Equal(context.DeadlineExceeded))
Expect(elapsed).To(BeNumerically(">=", 10*time.Millisecond))
Expect(elapsed).To(BeNumerically("<", 100*time.Millisecond))
})
})
var _ = Describe("func SleepX()", func() {
It("applies the transform", func() {
x := func(t time.Duration) time.Duration {
return t / 2
}
start := time.Now()
err := SleepX(context.Background(), x, 20*time.Millisecond)
stop := time.Now()
elapsed := stop.Sub(start)
Expect(err).ShouldNot(HaveOccurred())
Expect(elapsed).To(BeNumerically(">=", 10*time.Millisecond))
Expect(elapsed).To(BeNumerically("<", 20*time.Millisecond))
})
})
var _ = Describe("func SleepUntil()", func() {
It("sleeps until earliest time", func() {
start := time.Now()
err := SleepUntil(context.Background(), start.Add(20*time.Millisecond), start.Add(10*time.Millisecond))
stop := time.Now()
elapsed := stop.Sub(start)
Expect(err).ShouldNot(HaveOccurred())
Expect(elapsed).To(BeNumerically(">=", 10*time.Millisecond))
Expect(elapsed).To(BeNumerically("<", 20*time.Millisecond))
})
It("returns immediately if all of the times are in the past", func() {
start := time.Now()
err := SleepUntil(context.Background(), start.Add(-20*time.Millisecond), start.Add(-10*time.Millisecond))
stop := time.Now()
elapsed := stop.Sub(start)
Expect(err).ShouldNot(HaveOccurred())
Expect(elapsed).To(BeNumerically("<", 5*time.Millisecond))
})
It("returns early if the context is canceled", func() {
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Millisecond)
defer cancel()
start := time.Now()
err := SleepUntil(ctx, start.Add(100*time.Millisecond))
stop := time.Now()
elapsed := stop.Sub(start)
Expect(err).To(Equal(context.DeadlineExceeded))
Expect(elapsed).To(BeNumerically(">=", 10*time.Millisecond))
Expect(elapsed).To(BeNumerically("<", 100*time.Millisecond))
})
})
var _ = Describe("func SleepUntilX()", func() {
It("applies the transform", func() {
x := func(t time.Duration) time.Duration {
return t / 2
}
start := time.Now()
err := SleepUntilX(context.Background(), x, start.Add(20*time.Millisecond))
stop := time.Now()
elapsed := stop.Sub(start)
Expect(err).ShouldNot(HaveOccurred())
Expect(elapsed).To(BeNumerically(">=", 10*time.Millisecond))
Expect(elapsed).To(BeNumerically("<", 20*time.Millisecond))
})
})