-
Notifications
You must be signed in to change notification settings - Fork 0
/
chan_test.go
102 lines (83 loc) · 2.64 KB
/
chan_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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
package slice
import (
"context"
"fmt"
"github.com/stretchr/testify/assert"
"testing"
)
// ------------------------------------------------- ReadFromChannel ---------------------------------------------------
func TestReadFromChannel(t *testing.T) {
channel := make(chan int, 10)
for i := 0; i < 3; i++ {
channel <- i
}
close(channel)
slice := ReadFromChannel(context.Background(), channel)
exceptedSlice := []int{0, 1, 2}
assert.Equal(t, exceptedSlice, slice)
}
func ExampleReadFromChannel() {
channel := make(chan int, 10)
for i := 0; i < 3; i++ {
channel <- i
}
close(channel)
slice := ReadFromChannel(context.Background(), channel)
fmt.Println(slice)
// Output:
// [0 1 2]
}
// ------------------------------------------------- SendToChannel -----------------------------------------------------
func TestSendToChannel(t *testing.T) {
// 发送到正常的channel
channel := make(chan int, 10)
slice := []int{0, 1, 2}
sendSuccessCount := SendToChannel(context.Background(), slice, channel)
assert.Equal(t, 3, sendSuccessCount)
// 发送到关闭的channel
close(channel)
sendSuccessCount = SendToChannel(context.Background(), slice, channel)
assert.Equal(t, 0, sendSuccessCount)
}
func ExampleSendToChannel() {
// 发送到正常的channel
channel := make(chan int, 10)
slice := []int{0, 1, 2}
sendSuccessCount := SendToChannel(context.Background(), slice, channel)
fmt.Println(sendSuccessCount)
// Output:
// 3
}
// ------------------------------------------------- ToChannel ---------------------------------------------------------
func TestToChannel(t *testing.T) {
slice := []int{0, 1, 2}
channel := ToChannel(slice)
close(channel)
fromChannelSlice := ReadFromChannel(context.Background(), channel)
assert.Equal(t, slice, fromChannelSlice)
}
func ExampleToChannel() {
slice := []int{0, 1, 2}
channel := ToChannel(slice)
close(channel)
fromChannelSlice := ReadFromChannel(context.Background(), channel)
fmt.Println(fromChannelSlice)
// Output:
// [0 1 2]
}
// ------------------------------------------------- ToClosedChannel ---------------------------------------------------
func TestToClosedChannel(t *testing.T) {
slice := []int{0, 1, 2}
channel := ToClosedChannel(slice)
fromChannelSlice := ReadFromChannel(context.Background(), channel)
assert.Equal(t, slice, fromChannelSlice)
}
func ExampleToClosedChannel() {
slice := []int{0, 1, 2}
channel := ToClosedChannel(slice)
fromChannelSlice := ReadFromChannel(context.Background(), channel)
fmt.Println(fromChannelSlice)
// Output:
// [0 1 2]
}
// ---------------------------------------------------------------------------------------------------------------------