| 1 |
package contextx_test |
| 2 |
|
| 3 |
import ( |
| 4 |
"context" |
| 5 |
"errors" |
| 6 |
"testing" |
| 7 |
"time" |
| 8 |
|
| 9 |
"github.com/powerman/check" |
| 10 |
"github.com/powerman/check/internal/contextx" |
| 11 |
) |
| 12 |
|
| 13 |
type ctxKey string |
| 14 |
|
| 15 |
var errCause = errors.New("cause") |
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
func closed(ctx context.Context) bool { |
| 21 |
select { |
| 22 |
case <-ctx.Done(): |
| 23 |
return true |
| 24 |
case <-time.After(time.Second): |
| 25 |
return false |
| 26 |
} |
| 27 |
} |
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
func open(ctx context.Context) bool { |
| 32 |
select { |
| 33 |
case <-ctx.Done(): |
| 34 |
return false |
| 35 |
case <-time.After(time.Millisecond): |
| 36 |
return true |
| 37 |
} |
| 38 |
} |
| 39 |
|
| 40 |
func TestMergeValues(tt *testing.T) { |
| 41 |
tt.Parallel() |
| 42 |
t := check.T(tt) |
| 43 |
|
| 44 |
parent := context.WithValue(context.Background(), ctxKey("a"), "parent-a") |
| 45 |
parent = context.WithValue(parent, ctxKey("shared"), "parent-shared") |
| 46 |
extra := context.WithValue(context.Background(), ctxKey("b"), "extra-b") |
| 47 |
extra = context.WithValue(extra, ctxKey("shared"), "extra-shared") |
| 48 |
|
| 49 |
ctx := contextx.MergeValues(parent, extra) |
| 50 |
|
| 51 |
t.Equal(ctx.Value(ctxKey("a")), "parent-a") |
| 52 |
t.Equal(ctx.Value(ctxKey("b")), "extra-b") |
| 53 |
t.Equal(ctx.Value(ctxKey("shared")), "parent-shared") |
| 54 |
t.Nil(ctx.Value(ctxKey("missing"))) |
| 55 |
} |
| 56 |
|
| 57 |
func TestMergeValues_cancellation_from_parent_only(tt *testing.T) { |
| 58 |
tt.Parallel() |
| 59 |
t := check.T(tt) |
| 60 |
|
| 61 |
parentCtx, cancelParent := context.WithCancel(context.Background()) |
| 62 |
tt.Cleanup(cancelParent) |
| 63 |
extraCtx, cancelExtra := context.WithCancel(context.Background()) |
| 64 |
tt.Cleanup(cancelExtra) |
| 65 |
|
| 66 |
ctx := contextx.MergeValues(parentCtx, extraCtx) |
| 67 |
|
| 68 |
cancelExtra() |
| 69 |
t.True(open(ctx)) |
| 70 |
|
| 71 |
cancelParent() |
| 72 |
t.True(closed(ctx)) |
| 73 |
t.Err(ctx.Err(), context.Canceled) |
| 74 |
} |
| 75 |
|
| 76 |
func TestMergeCancel_via_parent(tt *testing.T) { |
| 77 |
tt.Parallel() |
| 78 |
t := check.T(tt) |
| 79 |
|
| 80 |
parent, cancelParent := context.WithCancel(context.Background()) |
| 81 |
tt.Cleanup(cancelParent) |
| 82 |
|
| 83 |
|
| 84 |
ctx, cancel := contextx.MergeCancel(parent, context.Background()) |
| 85 |
tt.Cleanup(cancel) |
| 86 |
|
| 87 |
t.True(open(ctx)) |
| 88 |
cancelParent() |
| 89 |
t.True(closed(ctx)) |
| 90 |
t.Err(ctx.Err(), context.Canceled) |
| 91 |
} |
| 92 |
|
| 93 |
func TestMergeCancel_via_extra(tt *testing.T) { |
| 94 |
tt.Parallel() |
| 95 |
t := check.T(tt) |
| 96 |
|
| 97 |
extra, cancelExtra := context.WithCancelCause(context.Background()) |
| 98 |
tt.Cleanup(func() { cancelExtra(context.Canceled) }) |
| 99 |
|
| 100 |
ctx, cancel := contextx.MergeCancel(context.Background(), extra) |
| 101 |
tt.Cleanup(cancel) |
| 102 |
|
| 103 |
t.True(open(ctx)) |
| 104 |
cancelExtra(errCause) |
| 105 |
t.True(closed(ctx)) |
| 106 |
t.Err(ctx.Err(), context.Canceled) |
| 107 |
t.Err(context.Cause(ctx), errCause) |
| 108 |
} |
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
func TestMergeCancel_via_func(tt *testing.T) { |
| 115 |
tt.Parallel() |
| 116 |
t := check.T(tt) |
| 117 |
|
| 118 |
ctx, cancel := contextx.MergeCancel(context.Background(), context.Background()) |
| 119 |
|
| 120 |
t.True(open(ctx)) |
| 121 |
cancel() |
| 122 |
t.True(closed(ctx)) |
| 123 |
t.Err(ctx.Err(), context.Canceled) |
| 124 |
} |
| 125 |
|
| 126 |
func TestMergeCancel_values_from_parent_only(tt *testing.T) { |
| 127 |
tt.Parallel() |
| 128 |
t := check.T(tt) |
| 129 |
|
| 130 |
parent := context.WithValue(context.Background(), ctxKey("a"), "parent-a") |
| 131 |
extra := context.WithValue(context.Background(), ctxKey("b"), "extra-b") |
| 132 |
|
| 133 |
ctx, cancel := contextx.MergeCancel(parent, extra) |
| 134 |
tt.Cleanup(cancel) |
| 135 |
|
| 136 |
t.Equal(ctx.Value(ctxKey("a")), "parent-a") |
| 137 |
t.Nil(ctx.Value(ctxKey("b"))) |
| 138 |
} |
| 139 |
|
| 140 |
func TestMergeCancel_deadline(tt *testing.T) { |
| 141 |
tt.Parallel() |
| 142 |
|
| 143 |
base := time.Now() |
| 144 |
early := base.Add(time.Minute) |
| 145 |
late := base.Add(time.Hour) |
| 146 |
|
| 147 |
at := func(tt *testing.T, deadline time.Time) context.Context { |
| 148 |
tt.Helper() |
| 149 |
ctx, cancel := context.WithDeadline(context.Background(), deadline) |
| 150 |
tt.Cleanup(cancel) |
| 151 |
return ctx |
| 152 |
} |
| 153 |
mk := func(tt *testing.T, deadline *time.Time) context.Context { |
| 154 |
tt.Helper() |
| 155 |
if deadline == nil { |
| 156 |
return context.Background() |
| 157 |
} |
| 158 |
return at(tt, *deadline) |
| 159 |
} |
| 160 |
|
| 161 |
testCases := []struct { |
| 162 |
name string |
| 163 |
parent, extra *time.Time |
| 164 |
want *time.Time |
| 165 |
}{ |
| 166 |
{"neither", nil, nil, nil}, |
| 167 |
{"parent-only", &early, nil, &early}, |
| 168 |
{"extra-only", nil, &early, &early}, |
| 169 |
{"parent-earlier", &early, &late, &early}, |
| 170 |
{"extra-earlier", &late, &early, &early}, |
| 171 |
} |
| 172 |
for _, tc := range testCases { |
| 173 |
tt.Run(tc.name, func(tt *testing.T) { |
| 174 |
tt.Parallel() |
| 175 |
t := check.T(tt) |
| 176 |
|
| 177 |
ctx, cancel := contextx.MergeCancel(mk(tt, tc.parent), mk(tt, tc.extra)) |
| 178 |
tt.Cleanup(cancel) |
| 179 |
|
| 180 |
deadline, ok := ctx.Deadline() |
| 181 |
t.Equal(ok, tc.want != nil) |
| 182 |
if tc.want != nil { |
| 183 |
t.True(deadline.Equal(*tc.want)) |
| 184 |
} |
| 185 |
}) |
| 186 |
} |
| 187 |
} |
| 188 |
|
| 189 |
func TestMerge(tt *testing.T) { |
| 190 |
tt.Parallel() |
| 191 |
t := check.T(tt) |
| 192 |
|
| 193 |
parent := context.WithValue(context.Background(), ctxKey("a"), "parent-a") |
| 194 |
parent = context.WithValue(parent, ctxKey("shared"), "parent-shared") |
| 195 |
extra := context.WithValue(context.Background(), ctxKey("b"), "extra-b") |
| 196 |
extra = context.WithValue(extra, ctxKey("shared"), "extra-shared") |
| 197 |
|
| 198 |
parentCtx, cancelParent := context.WithCancel(parent) |
| 199 |
tt.Cleanup(cancelParent) |
| 200 |
extraCtx, cancelExtra := context.WithCancel(extra) |
| 201 |
tt.Cleanup(cancelExtra) |
| 202 |
|
| 203 |
ctx, cancel := contextx.Merge(parentCtx, extraCtx) |
| 204 |
tt.Cleanup(cancel) |
| 205 |
|
| 206 |
|
| 207 |
t.Equal(ctx.Value(ctxKey("a")), "parent-a") |
| 208 |
t.Equal(ctx.Value(ctxKey("b")), "extra-b") |
| 209 |
t.Equal(ctx.Value(ctxKey("shared")), "parent-shared") |
| 210 |
|
| 211 |
|
| 212 |
t.True(open(ctx)) |
| 213 |
cancelExtra() |
| 214 |
t.True(closed(ctx)) |
| 215 |
t.Err(ctx.Err(), context.Canceled) |
| 216 |
} |