Debian Codegraph @

dag/dagctx_test.go

raw symbols SCIP
1 package ctxext
2
3 import (
4 "math/rand"
5 "testing"
6 "time"
7
8 context "golang.org/x/net/context"
9 )
10
11 func TestWithParentsSingle(t *testing.T) {
12 ctx1, cancel := context.WithCancel(context.Background())
13 ctx2 := WithParents(ctx1)
14
15 select {
16 case <-ctx2.Done():
17 t.Fatal("ended too early")
18 case <-time.After(time.Millisecond):
19 }
20
21 cancel()
22
23 select {
24 case <-ctx2.Done():
25 case <-time.After(time.Millisecond):
26 t.Error("should've cancelled it")
27 }
28
29 if ctx2.Err() != ctx1.Err() {
30 t.Error("errors should match")
31 }
32 }
33
34 func TestWithParentsDeadline(t *testing.T) {
35 ctx1, _ := context.WithCancel(context.Background())
36 ctx2, _ := context.WithTimeout(context.Background(), time.Second)
37 ctx3, _ := context.WithTimeout(context.Background(), time.Second*2)
38
39 ctx := WithParents(ctx1)
40 d, ok := ctx.Deadline()
41 if ok {
42 t.Error("ctx should have no deadline")
43 }
44
45 ctx = WithParents(ctx1, ctx2, ctx3)
46 d, ok = ctx.Deadline()
47 d2, ok2 := ctx2.Deadline()
48 if !ok {
49 t.Error("ctx should have deadline")
50 } else if !ok2 {
51 t.Error("ctx2 should have deadline")
52 } else if !d.Equal(d2) {
53 t.Error("ctx should have ctx2 deadline")
54 }
55 }
56
57 func SubtestWithParentsMany(t *testing.T, n int) {
58
59 ctxs := make([]context.Context, n)
60 cancels := make([]context.CancelFunc, n)
61 for i := 0; i < n; i++ {
62 if i == 0 { // first must be new.
63 ctxs[i], cancels[i] = context.WithCancel(context.Background())
64 continue
65 }
66
67 r := rand.Intn(i) // select a previous context
68 switch rand.Intn(6) {
69 case 0: // same as old
70 ctxs[i], cancels[i] = ctxs[r], cancels[r]
71 case 1: // derive from another
72 ctxs[i], cancels[i] = context.WithCancel(ctxs[r])
73 case 2: // deadline
74 t := (time.Second) * time.Duration(r+2) // +2 so we dont run into 0 or timing bugs
75 ctxs[i], cancels[i] = context.WithTimeout(ctxs[r], t)
76 default: // new context
77 ctxs[i], cancels[i] = context.WithCancel(context.Background())
78 }
79 }
80
81 ctx := WithParents(ctxs...)
82
83 // test deadline is earliest.
84 d1 := earliestDeadline(ctxs)
85 d2, ok := ctx.Deadline()
86 switch {
87 case d1 == nil && ok:
88 t.Error("nil, should not have deadline")
89 case d1 != nil && !ok:
90 t.Error("not nil, should have deadline")
91 case d1 != nil && ok && !d1.Equal(d2):
92 t.Error("should find same deadline")
93 }
94 if ok {
95 t.Logf("deadline - now: %s", d2.Sub(time.Now()))
96 }
97
98 select {
99 case <-ctx.Done():
100 t.Fatal("ended too early")
101 case <-time.After(time.Millisecond):
102 }
103
104 // cancel just one
105 r := rand.Intn(len(cancels))
106 cancels[r]()
107
108 select {
109 case <-ctx.Done():
110 case <-time.After(time.Millisecond):
111 t.Error("any should've cancelled it")
112 }
113
114 if ctx.Err() != ctxs[r].Err() {
115 t.Error("errors should match")
116 }
117 }
118
119 func TestWithParentsMany(t *testing.T) {
120 n := 100
121 for i := 1; i < n; i++ {
122 SubtestWithParentsMany(t, i)
123 }
124 }