-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontext_test.go
59 lines (46 loc) · 1.2 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
package just_test
import (
"context"
"testing"
"time"
"github.com/kazhuravlev/just"
"github.com/stretchr/testify/assert"
)
func TestContextWithTimeout(t *testing.T) {
t.Parallel()
fn := func(ctx context.Context) error {
select {
case <-ctx.Done():
return ctx.Err()
case <-time.After(time.Second):
return nil
}
}
err := just.ContextWithTimeout(context.Background(), time.Millisecond, fn)
assert.ErrorIs(t, context.DeadlineExceeded, err)
fn2 := func(ctx context.Context) error {
return ctx.Err()
}
err2 := just.ContextWithTimeout(context.Background(), time.Millisecond, fn2)
assert.NoError(t, err2)
}
func TestContextWithTimeout2(t *testing.T) {
t.Parallel()
fn := func(ctx context.Context) (int, error) {
select {
case <-ctx.Done():
return 10, ctx.Err()
case <-time.After(time.Second):
return 20, nil
}
}
r, err := just.ContextWithTimeout2(context.Background(), time.Millisecond, fn)
assert.ErrorIs(t, context.DeadlineExceeded, err)
assert.Equal(t, 10, r)
fn2 := func(ctx context.Context) (int, error) {
return 42, ctx.Err()
}
r, err2 := just.ContextWithTimeout2(context.Background(), time.Millisecond, fn2)
assert.NoError(t, err2)
assert.Equal(t, 42, r)
}