-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontext_test.go
86 lines (73 loc) · 2.34 KB
/
context_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
85
86
package cli
import (
"context"
"syscall"
"testing"
"gotest.tools/v3/assert"
)
func Test_ctxCommand(t *testing.T) {
{ // check context setup
ctx := NewCommandContext(context.Background())
value := ctx.Value(ctxKeyCommand)
assert.Check(t, value != nil)
}
{ // check setting and getting values
{ // unprepared context
ctx := context.Background()
SetInitializedFlagsInContext(ctx, []Flag{nil, nil}, []Flag{nil, nil})
local, persistent := GetInitializedFlagsFromContext(ctx)
assert.Check(t, local == nil)
assert.Check(t, persistent == nil)
}
{ // prepared context
ctx := NewCommandContext(context.Background())
SetInitializedFlagsInContext(ctx, []Flag{nil, nil}, []Flag{nil})
local, persistent := GetInitializedFlagsFromContext(ctx)
assert.Check(t, len(local) == 2)
assert.Check(t, len(persistent) == 1)
}
}
}
func Test_ctxMetadata(t *testing.T) {
{ // check context setup
ctx := NewContextWithMetadata(context.Background())
value := ctx.Value(ctxKeyMetadata)
assert.Check(t, value != nil)
}
{ // check setting and getting values
{ // unprepared context
ctx := context.Background()
SetMetadataInContext(ctx, "key", "value")
assert.Check(t, GetMetadataFromContext(ctx, "key") == nil)
}
{ // prepared context
ctx := NewContextWithMetadata(context.Background())
SetMetadataInContext(ctx, "key", "value")
assert.Check(t, GetMetadataFromContext(ctx, "key").(string) == "value")
}
}
}
func Test_NewContextCancelableBySignal(t *testing.T) {
t.Run("calling cancel func cancels the context", func(t *testing.T) {
ctx, cancel := NewContextCancelableBySignal(syscall.SIGUSR1)
assert.NilError(t, ctx.Err())
cancel()
<-ctx.Done()
assert.ErrorIs(t, ctx.Err(), context.Canceled)
})
t.Run("sending provided signal cancels the context", func(t *testing.T) {
ctx, cancel := NewContextCancelableBySignal(syscall.SIGUSR1)
defer cancel()
assert.NilError(t, ctx.Err())
assert.NilError(t, syscall.Kill(syscall.Getpid(), syscall.SIGUSR1))
<-ctx.Done()
assert.ErrorIs(t, ctx.Err(), context.Canceled)
})
t.Run("sending unknown signal keeps context intact", func(t *testing.T) {
ctx, cancel := NewContextCancelableBySignal(syscall.SIGUSR1)
defer cancel()
assert.NilError(t, ctx.Err())
assert.NilError(t, syscall.Kill(syscall.Getpid(), syscall.SIGUSR2))
assert.NilError(t, ctx.Err())
})
}