forked from cshum/imagor
-
Notifications
You must be signed in to change notification settings - Fork 1
/
context_test.go
51 lines (48 loc) · 1.25 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
package imagor
import (
"context"
"github.com/stretchr/testify/assert"
"testing"
"time"
)
func TestDefer(t *testing.T) {
var called int
ctx, cancel := context.WithCancel(context.Background())
assert.Panics(t, func() {
contextDefer(ctx, func() {
t.Fatal("should not call")
})
})
ctx = withContext(ctx)
contextDefer(ctx, func() {
called++
})
contextDefer(ctx, func() {
called++
})
cancel()
assert.Equal(t, 0, called, "should call after signal")
time.Sleep(time.Millisecond * 10)
contextDefer(ctx, func() {
called++
})
assert.Equal(t, 2, called, "should count all defers before cancel")
}
func TestDetachContext(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), time.Nanosecond)
defer cancel()
ctx = context.WithValue(ctx, "foo", "bar")
assert.False(t, isDetached(ctx))
time.Sleep(time.Millisecond)
assert.Equal(t, ctx.Err(), context.DeadlineExceeded)
ctx = detachContext(ctx)
assert.True(t, isDetached(ctx))
assert.Equal(t, "bar", ctx.Value("foo"))
assert.NoError(t, ctx.Err())
ctx, cancel2 := context.WithTimeout(ctx, time.Millisecond*5)
defer cancel2()
assert.NoError(t, ctx.Err())
assert.True(t, isDetached(ctx))
time.Sleep(time.Millisecond * 10)
assert.Equal(t, ctx.Err(), context.DeadlineExceeded)
}