-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgoroutine_test.go
More file actions
65 lines (57 loc) · 1.09 KB
/
goroutine_test.go
File metadata and controls
65 lines (57 loc) · 1.09 KB
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
package goroutinetimeout_test
import (
"fmt"
"sync"
"testing"
"time"
"github.com/coscms/goroutinetimeout"
"golang.org/x/net/context"
)
func testBase(t *testing.T, asyncPush bool) {
wg := sync.WaitGroup{}
ctx := context.Background()
queue := make(chan interface{}, 5)
push := func(i int) {
wg.Add(i)
for _i := 0; _i < i; _i++ {
queue <- _i
}
close(queue)
}
g := goroutinetimeout.New(`TestChan`, 4)
f := func(v interface{}) {
i := v.(int)
time.Sleep(2 * time.Second)
t.Logf(`Execute.%d`, i)
wg.Done()
}
if !asyncPush {
// 1.
go g.ExecuteWithChan(ctx, queue, f)
push(10)
} else {
// 2.
go push(10)
g.ExecuteWithChan(ctx, queue, f)
}
wg.Wait()
}
func TestBaseAsyncPush(t *testing.T) {
testBase(t, true)
}
func TestBaseSyncPush(t *testing.T) {
testBase(t, false)
}
func TestTimeout(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), time.Second*3)
defer cancel()
var i int32
g := goroutinetimeout.New(`TestTimeout`)
g.Execute(ctx, func() {
time.Sleep(time.Second * 4)
i++
})
if i > 1 {
panic(fmt.Sprintf(`i > 1 (%d)`, i))
}
}