generated from devnw/oss-template
-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
mocks_test.go
355 lines (291 loc) · 6.04 KB
/
mocks_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
344
345
346
347
348
349
350
351
352
353
354
355
package plex
import (
"bytes"
"context"
"crypto/sha1"
"fmt"
"io"
"math/rand"
"net"
"sync"
"testing"
"time"
)
type testconn struct {
rwc *rwStream
tcp bool
addr string
}
func (c *testconn) Read(b []byte) (n int, err error) {
if c.rwc == nil {
return 0, io.EOF
}
return c.rwc.Read(b)
}
func (c *testconn) Write(b []byte) (n int, err error) {
if c.rwc == nil {
return 0, io.EOF
}
return c.rwc.Write(b)
}
func (c *testconn) Close() error {
return nil
}
func (c *testconn) RemoteAddr() net.Addr {
return &testaddr{
tcp: c.tcp,
addr: c.addr,
}
}
// Not implemented for now, just here to make bconn
// look like a net.Conn
func (*testconn) LocalAddr() net.Addr { return nil }
func (*testconn) SetDeadline(t time.Time) error { return nil }
func (*testconn) SetReadDeadline(t time.Time) error { return nil }
func (*testconn) SetWriteDeadline(t time.Time) error { return nil }
type iotestconn struct {
rw io.ReadWriter
tcp bool
addr string
}
func (c *iotestconn) Read(b []byte) (n int, err error) {
if c.rw == nil {
return 0, io.EOF
}
return c.rw.Read(b)
}
func (c *iotestconn) Write(b []byte) (n int, err error) {
if c.rw == nil {
return 0, io.EOF
}
return c.rw.Write(b)
}
func (c *iotestconn) Close() error {
return nil
}
func (c *iotestconn) RemoteAddr() net.Addr {
return &testaddr{
tcp: c.tcp,
addr: c.addr,
}
}
// Not implemented for now, just here to make bconn
// look like a net.Conn
func (*iotestconn) LocalAddr() net.Addr { return nil }
func (*iotestconn) SetDeadline(t time.Time) error { return nil }
func (*iotestconn) SetReadDeadline(t time.Time) error { return nil }
func (*iotestconn) SetWriteDeadline(t time.Time) error { return nil }
type testaddr struct {
tcp bool
addr string
}
func (t *testaddr) Network() string {
if t.tcp {
return "tcp"
}
return "udp"
}
func (t *testaddr) String() string {
return t.addr
}
type ctxtest struct {
parent context.Context
child context.Context
}
func ctxCancelTests() map[string]ctxtest {
ctx := context.Background()
cancelledCtx, cancelledCancel := context.WithCancel(context.Background())
cancelledCancel()
return map[string]ctxtest{
"child cancel": {
parent: ctx,
child: cancelledCtx,
},
"parent cancel": {
parent: cancelledCtx,
child: ctx,
},
"parent cancel, nil child": {
parent: cancelledCtx,
child: nil,
},
}
}
func (test ctxtest) Eval(t *testing.T, err error) {
if err == nil {
t.Fatalf("expected error")
}
if err != context.Canceled {
t.Fatalf("expected context canceled; got %v", err)
}
if test.parent != nil {
if test.parent.Err() == err {
return
}
}
if test.child != nil {
if test.child.Err() == err {
return
}
}
t.Fatalf("expected error to match parent or child")
}
// SumByteSlice is a map with a key which is the sha1sum of the byte slice
type SumByteSlice map[string][]byte
func (s SumByteSlice) SliceOfReadWriter() []io.ReadWriter {
rws := make([]io.ReadWriter, 0, len(s))
for _, v := range s {
rws = append(rws, bytes.NewBuffer(v))
}
return rws
}
// setOfRandBytes returns a set of random byte slices of the given size
func setOfRandBytes(size int) (data SumByteSlice, err error) {
data = SumByteSlice{}
for i := 0; i < size; i++ {
// Mod 1024 to make sure the random byteData
// are not too large
byteData, err := randBytes(rand.Int() % 1024)
if err != nil {
return nil, err
}
data[fmt.Sprintf("%x", sha1.Sum(byteData))] = byteData
}
return data, nil
}
// randBytes returns a random byte slice of the given size
func randBytes(size int) ([]byte, error) {
buff := make([]byte, size)
n, err := rand.Read(buff)
if err != nil {
return nil, fmt.Errorf("error reading random data: %s", err)
}
return buff[:n], nil
}
type wrappedError interface {
error
Unwrap() error
}
func newStream(ctx context.Context) *testconn {
ctx, cancel := _ctx(ctx)
return &testconn{
&rwStream{
ctx: ctx,
cancel: cancel,
data: make(chan byte),
},
true,
"0.0.0.0",
}
}
type rwStream struct {
ctx context.Context
cancel context.CancelFunc
data chan byte
mu sync.Mutex
wg sync.WaitGroup
}
func (rws *rwStream) Recv(ctx context.Context, buffer int) (<-chan byte, error) {
ctx = merge(rws.ctx, ctx)
out := make(chan byte)
rws.mu.Lock()
defer rws.mu.Unlock()
rws.wg.Add(1)
go func(out chan<- byte, in <-chan byte) {
defer func() {
_ = recover() // TODO: handle in the future?
}()
defer rws.wg.Done()
defer close(out)
for {
select {
case <-rws.ctx.Done():
return
case <-ctx.Done():
return
case b, ok := <-in:
if !ok {
return
}
select {
case <-rws.ctx.Done():
return
case <-ctx.Done():
return
case out <- b:
}
}
}
}(out, rws.data)
return out, nil
}
func (rws *rwStream) Send(ctx context.Context, buffer int) (chan<- byte, error) {
ctx = merge(rws.ctx, ctx)
in := make(chan byte)
rws.mu.Lock()
defer rws.mu.Unlock()
rws.wg.Add(1)
go func(internal chan<- byte, in <-chan byte) {
defer func() {
_ = recover() // TODO: handle in the future?
}()
defer rws.wg.Done()
for {
select {
case <-rws.ctx.Done():
return
case <-ctx.Done():
return
case b, ok := <-in:
if !ok {
return
}
select {
case <-rws.ctx.Done():
return
case <-ctx.Done():
return
case internal <- b:
}
}
}
}(rws.data, in)
return in, nil
}
func (rws *rwStream) Read(p []byte) (n int, err error) {
var i int
for i = 0; i < len(p); i++ {
select {
case <-rws.ctx.Done():
return 0, rws.ctx.Err()
case b, ok := <-rws.data:
if !ok {
return 0, io.EOF
}
p[i] = b
}
}
return i, nil
}
func (rws *rwStream) Write(p []byte) (n int, err error) {
var i int
for i = 0; i < len(p); i++ {
select {
case <-rws.ctx.Done():
return 0, rws.ctx.Err()
case rws.data <- p[i]:
}
}
return i, nil
}
func (rws *rwStream) Close() error {
defer func() {
_ = recover() // TODO: handle in the future?
}()
rws.cancel()
<-rws.ctx.Done()
rws.mu.Lock()
defer rws.mu.Unlock()
rws.wg.Wait()
return nil
}