-
Notifications
You must be signed in to change notification settings - Fork 1
/
ctx_test.go
47 lines (40 loc) · 1.09 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
package kutils
import (
"context"
"testing"
"time"
"github.com/stretchr/testify/assert"
)
func TestCtxWithoutCancel(t *testing.T) {
// nolint:staticcheck
cancelledCtx, cancel := context.WithTimeout(
context.WithValue(CtxWithoutCancel(nil), "key", "value"), time.Second)
detachedCtx := CtxWithoutCancel(cancelledCtx)
t.Run("Deadline()", func(t *testing.T) {
_, ok := cancelledCtx.Deadline()
assert.True(t, ok, "cancelledCtx should have Deadline")
_, ok = detachedCtx.Deadline()
assert.False(t, ok, "detachedCtx should not have Deadline")
cancel()
})
t.Run("Err()", func(t *testing.T) {
assert.NotNil(t, cancelledCtx.Err())
assert.Nil(t, detachedCtx.Err())
})
t.Run("Done()", func(t *testing.T) {
select {
case <-cancelledCtx.Done():
default:
assert.Fail(t, "cancelledCtx.Done() should be closed")
}
select {
case <-detachedCtx.Done():
assert.Fail(t, "detachedCtx.Done() should not be closed")
default:
}
})
t.Run("Value()", func(t *testing.T) {
assert.Equal(t, "value", cancelledCtx.Value("key"))
assert.Equal(t, "value", detachedCtx.Value("key"))
})
}