-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjoin_test.go
More file actions
168 lines (132 loc) · 3.56 KB
/
Copy pathjoin_test.go
File metadata and controls
168 lines (132 loc) · 3.56 KB
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
// Copyright (c) CattleCloud LLC
// SPDX-License-Identifier: BSD-3-Clause
package scope
import (
"context"
"errors"
"testing"
"time"
)
func TestJoin_CancelA(t *testing.T) {
t.Parallel()
ctxA, cancelA := Cancelable()
ctxB := New()
j, _ := Join(ctxA, ctxB)
cancelA()
<-j.Done()
if !errors.Is(j.Err(), context.Canceled) {
t.Errorf("expected context.Canceled, got %v", j.Err())
}
}
func TestJoin_CancelB(t *testing.T) {
t.Parallel()
ctxA := New()
ctxB, cancelB := Cancelable()
j, _ := Join(ctxA, ctxB)
cancelB()
<-j.Done()
if !errors.Is(j.Err(), context.Canceled) {
t.Errorf("expected context.Canceled, got %v", j.Err())
}
}
func TestJoin_CancelSelf(t *testing.T) {
t.Parallel()
ctxA := New()
ctxB := New()
j, cancel := Join(ctxA, ctxB)
cancel()
<-j.Done()
if !errors.Is(j.Err(), context.Canceled) {
t.Errorf("expected context.Canceled, got %v", j.Err())
}
}
func TestJoin_AlreadyDone(t *testing.T) {
t.Parallel()
t.Run("a already done", func(t *testing.T) {
ctxA, cancelA := Cancelable()
cancelA()
ctxB := New()
j1, _ := Join(ctxA, ctxB)
select {
case <-j1.Done():
default:
t.Fatal("expected joined context to be done immediately when A is already done")
}
if !errors.Is(j1.Err(), context.Canceled) {
t.Errorf("expected context.Canceled, got %v", j1.Err())
}
})
t.Run("b already done", func(t *testing.T) {
ctxC := New()
ctxD, cancelD := Cancelable()
cancelD()
j2, _ := Join(ctxC, ctxD)
select {
case <-j2.Done():
default:
t.Fatal("expected joined context to be done immediately when B is already done")
}
if !errors.Is(j2.Err(), context.Canceled) {
t.Errorf("expected context.Canceled, got %v", j2.Err())
}
})
}
func TestJoin_Value(t *testing.T) {
t.Parallel()
type key string
ctxA := WithValue(New(), key("k1"), "v1")
ctxB := WithValue(New(), key("k2"), "v2")
ctxBConflicting := WithValue(ctxB, key("k1"), "v1-b")
j1, cancel1 := Join(ctxA, ctxB)
defer cancel1()
if v := j1.Value(key("k1")); v != "v1" {
t.Errorf("expected v1, got %v", v)
}
if v := j1.Value(key("k2")); v != "v2" {
t.Errorf("expected v2, got %v", v)
}
if v := j1.Value(key("missing")); v != nil {
t.Errorf("expected nil, got %v", v)
}
j2, cancel2 := Join(ctxA, ctxBConflicting)
defer cancel2()
// A must take precedence over B
if v := j2.Value(key("k1")); v != "v1" {
t.Errorf("expected v1 (from ctxA), got %v", v)
}
}
func TestJoin_Deadline(t *testing.T) {
t.Parallel()
now := time.Now()
ctxNoDeadline := New()
ctxEarly, cancelEarly := Deadline(now.Add(1 * time.Hour))
defer cancelEarly()
ctxLate, cancelLate := Deadline(now.Add(2 * time.Hour))
defer cancelLate()
tests := []struct {
name string
a context.Context
b context.Context
wantDeadline time.Time
wantOk bool
}{
{"no deadlines", ctxNoDeadline, ctxNoDeadline, time.Time{}, false},
{"only a has deadline", ctxEarly, ctxNoDeadline, now.Add(1 * time.Hour), true},
{"only b has deadline", ctxNoDeadline, ctxEarly, now.Add(1 * time.Hour), true},
{"both have deadlines, a is earlier", ctxEarly, ctxLate, now.Add(1 * time.Hour), true},
{"both have deadlines, b is earlier", ctxLate, ctxEarly, now.Add(1 * time.Hour), true},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
j, cancel := Join(tt.a, tt.b)
defer cancel()
gotDeadline, gotOk := j.Deadline()
if gotOk != tt.wantOk {
t.Errorf("Deadline() ok = %v, want %v", gotOk, tt.wantOk)
}
if gotOk && !gotDeadline.Equal(tt.wantDeadline) {
t.Errorf("Deadline() = %v, want %v", gotDeadline, tt.wantDeadline)
}
})
}
}