-
Notifications
You must be signed in to change notification settings - Fork 6
/
ctx_test.go
76 lines (66 loc) · 1.29 KB
/
ctx_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
// SPDX-FileCopyrightText: 2021 Henry Bubert
//
// SPDX-License-Identifier: MIT
package muxrpc
import (
"context"
"fmt"
"strings"
"testing"
"time"
"github.com/ssbc/go-luigi"
)
func TestCloseContext(t *testing.T) {
type testcase struct {
closes []string
expErr error
}
tcs := []testcase{
{
closes: []string{"cls"},
expErr: luigi.EOS{},
},
{
closes: []string{"cancel"},
expErr: context.Canceled,
},
{
closes: []string{"cls", "cancel"},
expErr: luigi.EOS{},
},
{
closes: []string{"cancel", "cls"},
expErr: context.Canceled,
},
{
expErr: nil,
},
}
mkTest := func(tc testcase) func(*testing.T) {
return func(t *testing.T) {
ctx := context.Background()
ctx, cancel := context.WithCancel(ctx)
defer cancel()
ctx, cls := withError(ctx, luigi.EOS{})
defer cls()
for _, op := range tc.closes {
switch op {
case "cls":
cls()
case "cancel":
cancel()
default:
t.Error("unexpected element in closes:", op)
}
// give other goroutine some time
time.Sleep(time.Millisecond)
}
if ctx.Err() != tc.expErr {
t.Errorf("error mismatch: expected %q, got: %v", tc.expErr, ctx.Err())
}
}
}
for i, tc := range tcs {
t.Run(fmt.Sprintf("%d-%s", i, strings.Join(tc.closes, ",")), mkTest(tc))
}
}