-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patherror_test.go
More file actions
64 lines (60 loc) · 1016 Bytes
/
error_test.go
File metadata and controls
64 lines (60 loc) · 1016 Bytes
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
package once
import (
"fmt"
"testing"
"time"
"github.com/stretchr/testify/assert"
)
func TestError(t *testing.T) {
cases := []struct {
f func() error
e string
}{
{
f: func() error {
panic("hi, I'm a panic")
},
e: "I'm a panic",
},
{
f: func() error {
panic(fmt.Errorf("I'm an error and a panic"))
},
e: "I'm an error and a panic",
},
{
f: func() error {
return nil
},
e: "",
},
{
f: func() error {
return fmt.Errorf("a regular error")
},
e: "a regular error",
},
}
for _, tc := range cases {
c := make(chan error)
go func() {
var err error
e := ReliableError(c, &err)
defer e.Catch()
err = tc.f()
e.Do()
}()
timer := time.NewTimer(time.Second)
select {
case err := <-c:
if tc.e == "" {
assert.NoError(t, err, "no error expected")
} else if assert.Error(t, err, tc.e) {
assert.Contains(t, err.Error(), tc.e)
}
timer.Stop()
case <-timer.C:
t.Errorf("timeout for %s", tc.e)
}
}
}