-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnetchan_test.go
214 lines (197 loc) · 5.02 KB
/
netchan_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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
package netchan_test
import (
"io"
"log"
"strconv"
"strings"
"testing"
"time"
"github.com/pinkgopher/netchan"
)
// pipeConn represents one side of a full-duplex
// connection based on io.PipeReader/Writer
type pipeConn struct {
*io.PipeReader
*io.PipeWriter
}
func (c pipeConn) Close() error {
c.PipeReader.Close()
c.PipeWriter.Close()
return nil // ignoring errors
}
func newPipeConn() (sideA, sideB pipeConn) {
sideA.PipeReader, sideB.PipeWriter = io.Pipe()
sideB.PipeReader, sideA.PipeWriter = io.Pipe()
return
}
func init() {
log.SetFlags(0)
}
// intProducer sends integers from 0 to n-1 on net-chan chName
func intProducer(t *testing.T, mn *netchan.Session, chName string, n int) {
go func() {
ch := make(chan int, 15)
err := mn.OpenSend(chName, ch)
if err != nil {
log.Fatal(err)
}
for i := 0; i < n; i++ {
select {
case ch <- i:
case <-mn.Done():
log.Fatal(mn.Err())
}
}
close(ch)
}()
}
// intConsumer drains net-chan chName, stores the received integers in a slice
// and delivers the slice on a channel, which is returned
func intConsumer(t *testing.T, mn *netchan.Session, chName string) <-chan []int {
sliceCh := make(chan []int, 1)
go func() {
var slice []int
ch := make(chan int, 8)
err := mn.OpenRecv(chName, ch, 60)
if err != nil {
log.Fatal(err)
}
Loop:
for {
select {
case i, ok := <-ch:
if !ok {
break Loop
}
slice = append(slice, i)
case <-mn.Done():
log.Fatal(mn.Err())
}
}
sliceCh <- slice
}()
return sliceCh
}
// checks that s[i] == i for each i
func checkIntSlice(t *testing.T, s []int) {
for i, si := range s {
if i != si {
log.Fatalf("expected i == s[i], found i == %d, s[i] == %d", i, si)
return
}
}
}
// start the producer before the consumer
func TestSendThenRecv(t *testing.T) {
sideA, sideB := newPipeConn()
intProducer(t, netchan.NewSession(sideA), "integers", 100)
time.Sleep(50 * time.Millisecond)
s := <-intConsumer(t, netchan.NewSession(sideB), "integers")
checkIntSlice(t, s)
}
// start the consumer before the producer
func TestRecvThenSend(t *testing.T) {
sideA, sideB := newPipeConn()
sliceCh := intConsumer(t, netchan.NewSession(sideB), "integers")
time.Sleep(50 * time.Millisecond)
intProducer(t, netchan.NewSession(sideA), "integers", 100)
checkIntSlice(t, <-sliceCh)
}
// open many chans in both directions
func TestManyChans(t *testing.T) {
sideA, sideB := newPipeConn()
manA := netchan.NewSession(sideA)
manB := netchan.NewSession(sideB)
var sliceChans [100]<-chan []int
for i := range sliceChans {
chName := "integers" + strconv.Itoa(i)
if i%2 == 0 {
// producer is sideA, consumer is sideB
intProducer(t, manA, chName, 400)
sliceChans[i] = intConsumer(t, manB, chName)
} else {
// producer is sideB, consumer is sideA
intProducer(t, manB, chName, 400)
sliceChans[i] = intConsumer(t, manA, chName)
}
}
for _, ch := range sliceChans {
checkIntSlice(t, <-ch)
}
}
// send many integers on a net-chan with a small buffer. If the credit system is broken,
// at some point the credit will stay 0 (deadlock) or it will excede the limit,
// causing an error
// TODO: find a better way of testing this
func TestCredits(t *testing.T) {
sideA, sideB := newPipeConn()
intProducer(t, netchan.NewSession(sideA), "integers", 1000)
s := <-intConsumer(t, netchan.NewSession(sideB), "integers")
checkIntSlice(t, s)
}
func TestMsgSizeLimit(t *testing.T) {
sideA, sideB := newPipeConn()
go sliceProducer(t, sideA)
sliceConsumer(t, sideB)
}
const (
limit = 2000 // size limit enforced by sliceConsumer
numSlices = 20 // number of slices to send
)
// sliceProducer sends on "slices". The last slice will be too big.
func sliceProducer(t *testing.T, conn io.ReadWriteCloser) {
mn := netchan.NewSession(conn)
ch := make(chan []byte, 1)
err := mn.OpenSend("slices", ch)
if err != nil {
log.Fatal(err)
}
small := make([]byte, limit-30) // some tolerance for gob type info
big := make([]byte, limit+5)
for i := 1; i <= numSlices; i++ {
slice := small
if i == numSlices {
slice = big
}
select {
case ch <- slice:
case <-mn.Done():
log.Fatal(mn.Err())
}
}
close(ch)
}
// sliceConsumer receives from "slices" using a limitedReader. The last slice is too big
// and must generate an error that matches the one returned by the limitedReader used by
// the decoder
func sliceConsumer(t *testing.T, conn io.ReadWriteCloser) {
mn := netchan.NewSessionLimit(conn, limit)
// use a receive buffer with capacity 1, so that items come
// one at a time and we get the error for the last one only
ch := make(chan []byte)
err := mn.OpenRecv("slices", ch, 1)
if err != nil {
log.Fatal(err)
}
for i := 1; i <= numSlices; i++ {
if i < numSlices {
select {
case <-ch:
case <-mn.Done():
log.Fatal(mn.Err())
}
continue
}
// i == numSlices, expect errSizeExceeded
select {
case <-ch:
log.Fatal("manager did not block too big message")
case <-mn.Done():
err := mn.Err()
if strings.Contains(err.Error(), "too big") {
return // success
}
log.Fatal(err)
}
}
}