-
Notifications
You must be signed in to change notification settings - Fork 0
/
cts_test.go
343 lines (300 loc) · 7.6 KB
/
cts_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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
package ssh
import (
"encoding/binary"
"fmt"
"io"
"testing"
"time"
"github.com/glycerine/rbuf"
)
var timeGood = fmt.Errorf("overall time completed")
var writeOk = fmt.Errorf("write was ok")
var readOk = fmt.Errorf("read was ok")
// a simple circular buffer than
// we can fill for any amount of
// time and track the total number
// of bytes written to it.
type infiniteRing struct {
ring *rbuf.FixedSizeRingBuf
nrtot int64
nwtot int64
next int64
sz int
}
const ringsz = 64 * 1024
const maxwords = ringsz / 8
func newInfiniteRing() *infiniteRing {
return &infiniteRing{
ring: rbuf.NewFixedSizeRingBuf(ringsz),
sz: ringsz,
}
}
// Write checks and panics if data is not in order.
// It expects each 64-bit word to contain the next
// integer, little endian.
func (ir *infiniteRing) Write(b []byte) (n int, err error) {
words := len(b) / 8
if words > maxwords {
words = maxwords
}
if words == 0 {
return 0, nil
}
ir.ring.Reset()
n, err = ir.ring.WriteAndMaybeOverwriteOldestData(b[:words*8])
ir.nwtot += int64(n)
q("infiniteRing.Write total of %v", ir.nwtot)
expect := make([]byte, 8)
by := ir.ring.Bytes()
for i := 0; i < words; i++ {
binary.LittleEndian.PutUint64(expect, uint64(ir.next))
obs := by[i*8 : (i+1)*8]
obsnum := int64(binary.LittleEndian.Uint64(obs))
if obsnum != ir.next {
panic(fmt.Sprintf("bytes written to ring where not in order! observed='%v', expected='%v'. at i=%v out of %v words", obsnum, ir.next, i, words))
}
ir.next++
}
return
}
func (ir *infiniteRing) Read(b []byte) (n int, err error) {
n, err = ir.ring.Read(b)
ir.nrtot += int64(n)
return
}
type seqWords struct {
next int64
}
// provide the integers, starting at zero and
// counting up, as 64-bit words.
func newSequentialWords() *seqWords {
return &seqWords{}
}
func (s *seqWords) Read(b []byte) (n int, err error) {
numword := len(b) / 8
for i := 0; i < numword; i++ {
binary.LittleEndian.PutUint64(b[i*8:(i+1)*8], uint64(s.next))
s.next++
}
//p("seqWords.Read up to %v done, total bytes %v", s.next, s.next*8)
return numword * 8, nil
}
// Given a 2 sec idle *read* or *write* timeout, if we continuously transfer
// for 20 seconds (or 10x our idle timeout), we should not see any timeout since
// our activity is ongoing continuously.
func TestCtsReadWithNoIdleTimeout(t *testing.T) {
defer xtestend(xtestbegin(t))
testCts(true, t)
}
func TestCtsWriteWithNoIdleTimeout(t *testing.T) {
defer xtestend(xtestbegin(t))
testCts(false, t)
}
func setTo(r, w Channel, timeOutOnReader bool, idleout time.Duration) {
// set the timeout on the reader/writer
if timeOutOnReader {
err := r.SetReadIdleTimeout(idleout)
if err != nil {
panic(fmt.Sprintf("r.SetIdleTimeout: %v", err))
}
} else {
// set the timeout on the writer
err := w.SetWriteIdleTimeout(idleout)
if err != nil {
panic(fmt.Sprintf("w.SetIdleTimeout: %v", err))
}
}
}
func setClose(r, w Channel, closeReader bool) {
// set the timeout on the writer, ignore
// errors, probably race to shutdown; this is
// aimed at shutdown.
if closeReader {
r.Close()
} else {
w.Close()
}
}
func testCts(timeOutOnReader bool, t *testing.T) {
halt := NewHalter()
defer halt.RequestStop()
r, w, mux := channelPair(t, halt)
p("r.idleTimer = %p", r.idleR)
p("w.idleTimer = %p", w.idleW)
idleout := 2000 * time.Millisecond
overall := 10 * idleout
t0 := time.Now()
tstop := t0.Add(overall)
haltr := NewHalter()
haltw := NewHalter()
defer haltr.RequestStop()
defer haltw.RequestStop()
setTo(r, w, timeOutOnReader, idleout)
readErr := make(chan error)
writeErr := make(chan error)
var seq *seqWords
var ring *infiniteRing
go readerToRing(idleout, r, haltr, overall, tstop, readErr, &ring)
go seqWordsToWriter(w, haltw, tstop, writeErr, &seq)
after := time.After(overall)
// wait for our overall time, and for both to return
var rerr, werr error
var rok, wok bool
var haltrDone, haltwDone bool
complete := func() bool {
return rok && wok && haltrDone && haltwDone
}
collectionLoop:
for {
select {
case <-haltr.DoneChan():
haltrDone = true
if complete() {
break collectionLoop
}
case <-haltw.DoneChan():
haltwDone = true
if complete() {
break collectionLoop
}
case <-after:
p("after completed!")
after = nil
// the main point of the test: did after timeout
// fire before r or w returned?
if rok || wok {
panic("sadness, failed test: rok || wok happened before overall elapsed")
} else {
p("success!!!!!")
}
// release the other. e.g. the writer will typically be blocked after
// the reader timeout test, since the writer didn't get a timeout.
// Closing is faster than setting a timeout and waiting for it.
setClose(r, w, !timeOutOnReader)
haltr.RequestStop()
haltw.RequestStop()
if complete() {
break collectionLoop
}
case rerr = <-readErr:
p("got rerr")
now := time.Now()
if now.Before(tstop) {
panic(fmt.Sprintf("rerr: '%v', stopped too early, before '%v'. now=%v. now-before=%v", rerr, tstop, now, now.Sub(tstop))) // panicing here
}
rok = true
if complete() {
break collectionLoop
}
case werr = <-writeErr:
p("got werr")
now := time.Now()
if now.Before(tstop) {
panic(fmt.Sprintf("rerr: '%v', stopped too early, before '%v'. now=%v. now-before=%v", werr, tstop, now, now.Sub(tstop)))
}
wok = true
if complete() {
break collectionLoop
}
}
}
p("done with collection loop")
// sanity check that we read all we wrote.
seqby := (seq.next - 1) * 8
if ring.nwtot != seqby {
// panic: wrote 18636636160 but read 18636799992. diff=-163832
// the differ by some, since shutdown isn't coordinated
// by having the sender stop sending and close first.
p("wrote %v but read %v. diff=%v", ring.nwtot, seqby, ring.nwtot-seqby)
}
// actually shutdown is pretty racy, lots of possible errors on Close,
// such as EOF
w.Close()
r.Close()
mux.Close()
}
// setup reader r -> infiniteRing ring. returns
// readOk upon success.
func readerToRing(idleout time.Duration, r Channel, halt *Halter, overall time.Duration, tstop time.Time, readErr chan error, pRing **infiniteRing) (err error) {
defer func() {
p("readerToRing returning on readErr, err = '%v'", err)
readErr <- err
halt.MarkDone()
}()
ring := newInfiniteRing()
*pRing = ring
src := r
dst := ring
buf := make([]byte, 32*1024)
numwrites := 0
for {
nr, er := src.Read(buf)
if nr > 0 {
nw, ew := dst.Write(buf[0:nr])
if ew != nil {
err = ew
p("readerToRing sees Write err %v", ew)
break
}
if nr != nw {
err = io.ErrShortWrite
break
}
numwrites++
select {
case <-halt.ReqStopChan():
return readOk
default:
}
}
if er != nil {
p("readerToRing sees Read err %v", er)
if er != io.EOF {
err = er
}
break
}
} //end for
return err
}
// read from the integers 0,1,2,... and write to w until tstop.
// returns writeOk upon success
func seqWordsToWriter(w Channel, halt *Halter, tstop time.Time, writeErr chan error, pSeqWords **seqWords) (err error) {
defer func() {
//p("seqWordsToWriter returning err = '%v'", err)
writeErr <- err
halt.MarkDone()
}()
src := newSequentialWords()
*pSeqWords = src
dst := w
buf := make([]byte, 32*1024)
for {
nr, er := src.Read(buf)
if nr > 0 {
nw, ew := dst.Write(buf[0:nr])
if ew != nil {
//p("seqWriter sees Write err %v", ew)
err = ew
break
}
if nr != nw {
err = io.ErrShortWrite
break
}
select {
case <-halt.ReqStopChan():
return writeOk
default:
}
}
if er != nil {
p("seqWriter sees Read err %v", er)
if er != io.EOF {
err = er
}
break
}
}
return err
}