-
Notifications
You must be signed in to change notification settings - Fork 0
/
group_test.go
61 lines (50 loc) · 1.14 KB
/
group_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
60
61
package relax
import (
"context"
"errors"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestGroup_CancelParentContext_ChildContextDone(t *testing.T) {
// Root context to cancel
ctx, cancel := context.WithCancel(context.Background())
// Group to wait on root and group context Done()
e, gCtx := NewGroup(ctx)
e.Go(func() error {
<-ctx.Done()
<-gCtx.Done()
return nil
})
// Cancel root context
cancel()
// Validate
assert.NoError(t, e.Wait())
}
func TestGroup_Panic_Error(t *testing.T) {
// Group
e, ctx := NewGroup(context.Background())
// Routine that panics
panicMsg := "test panic"
e.Go(func() error {
panic(panicMsg)
})
// Sibling routine that blocks on Group context
e.Go(func() error {
<-ctx.Done()
return nil
})
// Wait for all goroutines
err := e.Wait()
require.Error(t, err)
// Verify panic message/error is returned
assert.Contains(t, err.Error(), panicMsg)
assert.True(t, errors.Is(err, PanicError))
}
func TestGroup_NoPanic_NoError(t *testing.T) {
e, _ := NewGroup(context.Background())
e.Go(func() error {
return nil
})
assert.NoError(t, e.Wait())
}