1
- package backedpipe
1
+ package backedpipe_test
2
2
3
3
import (
4
4
"bytes"
@@ -10,6 +10,9 @@ import (
10
10
11
11
"github.com/stretchr/testify/require"
12
12
"golang.org/x/xerrors"
13
+
14
+ "github.com/coder/coder/v2/coderd/agentapi/backedpipe"
15
+ "github.com/coder/coder/v2/testutil"
13
16
)
14
17
15
18
// mockConnection implements io.ReadWriteCloser for testing
@@ -59,7 +62,7 @@ func (mc *mockConnection) Close() error {
59
62
}
60
63
61
64
func (mc * mockConnection ) WriteString (s string ) {
62
- mc .readBuffer .WriteString (s )
65
+ _ , _ = mc .readBuffer .WriteString (s )
63
66
}
64
67
65
68
func (mc * mockConnection ) ReadString () string {
@@ -83,7 +86,7 @@ func (mc *mockConnection) Reset() {
83
86
}
84
87
85
88
// mockReconnectFunc creates a unified reconnect function with all behaviors enabled
86
- func mockReconnectFunc (connections ... * mockConnection ) (ReconnectFunc , * int , chan struct {}) {
89
+ func mockReconnectFunc (connections ... * mockConnection ) (backedpipe. ReconnectFunc , * int , chan struct {}) {
87
90
connectionIndex := 0
88
91
callCount := 0
89
92
signalChan := make (chan struct {}, 1 )
@@ -108,11 +111,12 @@ func mockReconnectFunc(connections ...*mockConnection) (ReconnectFunc, *int, cha
108
111
109
112
// Determine readerSeqNum based on call count
110
113
var readerSeqNum uint64
111
- if callCount == 1 {
114
+ switch {
115
+ case callCount == 1 :
112
116
readerSeqNum = 0
113
- } else if conn .seqNum != 0 {
117
+ case conn .seqNum != 0 :
114
118
readerSeqNum = conn .seqNum
115
- } else {
119
+ default :
116
120
readerSeqNum = writerSeqNum
117
121
}
118
122
@@ -128,7 +132,7 @@ func TestBackedPipe_NewBackedPipe(t *testing.T) {
128
132
ctx := context .Background ()
129
133
reconnectFn , _ , _ := mockReconnectFunc (newMockConnection ())
130
134
131
- bp := NewBackedPipe (ctx , reconnectFn )
135
+ bp := backedpipe . NewBackedPipe (ctx , reconnectFn )
132
136
require .NotNil (t , bp )
133
137
require .False (t , bp .Connected ())
134
138
}
@@ -140,7 +144,7 @@ func TestBackedPipe_Connect(t *testing.T) {
140
144
conn := newMockConnection ()
141
145
reconnectFn , callCount , _ := mockReconnectFunc (conn )
142
146
143
- bp := NewBackedPipe (ctx , reconnectFn )
147
+ bp := backedpipe . NewBackedPipe (ctx , reconnectFn )
144
148
145
149
err := bp .Connect (ctx )
146
150
require .NoError (t , err )
@@ -155,7 +159,7 @@ func TestBackedPipe_ConnectAlreadyConnected(t *testing.T) {
155
159
conn := newMockConnection ()
156
160
reconnectFn , _ , _ := mockReconnectFunc (conn )
157
161
158
- bp := NewBackedPipe (ctx , reconnectFn )
162
+ bp := backedpipe . NewBackedPipe (ctx , reconnectFn )
159
163
160
164
err := bp .Connect (ctx )
161
165
require .NoError (t , err )
@@ -173,7 +177,7 @@ func TestBackedPipe_ConnectAfterClose(t *testing.T) {
173
177
conn := newMockConnection ()
174
178
reconnectFn , _ , _ := mockReconnectFunc (conn )
175
179
176
- bp := NewBackedPipe (ctx , reconnectFn )
180
+ bp := backedpipe . NewBackedPipe (ctx , reconnectFn )
177
181
178
182
err := bp .Close ()
179
183
require .NoError (t , err )
@@ -190,7 +194,7 @@ func TestBackedPipe_BasicReadWrite(t *testing.T) {
190
194
conn := newMockConnection ()
191
195
reconnectFn , _ , _ := mockReconnectFunc (conn )
192
196
193
- bp := NewBackedPipe (ctx , reconnectFn )
197
+ bp := backedpipe . NewBackedPipe (ctx , reconnectFn )
194
198
195
199
err := bp .Connect (ctx )
196
200
require .NoError (t , err )
@@ -218,7 +222,7 @@ func TestBackedPipe_WriteBeforeConnect(t *testing.T) {
218
222
conn := newMockConnection ()
219
223
reconnectFn , _ , _ := mockReconnectFunc (conn )
220
224
221
- bp := NewBackedPipe (ctx , reconnectFn )
225
+ bp := backedpipe . NewBackedPipe (ctx , reconnectFn )
222
226
223
227
// Write before connecting should succeed (buffered)
224
228
n , err := bp .Write ([]byte ("hello" ))
@@ -239,7 +243,7 @@ func TestBackedPipe_ReadBlocksWhenDisconnected(t *testing.T) {
239
243
ctx := context .Background ()
240
244
reconnectFn , _ , _ := mockReconnectFunc (newMockConnection ())
241
245
242
- bp := NewBackedPipe (ctx , reconnectFn )
246
+ bp := backedpipe . NewBackedPipe (ctx , reconnectFn )
243
247
244
248
// Start a read that should block
245
249
readDone := make (chan struct {})
@@ -282,7 +286,7 @@ func TestBackedPipe_Reconnection(t *testing.T) {
282
286
conn2 .seqNum = 17 // Remote has received 17 bytes, so replay from sequence 17
283
287
reconnectFn , _ , signalChan := mockReconnectFunc (conn1 , conn2 )
284
288
285
- bp := NewBackedPipe (ctx , reconnectFn )
289
+ bp := backedpipe . NewBackedPipe (ctx , reconnectFn )
286
290
287
291
// Initial connect
288
292
err := bp .Connect (ctx )
@@ -322,7 +326,7 @@ func TestBackedPipe_Close(t *testing.T) {
322
326
conn := newMockConnection ()
323
327
reconnectFn , _ , _ := mockReconnectFunc (conn )
324
328
325
- bp := NewBackedPipe (ctx , reconnectFn )
329
+ bp := backedpipe . NewBackedPipe (ctx , reconnectFn )
326
330
327
331
err := bp .Connect (ctx )
328
332
require .NoError (t , err )
@@ -346,7 +350,7 @@ func TestBackedPipe_CloseIdempotent(t *testing.T) {
346
350
conn := newMockConnection ()
347
351
reconnectFn , _ , _ := mockReconnectFunc (conn )
348
352
349
- bp := NewBackedPipe (ctx , reconnectFn )
353
+ bp := backedpipe . NewBackedPipe (ctx , reconnectFn )
350
354
351
355
err := bp .Close ()
352
356
require .NoError (t , err )
@@ -363,10 +367,10 @@ func TestBackedPipe_WaitForConnection(t *testing.T) {
363
367
conn := newMockConnection ()
364
368
reconnectFn , _ , _ := mockReconnectFunc (conn )
365
369
366
- bp := NewBackedPipe (ctx , reconnectFn )
370
+ bp := backedpipe . NewBackedPipe (ctx , reconnectFn )
367
371
368
372
// Should timeout when not connected
369
- timeoutCtx , cancel := context .WithTimeout (ctx , 10 * time . Millisecond )
373
+ timeoutCtx , cancel := context .WithTimeout (ctx , testutil . WaitShort )
370
374
defer cancel ()
371
375
372
376
err := bp .WaitForConnection (timeoutCtx )
@@ -390,7 +394,7 @@ func TestBackedPipe_ConcurrentReadWrite(t *testing.T) {
390
394
conn := newMockConnection ()
391
395
reconnectFn , _ , _ := mockReconnectFunc (conn )
392
396
393
- bp := NewBackedPipe (ctx , reconnectFn )
397
+ bp := backedpipe . NewBackedPipe (ctx , reconnectFn )
394
398
395
399
err := bp .Connect (ctx )
396
400
require .NoError (t , err )
@@ -478,7 +482,7 @@ func TestBackedPipe_ReconnectFunctionFailure(t *testing.T) {
478
482
return nil , 0 , xerrors .New ("reconnect failed" )
479
483
}
480
484
481
- bp := NewBackedPipe (ctx , failingReconnectFn )
485
+ bp := backedpipe . NewBackedPipe (ctx , failingReconnectFn )
482
486
483
487
err := bp .Connect (ctx )
484
488
require .Error (t , err )
@@ -494,7 +498,7 @@ func TestBackedPipe_ForceReconnect(t *testing.T) {
494
498
conn2 := newMockConnection ()
495
499
reconnectFn , callCount , _ := mockReconnectFunc (conn1 , conn2 )
496
500
497
- bp := NewBackedPipe (ctx , reconnectFn )
501
+ bp := backedpipe . NewBackedPipe (ctx , reconnectFn )
498
502
499
503
// Initial connect
500
504
err := bp .Connect (ctx )
@@ -538,7 +542,7 @@ func TestBackedPipe_ForceReconnectWhenClosed(t *testing.T) {
538
542
conn := newMockConnection ()
539
543
reconnectFn , _ , _ := mockReconnectFunc (conn )
540
544
541
- bp := NewBackedPipe (ctx , reconnectFn )
545
+ bp := backedpipe . NewBackedPipe (ctx , reconnectFn )
542
546
543
547
// Close the pipe first
544
548
err := bp .Close ()
@@ -557,7 +561,7 @@ func TestBackedPipe_ForceReconnectWhenDisconnected(t *testing.T) {
557
561
conn := newMockConnection ()
558
562
reconnectFn , callCount , _ := mockReconnectFunc (conn )
559
563
560
- bp := NewBackedPipe (ctx , reconnectFn )
564
+ bp := backedpipe . NewBackedPipe (ctx , reconnectFn )
561
565
562
566
// Don't connect initially, just force reconnect
563
567
err := bp .ForceReconnect (ctx )
@@ -583,7 +587,7 @@ func BenchmarkBackedPipe_Write(b *testing.B) {
583
587
conn := newMockConnection ()
584
588
reconnectFn , _ , _ := mockReconnectFunc (conn )
585
589
586
- bp := NewBackedPipe (ctx , reconnectFn )
590
+ bp := backedpipe . NewBackedPipe (ctx , reconnectFn )
587
591
bp .Connect (ctx )
588
592
589
593
data := make ([]byte , 1024 ) // 1KB writes
@@ -599,7 +603,7 @@ func BenchmarkBackedPipe_Read(b *testing.B) {
599
603
conn := newMockConnection ()
600
604
reconnectFn , _ , _ := mockReconnectFunc (conn )
601
605
602
- bp := NewBackedPipe (ctx , reconnectFn )
606
+ bp := backedpipe . NewBackedPipe (ctx , reconnectFn )
603
607
bp .Connect (ctx )
604
608
605
609
// Fill connection with data
0 commit comments