-
Notifications
You must be signed in to change notification settings - Fork 0
/
tam_test.go
81 lines (75 loc) · 1.59 KB
/
tam_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
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at https://mozilla.org/MPL/2.0/.
package ctxroutines
import (
"context"
"errors"
"testing"
)
func TestTryAtMost1OK(t *testing.T) {
ch := make(chan bool, 10)
f := CTXRunner(func(c context.Context) error { ch <- true; return nil })
r := TryAtMost(10, f)
err := r.Run()
if err != nil {
t.Fatal("unexpected error:", err)
}
if l := len(ch); l != 1 {
t.Fatalf("expected 1 item, got %d", l)
}
}
func TestTryAtMost1OK2Fail(t *testing.T) {
e := errors.New("")
ch := make(chan bool, 10)
f := Counter(func(n uint64) error {
ch <- true
if n < 2 {
return e
}
return nil
})
r := TryAtMost(10, f)
err := r.Run()
if err != nil {
t.Fatal("unexpected error:", err)
}
if l := len(ch); l != 3 {
t.Fatalf("expected 3 items, got %d", l)
}
}
func TestTryAtMost1OK9Fail(t *testing.T) {
e := errors.New("")
ch := make(chan bool, 10)
f := Counter(func(n uint64) error {
ch <- true
if n < 9 {
return e
}
return nil
})
r := TryAtMost(10, f)
err := r.Run()
if err != nil {
t.Fatal("unexpected error:", err)
}
if l := len(ch); l != 10 {
t.Fatalf("expected 10 items, got %d", l)
}
}
func TestTryAtMostFail(t *testing.T) {
e := errors.New("")
ch := make(chan bool, 11)
f := CTXRunner(func(c context.Context) error {
ch <- true
return e
})
r := TryAtMost(10, f)
err := r.Run()
if err != e {
t.Fatal("unexpected error:", err)
}
if l := len(ch); l != 10 {
t.Fatalf("expected 10 items, got %d", l)
}
}