-
Notifications
You must be signed in to change notification settings - Fork 3
/
clock_121_test.go
84 lines (69 loc) · 2.58 KB
/
clock_121_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
//go:build go1.21
package clocks
import (
"context"
"errors"
"testing"
"time"
)
func TestDefaultClockContext121(t *testing.T) {
c := DefaultClock()
t.Run("ContextWithDeadlineCause", func(t *testing.T) {
base := c.Now()
ctx, cancel := c.ContextWithDeadlineCause(context.Background(), base.Add(time.Millisecond), errors.New("test"))
t.Cleanup(cancel)
if v := c.SleepUntil(ctx, base.Add(time.Second)); v {
t.Errorf("unexpected return value: %t; expected false", v)
} else {
if ctx.Err() != context.DeadlineExceeded {
t.Errorf("unexpected error: %v; expected %v", ctx.Err(), context.DeadlineExceeded)
}
if context.Cause(ctx) == nil || context.Cause(ctx).Error() != "test" {
t.Errorf("unexpected cause: %v; expected %v", context.Cause(ctx), "test")
}
}
})
t.Run("ContextWithDeadlineCauseCanceled", func(t *testing.T) {
base := c.Now()
ctx, cancel := c.ContextWithDeadlineCause(context.Background(), base.Add(500*time.Millisecond), errors.New("test"))
cancel()
if v := c.SleepUntil(ctx, base.Add(time.Second)); v {
t.Errorf("unexpected return value: %t; expected false", v)
} else {
if ctx.Err() != context.Canceled {
t.Errorf("unexpected error: %v; expected %v", ctx.Err(), context.Canceled)
}
if context.Cause(ctx) == nil || context.Cause(ctx) != context.Canceled {
t.Errorf("unexpected cause: %v; expected %v", context.Cause(ctx), context.Canceled)
}
}
})
t.Run("ContextWithTimeoutCause", func(t *testing.T) {
ctx, cancel := c.ContextWithTimeoutCause(context.Background(), time.Millisecond, errors.New("test"))
t.Cleanup(cancel)
if v := c.SleepFor(ctx, time.Second); v {
t.Errorf("unexpected return value: %t; expected false", v)
} else {
if ctx.Err() != context.DeadlineExceeded {
t.Errorf("unexpected error: %v; expected %v", ctx.Err(), context.DeadlineExceeded)
}
if context.Cause(ctx) == nil || context.Cause(ctx).Error() != "test" {
t.Errorf("unexpected cause: %v; expected %v", context.Cause(ctx), "test")
}
}
})
t.Run("ContextWithTimeoutCauseCanceled", func(t *testing.T) {
ctx, cancel := c.ContextWithTimeoutCause(context.Background(), 500*time.Millisecond, errors.New("test"))
cancel()
if v := c.SleepFor(ctx, time.Second); v {
t.Errorf("unexpected return value: %t; expected false", v)
} else {
if ctx.Err() != context.Canceled {
t.Errorf("unexpected error: %v; expected %v", ctx.Err(), context.Canceled)
}
if context.Cause(ctx) == nil || context.Cause(ctx) != context.Canceled {
t.Errorf("unexpected cause: %v; expected %v", context.Cause(ctx), context.Canceled)
}
}
})
}