-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
race_test.go
49 lines (44 loc) · 815 Bytes
/
race_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
package flowmatic_test
import (
"context"
"errors"
"testing"
"time"
"github.com/carlmjohnson/flowmatic"
)
func sleepFor(ctx context.Context, d time.Duration) bool {
timer := time.NewTimer(d)
defer timer.Stop()
select {
case <-timer.C:
return true
case <-ctx.Done():
return false
}
}
func TestRace_join_errs(t *testing.T) {
var (
a = errors.New("a")
b = errors.New("b")
)
err := flowmatic.Race(context.Background(),
func(ctx context.Context) error {
if !sleepFor(ctx, 10*time.Millisecond) {
return ctx.Err()
}
return a
},
func(ctx context.Context) error {
if !sleepFor(ctx, 30*time.Millisecond) {
return ctx.Err()
}
return b
},
)
if !errors.Is(err, a) || !errors.Is(err, b) {
t.Fatal(err)
}
if errors.Is(err, context.Canceled) {
t.Fatal(err)
}
}