-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathpipe_test.go
More file actions
107 lines (86 loc) · 1.52 KB
/
pipe_test.go
File metadata and controls
107 lines (86 loc) · 1.52 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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
package goob_test
import (
"context"
"sync"
"testing"
"time"
"github.com/ysmood/goob"
)
func TestPipeOrder(t *testing.T) {
checkLeak(t)
ctx, cancel := context.WithCancel(context.Background())
t.Cleanup(cancel)
write, events := goob.NewPipe(ctx)
write(1)
write(2)
write(3)
if 1 != <-events {
t.Fatal()
}
if 2 != <-events {
t.Fatal()
}
if 3 != <-events {
t.Fatal()
}
}
func TestPipe(t *testing.T) {
checkLeak(t)
const pipeCount = 10
const msgCount = 10
round := func() {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
write, events := goob.NewPipe(ctx)
wg := sync.WaitGroup{}
wg.Add(msgCount)
for i := 0; i < msgCount*2; i++ {
if i%2 == 0 {
go write(i)
} else {
go func() {
<-events
wg.Done()
}()
}
}
wg.Wait()
}
wg := sync.WaitGroup{}
wg.Add(pipeCount)
for i := 0; i < pipeCount; i++ {
go func() {
round()
wg.Done()
}()
}
wg.Wait()
}
func TestPipeCancel(t *testing.T) {
checkLeak(t)
const count = 1000
for i := 0; i < count; i++ {
ctx, cancel := context.WithCancel(context.Background())
write, _ := goob.NewPipe(ctx)
go write(1)
go cancel()
}
}
func TestPipeMonkey(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
t.Cleanup(cancel)
write, events := goob.NewPipe(ctx)
round := 30
count := 10000
for i := 0; i < round; i++ {
go func() {
for i := 0; i < count; i++ {
write(i)
}
}()
}
for i := 0; i < count*round; i++ {
time.Sleep(100 * time.Nanosecond)
<-events
}
}