-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgroup_test.go
85 lines (70 loc) · 1.67 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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
package srvc_test
import (
"errors"
"fmt"
"testing"
"github.com/go-srvc/srvc"
)
func TestErrGroup_MultipleErrors(t *testing.T) {
err1 := errors.New("err1")
err2 := errors.New("err2")
eg := &srvc.ErrGroup{}
eg.Go(func() error { return err1 })
eg.Go(func() error { return err2 })
err := eg.Wait()
errorIs(t, err, err1)
errorIs(t, err, err2)
}
func TestErrGroup_Mixed(t *testing.T) {
err1 := errors.New("err1")
err2 := errors.New("err2")
eg := &srvc.ErrGroup{}
eg.Go(func() error { return nil })
eg.Go(func() error { return err1 })
eg.Go(func() error { return nil })
eg.Go(func() error { return err2 })
eg.Go(func() error { return nil })
err := eg.Wait()
errorIs(t, err, err1)
errorIs(t, err, err2)
}
func TestErrGroup_NoError(t *testing.T) {
eg := &srvc.ErrGroup{}
eg.Go(func() error { return nil })
eg.Go(func() error { return nil })
eg.Go(func() error { return nil })
err := eg.Wait()
noError(t, err)
}
func TestErrGroup_NoTask(t *testing.T) {
eg := &srvc.ErrGroup{}
err := eg.Wait()
noError(t, err)
}
func TestError_Error(t *testing.T) {
const (
msg = "test error"
SomeErr = srvc.ErrStr(msg)
)
err := fmt.Errorf("more info: %w", SomeErr)
errorIs(t, err, SomeErr)
equal(t, msg, errors.Unwrap(err).Error())
}
func errorIs(t *testing.T, err, target error) {
t.Helper()
if !errors.Is(err, target) {
t.Errorf("expected to found error %v from %v", target, err)
}
}
func noError(t *testing.T, err error) {
t.Helper()
if err != nil {
t.Errorf("expected no error but got %v", err)
}
}
func equal[C comparable](t *testing.T, expected, actual C) {
t.Helper()
if expected != actual {
t.Errorf("expected %v but got %v", expected, actual)
}
}