forked from temporalio/tchannel-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
close_test.go
581 lines (483 loc) · 16.6 KB
/
close_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
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
// Copyright (c) 2015 Uber Technologies, Inc.
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
package tchannel_test
import (
"math/rand"
"sync"
"testing"
"time"
. "github.com/uber/tchannel-go"
"github.com/uber/tchannel-go/raw"
"github.com/uber/tchannel-go/testutils"
"github.com/uber/tchannel-go/testutils/goroutines"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.uber.org/atomic"
"golang.org/x/net/context"
)
type channelState struct {
testServer *testutils.TestServer
closeCh chan struct{}
closed bool
}
func makeCall(client *Channel, server *testutils.TestServer) error {
ctx, cancel := NewContext(time.Second)
defer cancel()
_, _, _, err := raw.Call(ctx, client, server.HostPort(), server.ServiceName(), "test", nil, nil)
return err
}
func assertStateChangesTo(t testing.TB, ch *Channel, state ChannelState) {
var lastState ChannelState
require.True(t, testutils.WaitFor(time.Second, func() bool {
lastState = ch.State()
return lastState == state
}), "Channel state is %v expected %v", lastState, state)
}
func TestCloseOnlyListening(t *testing.T) {
ch := testutils.NewServer(t, nil)
// If there are no connections, then the channel should close immediately.
ch.Close()
assert.Equal(t, ChannelClosed, ch.State())
assert.True(t, ch.Closed(), "Channel should be closed")
}
func TestCloseNewClient(t *testing.T) {
ch := testutils.NewClient(t, nil)
// If there are no connections, then the channel should close immediately.
ch.Close()
assert.Equal(t, ChannelClosed, ch.State())
assert.True(t, ch.Closed(), "Channel should be closed")
}
func ignoreError(h *testHandler) Handler {
return raw.Wrap(onErrorTestHandler{
testHandler: h,
onError: func(_ context.Context, err error) {},
})
}
func TestCloseAfterTimeout(t *testing.T) {
// Disable log verfication since connections are closed after a timeout
// and the relay might still be reading/writing to the connection.
// TODO: Ideally, we only disable log verification on the relay.
opts := testutils.NewOpts().DisableLogVerification()
testutils.WithTestServer(t, opts, func(t testing.TB, ts *testutils.TestServer) {
testHandler := newTestHandler(t)
ts.Register(ignoreError(testHandler), "block")
ctx, cancel := NewContext(100 * time.Millisecond)
defer cancel()
// Make a call, wait for it to timeout.
clientCh := ts.NewClient(nil)
_, _, _, err := raw.Call(ctx, clientCh, ts.HostPort(), ts.ServiceName(), "block", nil, nil)
require.Equal(t, ErrTimeout, err, "Expected call to timeout")
// The client channel should also close immediately.
clientCh.Close()
assertStateChangesTo(t, clientCh, ChannelClosed)
assert.True(t, clientCh.Closed(), "Channel should be closed")
})
}
func TestRelayCloseTimeout(t *testing.T) {
opts := testutils.NewOpts().
SetRelayOnly(). // this is a relay-specific test.
DisableLogVerification() // we're causing errors on purpose.
opts.DefaultConnectionOptions.MaxCloseTime = 100 * time.Millisecond
testutils.WithTestServer(t, opts, func(t testing.TB, ts *testutils.TestServer) {
gotCall := make(chan struct{})
unblock := make(chan struct{})
defer close(unblock)
testutils.RegisterEcho(ts.Server(), func() {
close(gotCall)
<-unblock
})
clientCh := ts.NewClient(opts)
// Start a call in the background, since it will block
go func() {
ctx, cancel := NewContext(10 * time.Second)
defer cancel()
_, _, _, err := raw.Call(ctx, clientCh, ts.HostPort(), ts.ServiceName(), "echo", nil, nil)
require.Error(t, err)
assert.Equal(t, ErrCodeNetwork, GetSystemErrorCode(err),
"expect network error from relay closing connection on timeout")
}()
<-gotCall
ts.Relay().Close()
// The relay should close within the timeout.
<-ts.Relay().ClosedChan()
})
}
func TestRaceExchangesWithClose(t *testing.T) {
var wg sync.WaitGroup
ctx, cancel := NewContext(testutils.Timeout(70 * time.Millisecond))
defer cancel()
opts := testutils.NewOpts().DisableLogVerification()
testutils.WithTestServer(t, opts, func(t testing.TB, ts *testutils.TestServer) {
server := ts.Server()
gotCall := make(chan struct{})
completeCall := make(chan struct{})
testutils.RegisterFunc(server, "dummy", func(ctx context.Context, args *raw.Args) (*raw.Res, error) {
return &raw.Res{}, nil
})
testutils.RegisterEcho(server, func() {
close(gotCall)
<-completeCall
})
client := ts.NewClient(opts)
defer client.Close()
callDone := make(chan struct{})
go func() {
assert.NoError(t, testutils.CallEcho(client, ts.HostPort(), server.ServiceName(), &raw.Args{}), "Echo failed")
close(callDone)
}()
// Wait until the server recieves a call, so it has an active inbound.
<-gotCall
// Start a bunch of clients to trigger races between connecting and close.
var closed atomic.Bool
for i := 0; i < 100; i++ {
wg.Add(1)
go func() {
defer wg.Done()
// We don't use ts.NewClient here to avoid data races.
c := testutils.NewClient(t, opts)
defer c.Close()
if closed.Load() {
return
}
if err := c.Ping(ctx, ts.HostPort()); err != nil {
return
}
if closed.Load() {
return
}
raw.Call(ctx, c, ts.HostPort(), server.ServiceName(), "dummy", nil, nil)
}()
}
// Now try to close the channel, it should block since there's active exchanges.
server.Close()
closed.Store(true)
assert.Equal(t, ChannelStartClose, ts.Server().State(), "Server should be in StartClose")
closed.Store(true)
close(completeCall)
<-callDone
})
// Wait for all calls to complete
wg.Wait()
}
// TestCloseStress ensures that once a Channel is closed, it cannot be reached.
func TestCloseStress(t *testing.T) {
CheckStress(t)
const numHandlers = 5
handler := &swapper{t}
var lock sync.RWMutex
var wg sync.WaitGroup
var channels []*channelState
// Start numHandlers servers, and don't close the connections till they are signalled.
for i := 0; i < numHandlers; i++ {
wg.Add(1)
go func() {
testutils.WithTestServer(t, nil, func(t testing.TB, ts *testutils.TestServer) {
ts.Register(raw.Wrap(handler), "test")
chState := &channelState{
testServer: ts,
closeCh: make(chan struct{}),
}
lock.Lock()
channels = append(channels, chState)
lock.Unlock()
wg.Done()
// Wait for a close signal.
<-chState.closeCh
// Lock until the connection is closed.
lock.Lock()
chState.closed = true
})
}()
}
// Wait till all the channels have been registered.
wg.Wait()
// Start goroutines to make calls until the test has ended.
testEnded := make(chan struct{})
for i := 0; i < 10; i++ {
go func() {
for {
select {
case <-testEnded:
return
default:
// Keep making requests till the test ends.
}
// Get 2 random channels and make a call from one to the other.
lock.RLock()
chState1 := channels[rand.Intn(len(channels))]
chState2 := channels[rand.Intn(len(channels))]
if chState1 == chState2 {
lock.RUnlock()
continue
}
// Grab a read lock to make sure channels aren't closed while we call.
ch1Closed := chState1.closed
ch2Closed := chState2.closed
err := makeCall(chState1.testServer.NewClient(nil), chState2.testServer)
lock.RUnlock()
if ch1Closed || ch2Closed {
assert.Error(
t,
err,
"Call from %v (%v) to %v (%v) should fail",
chState1.testServer.ServiceName(),
chState1.testServer.HostPort(),
chState2.testServer.ServiceName(),
chState2.testServer.HostPort(),
)
} else {
assert.NoError(
t,
err,
"Call from %v (%v) to %v (%v) should not fail",
chState1.testServer.ServiceName(),
chState1.testServer.HostPort(),
chState2.testServer.ServiceName(),
chState2.testServer.HostPort(),
)
}
}
}()
}
// Kill connections till all of the connections are dead.
for i := 0; i < numHandlers; i++ {
time.Sleep(time.Duration(rand.Intn(50)) * time.Millisecond)
channels[i].closeCh <- struct{}{}
}
}
type closeSemanticsTest struct {
*testing.T
isolated bool
}
func (t *closeSemanticsTest) makeServer(name string) (*Channel, chan struct{}) {
ch := testutils.NewServer(t.T, &testutils.ChannelOpts{ServiceName: name})
c := make(chan struct{})
testutils.RegisterFunc(ch, "stream", func(ctx context.Context, args *raw.Args) (*raw.Res, error) {
<-c
return &raw.Res{}, nil
})
testutils.RegisterFunc(ch, "call", func(ctx context.Context, args *raw.Args) (*raw.Res, error) {
return &raw.Res{}, nil
})
return ch, c
}
func (t *closeSemanticsTest) withNewClient(f func(ch *Channel)) {
ch := testutils.NewClient(t.T, &testutils.ChannelOpts{ServiceName: "client"})
f(ch)
ch.Close()
}
func (t *closeSemanticsTest) startCall(from *Channel, to *Channel, method string) (*OutboundCall, error) {
ctx, _ := NewContext(time.Second)
var call *OutboundCall
var err error
toPeer := to.PeerInfo()
if t.isolated {
sc := from.GetSubChannel(toPeer.ServiceName, Isolated)
sc.Peers().Add(toPeer.HostPort)
call, err = sc.BeginCall(ctx, method, nil)
} else {
call, err = from.BeginCall(ctx, toPeer.HostPort, toPeer.ServiceName, method, nil)
}
return call, err
}
func (t *closeSemanticsTest) call(from *Channel, to *Channel) error {
call, err := t.startCall(from, to, "call")
if err == nil {
_, _, _, err = raw.WriteArgs(call, nil, nil)
}
return err
}
func (t *closeSemanticsTest) callStream(from *Channel, to *Channel) <-chan struct{} {
c := make(chan struct{})
call, err := t.startCall(from, to, "stream")
require.NoError(t, err, "stream call failed to start")
require.NoError(t, NewArgWriter(call.Arg2Writer()).Write(nil), "write arg2")
require.NoError(t, NewArgWriter(call.Arg3Writer()).Write(nil), "write arg3")
go func() {
var d []byte
assert.NoError(t, NewArgReader(call.Response().Arg2Reader()).Read(&d), "read arg2 from %v to %v", from.PeerInfo(), to.PeerInfo())
assert.NoError(t, NewArgReader(call.Response().Arg3Reader()).Read(&d), "read arg3")
c <- struct{}{}
}()
return c
}
func (t *closeSemanticsTest) runTest() {
s1, s1C := t.makeServer("s1")
s2, s2C := t.makeServer("s2")
// Make a call from s1 -> s2, and s2 -> s1
call1 := t.callStream(s1, s2)
call2 := t.callStream(s2, s1)
// s1 and s2 are both open, so calls to it should be successful.
t.withNewClient(func(ch *Channel) {
require.NoError(t, t.call(ch, s1), "failed to call s1")
require.NoError(t, t.call(ch, s2), "failed to call s2")
})
require.NoError(t, t.call(s1, s2), "call s1 -> s2 failed")
require.NoError(t, t.call(s2, s1), "call s2 -> s1 failed")
// Close s1, should no longer be able to call it.
s1.Close()
assert.Equal(t, ChannelStartClose, s1.State())
t.withNewClient(func(ch *Channel) {
assert.Error(t, t.call(ch, s1), "closed channel should not accept incoming calls")
require.NoError(t, t.call(ch, s2),
"closed channel with pending incoming calls should allow outgoing calls")
})
// Even an existing connection (e.g. from s2) should fail.
// TODO: this will fail until the peer is shared.
if !assert.Equal(t, ErrChannelClosed, t.call(s2, s1),
"closed channel should not accept incoming calls") {
t.Errorf("err %v", t.call(s2, s1))
}
require.Error(t, t.call(s1, s2),
"closed channel with pending incoming calls disallows outgoing calls")
// Once the incoming connection is drained, outgoing calls should fail.
s1C <- struct{}{}
<-call2
assertStateChangesTo(t.T, s1, ChannelInboundClosed)
require.Error(t, t.call(s1, s2),
"closed channel with no pending incoming calls should not allow outgoing calls")
// Now the channel should be completely closed as there are no pending connections.
s2C <- struct{}{}
<-call1
assertStateChangesTo(t.T, s1, ChannelClosed)
// Close s2 so we don't leave any goroutines running.
s2.Close()
}
func TestCloseSemantics(t *testing.T) {
// We defer the check as we want it to run after the SetTimeout clears the timeout.
defer goroutines.VerifyNoLeaks(t, nil)
defer testutils.SetTimeout(t, 2*time.Second)()
ct := &closeSemanticsTest{t, false /* isolated */}
ct.runTest()
}
func TestCloseSemanticsIsolated(t *testing.T) {
// We defer the check as we want it to run after the SetTimeout clears the timeout.
defer goroutines.VerifyNoLeaks(t, nil)
defer testutils.SetTimeout(t, 2*time.Second)()
ct := &closeSemanticsTest{t, true /* isolated */}
ct.runTest()
}
func TestCloseSingleChannel(t *testing.T) {
ch := testutils.NewServer(t, nil)
var connected sync.WaitGroup
var completed sync.WaitGroup
blockCall := make(chan struct{})
testutils.RegisterFunc(ch, "echo", func(ctx context.Context, args *raw.Args) (*raw.Res, error) {
connected.Done()
<-blockCall
return &raw.Res{
Arg2: args.Arg2,
Arg3: args.Arg3,
}, nil
})
for i := 0; i < 10; i++ {
connected.Add(1)
completed.Add(1)
go func() {
ctx, cancel := NewContext(time.Second)
defer cancel()
peerInfo := ch.PeerInfo()
_, _, _, err := raw.Call(ctx, ch, peerInfo.HostPort, peerInfo.ServiceName, "echo", nil, nil)
assert.NoError(t, err, "Call failed")
completed.Done()
}()
}
// Wait for all calls to connect before triggerring the Close (so they do not fail).
connected.Wait()
ch.Close()
// Unblock the calls, and wait for all the calls to complete.
close(blockCall)
completed.Wait()
// Once all calls are complete, the channel should be closed.
assertStateChangesTo(t, ch, ChannelClosed)
goroutines.VerifyNoLeaks(t, nil)
}
func TestCloseOneSide(t *testing.T) {
ch1 := testutils.NewServer(t, &testutils.ChannelOpts{ServiceName: "client"})
ch2 := testutils.NewServer(t, &testutils.ChannelOpts{ServiceName: "server"})
connected := make(chan struct{})
completed := make(chan struct{})
blockCall := make(chan struct{})
testutils.RegisterFunc(ch2, "echo", func(ctx context.Context, args *raw.Args) (*raw.Res, error) {
connected <- struct{}{}
<-blockCall
return &raw.Res{
Arg2: args.Arg2,
Arg3: args.Arg3,
}, nil
})
go func() {
ctx, cancel := NewContext(time.Second)
defer cancel()
ch2Peer := ch2.PeerInfo()
_, _, _, err := raw.Call(ctx, ch1, ch2Peer.HostPort, ch2Peer.ServiceName, "echo", nil, nil)
assert.NoError(t, err, "Call failed")
completed <- struct{}{}
}()
// Wait for connected before calling Close.
<-connected
ch1.Close()
// Now unblock the call and wait for the call to complete.
close(blockCall)
<-completed
// Once the call completes, the channel should be closed.
assertStateChangesTo(t, ch1, ChannelClosed)
// We need to close all open TChannels before verifying blocked goroutines.
ch2.Close()
goroutines.VerifyNoLeaks(t, nil)
}
// TestCloseSendError tests that system errors are not attempted to be sent when
// a connection is closed, and ensures there's no race conditions such as the error
// frame being added to the channel just as it is closed.
func TestCloseSendError(t *testing.T) {
var (
closed atomic.Uint32
counter atomic.Uint32
)
opts := testutils.NewOpts().DisableLogVerification()
serverCh := testutils.NewServer(t, opts)
testutils.RegisterEcho(serverCh, func() {
if counter.Inc() > 10 {
// Close the server in a goroutine to possibly trigger more race conditions.
go func() {
closed.Inc()
serverCh.Close()
}()
}
})
clientCh := testutils.NewClient(t, opts)
// Create a connection that will be shared.
require.NoError(t, testutils.Ping(clientCh, serverCh), "Ping from client to server failed")
var wg sync.WaitGroup
for i := 0; i < 100; i++ {
wg.Add(1)
go func() {
time.Sleep(time.Duration(rand.Intn(1000)) * time.Microsecond)
err := testutils.CallEcho(clientCh, serverCh.PeerInfo().HostPort, serverCh.ServiceName(), nil)
if err != nil && closed.Load() == 0 {
t.Errorf("Call failed: %v", err)
}
wg.Done()
}()
}
// Wait for all the goroutines to end
wg.Wait()
clientCh.Close()
goroutines.VerifyNoLeaks(t, nil)
}