-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdetach_test.go
45 lines (39 loc) · 1.23 KB
/
detach_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
package ctxz_test
import (
"context"
"testing"
"github.com/stretchr/testify/assert"
"github.com/wqyjh/go-ctxz"
)
func TestWithoutCancel(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
ctx2 := context.WithValue(ctx, "key", "value")
ctx3 := ctxz.WithoutCancel(ctx2)
cancel()
assert.ErrorIs(t, context.Canceled, ctx.Err())
assert.ErrorIs(t, context.Canceled, ctx2.Err())
assert.NoError(t, ctx3.Err())
assert.Equal(t, "value", ctx3.Value("key"))
ctx4 := ctxz.WithoutCancel(ctx3)
assert.NotEqual(t, ctx2, ctx3)
assert.Equal(t, ctx3, ctx4)
}
func TestWithNewCancel(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
ctx2 := context.WithValue(ctx, "key", "value")
ctx3, cancel3 := ctxz.WithNewCancel(ctx2)
cancel()
assert.ErrorIs(t, context.Canceled, ctx.Err())
assert.NoError(t, ctx3.Err())
cancel3()
assert.ErrorIs(t, context.Canceled, ctx3.Err())
assert.Equal(t, "value", ctx3.Value("key"))
ctx, cancel = context.WithCancel(context.Background())
ctx2 = context.WithValue(ctx, "key", "value")
ctx3, cancel3 = ctxz.WithNewCancel(ctx2)
cancel3()
assert.ErrorIs(t, context.Canceled, ctx3.Err())
assert.NoError(t, ctx.Err())
cancel()
assert.ErrorIs(t, context.Canceled, ctx.Err())
}