generated from dogmatiq/template-go
-
Notifications
You must be signed in to change notification settings - Fork 2
/
context_test.go
62 lines (50 loc) · 1.66 KB
/
context_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
package linger_test
import (
"context"
"time"
. "github.com/dogmatiq/linger"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)
var _ = Describe("func FromContextDeadline()", func() {
It("returns the time until the deadline of the context", func() {
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
d, ok := FromContextDeadline(ctx)
Expect(ok).To(BeTrue())
Expect(d).To(BeNumerically("~", 10*time.Second, 1*time.Second))
})
It("returns false if the context does not have a deadline", func() {
_, ok := FromContextDeadline(context.Background())
Expect(ok).To(BeFalse())
})
})
var _ = Describe("func ContextWithTimeout()", func() {
It("sets a timeout for the first positive duration", func() {
expect := time.Now().Add(10 * time.Second)
ctx, cancel := ContextWithTimeout(context.Background(), 0*time.Second, -1*time.Second, 10*time.Second)
defer cancel()
dl, ok := ctx.Deadline()
Expect(ok).To(BeTrue())
Expect(dl).To(BeTemporally("~", expect))
})
It("times out 'immediately' if none of the durations are positive", func() {
ctx, cancel := ContextWithTimeout(context.Background(), 0*time.Second, -1*time.Second)
defer cancel()
err := ctx.Err()
Expect(err).To(Equal(context.DeadlineExceeded))
})
})
var _ = Describe("func ContextWithTimeoutX()", func() {
It("applies the transform", func() {
x := func(t time.Duration) time.Duration {
return t / 2
}
expect := time.Now().Add(10 * time.Second)
ctx, cancel := ContextWithTimeoutX(context.Background(), x, 20*time.Second)
defer cancel()
dl, ok := ctx.Deadline()
Expect(ok).To(BeTrue())
Expect(dl).To(BeTemporally("~", expect))
})
})