-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path05_split_test.go
More file actions
82 lines (70 loc) · 1.38 KB
/
05_split_test.go
File metadata and controls
82 lines (70 loc) · 1.38 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
package go_channel__test
import (
"testing"
"time"
)
func split(from chan string) (chan string, chan string) {
to1 := make(chan string, cap(from))
to2 := make(chan string, cap(from))
go func() {
defer close(to1)
defer close(to2)
for data := range from {
to1 <- data
to2 <- data
}
}()
return to1, to2
}
// read c1 value "string 01"
// read c1 value "string 02"
// channel c has 2/5 items
// channel c1 has 0/5 items
// read c2 value "string 01"
// read c2 value "string 02"
// channel c2 has 1/5 items
// c1 closed
// c2 closed
// finish
func Test_split(t *testing.T) {
c := make(chan string, 5)
c1, c2 := split(c)
go func() {
for s := range c1 {
t.Logf("read c1 value \"%s\"\n", s)
}
t.Log("c1 closed")
}()
go func() {
for s := range c2 {
t.Logf("read c2 value \"%s\"\n", s)
}
t.Log("c2 closed")
}()
c <- "string 01"
c <- "string 02"
t.Logf("channel c has %d/%d items\n", len(c), cap(c))
t.Logf("channel c1 has %d/%d items\n", len(c1), cap(c1))
t.Logf("channel c2 has %d/%d items\n", len(c2), cap(c2))
close(c)
time.Sleep(100 * time.Millisecond)
t.Log("finish")
}
func Benchmark_Split(b *testing.B) {
source := make(chan string, 3)
out1, out2 := split(source)
go func() {
for _ = range out1 {
}
}()
go func() {
for _ = range out2 {
}
}()
b.ReportAllocs()
b.ResetTimer()
for n := 0; n < b.N; n++ {
source <- "aaa"
}
close(source)
}