-
Notifications
You must be signed in to change notification settings - Fork 0
/
ctxgroup_test.go
50 lines (45 loc) · 1.13 KB
/
ctxgroup_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
package ctxgroup_test
import (
"context"
"errors"
"testing"
"github.com/pyr-sh/ctxgroup"
)
func TestContextCancellation(t *testing.T) {
ctx := context.Background()
g := ctxgroup.WithContext(ctx)
g.GoCtx(func(ctx context.Context) error {
<-ctx.Done()
return ctx.Err()
})
g.GoCtx(func(ctx context.Context) error {
return errors.New("hello world")
})
err := g.Wait()
if err == nil || err.Error() != "hello world" {
t.Fatalf(`expected the error to be "hello world", got %v (%T)`, err, err)
}
}
func TestContextAlreadyCancelled(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
g := ctxgroup.WithContext(ctx)
cancel()
g.GoCtx(func(ctx context.Context) error {
<-ctx.Done()
return ctx.Err()
})
err := g.Wait()
if err == nil || err != context.Canceled {
t.Fatalf(`expected the error to be "context canceled", got %v (%T)`, err, err)
}
}
func TestWaitErrorOnFunctionSuccess(t *testing.T) {
ctx := context.Background()
g := ctxgroup.WithContext(ctx)
g.GoCtx(func(ctx context.Context) error {
return nil
})
if err := g.Wait(); err != nil {
t.Fatalf("expected error to be nil, got %v", err)
}
}