-
Notifications
You must be signed in to change notification settings - Fork 0
/
trap_test.go
52 lines (46 loc) · 1.05 KB
/
trap_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
package supervisor_test
import (
"context"
"errors"
"fmt"
"github.com/akaspin/supervisor"
"github.com/stretchr/testify/assert"
"strconv"
"testing"
)
func TestTrap_Wait(t *testing.T) {
for i := 0; i < compositeTestIterations; i++ {
t.Run("with error "+strconv.Itoa(i), func(t *testing.T) {
t.Parallel()
trap := supervisor.NewTrap(context.Background())
trap.Open()
go func() {
assert.EqualError(t, trap.Wait(), "bang")
}()
trap.Trap(errors.New("bang"))
assert.EqualError(t, trap.Wait(), "bang")
})
t.Run("no error "+strconv.Itoa(i), func(t *testing.T) {
t.Parallel()
trap := supervisor.NewTrap(context.Background())
trap.Open()
go func() {
assert.NoError(t, trap.Wait())
}()
trap.Close()
assert.NoError(t, trap.Wait())
})
}
}
func ExampleTrap_Wait() {
trap := supervisor.NewTrap(context.Background())
trap.Open()
go func() {
if err := trap.Wait(); err != nil && err.Error() != "bang" {
fmt.Println(trap.Wait())
}
}()
trap.Trap(errors.New("bang"))
fmt.Println(trap.Wait())
// Output: bang
}