go.uber.org/yarpc@v1.72.1/transport/tchannel/outbound_channel_test.go (about) 1 // Copyright (c) 2022 Uber Technologies, Inc. 2 // 3 // Permission is hereby granted, free of charge, to any person obtaining a copy 4 // of this software and associated documentation files (the "Software"), to deal 5 // in the Software without restriction, including without limitation the rights 6 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 7 // copies of the Software, and to permit persons to whom the Software is 8 // furnished to do so, subject to the following conditions: 9 // 10 // The above copyright notice and this permission notice shall be included in 11 // all copies or substantial portions of the Software. 12 // 13 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 18 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 19 // THE SOFTWARE. 20 21 package tchannel 22 23 import ( 24 "bytes" 25 "net" 26 "testing" 27 28 "github.com/stretchr/testify/assert" 29 "github.com/stretchr/testify/require" 30 "github.com/uber/tchannel-go" 31 "github.com/uber/tchannel-go/testutils" 32 "go.uber.org/atomic" 33 "go.uber.org/yarpc/api/transport" 34 "go.uber.org/yarpc/encoding/raw" 35 "go.uber.org/yarpc/internal/testtime" 36 "go.uber.org/yarpc/peer" 37 "go.uber.org/yarpc/peer/hostport" 38 "golang.org/x/net/context" 39 ) 40 41 func TestOutboundChannel(t *testing.T) { 42 server := testutils.NewServer(t, nil) 43 defer server.Close() 44 serverHostPort := server.PeerInfo().HostPort 45 46 var handlerInvoked bool 47 server.GetSubChannel("service").SetHandler(tchannel.HandlerFunc( 48 func(ctx context.Context, call *tchannel.InboundCall) { 49 handlerInvoked = true 50 _, err := readHeaders(tchannel.Raw, call.Arg2Reader) 51 if !assert.NoError(t, err, "failed to read request") { 52 return 53 } 54 55 // write a response 56 err = writeArgs(call.Response(), []byte{0x00, 0x00}, []byte("")) 57 assert.NoError(t, err, "failed to write response") 58 }), 59 ) 60 61 opts := []TransportOption{ServiceName("caller")} 62 trans, err := NewTransport(opts...) 63 require.NoError(t, err) 64 65 // Atomic bool to avoid data race as dialer can be invoked twice 66 // due to race between tchannel outbound and tchannel peer. 67 var dialerInvoked atomic.Bool 68 dialerFunc := func(ctx context.Context, network, hostPort string) (net.Conn, error) { 69 dialerInvoked.Store(true) 70 return (&net.Dialer{}).DialContext(ctx, network, hostPort) 71 } 72 outboundChannel, err := trans.createOutboundChannel(dialerFunc) 73 require.NoError(t, err) 74 75 require.NoError(t, trans.Start(), "failed to start transport") 76 defer trans.Stop() 77 78 out := trans.NewOutbound(peer.NewSingle(hostport.PeerIdentifier(serverHostPort), outboundChannel)) 79 require.NoError(t, out.Start(), "failed to start outbound") 80 defer out.Stop() 81 82 ctx, cancel := context.WithTimeout(context.Background(), 200*testtime.Millisecond) 83 defer cancel() 84 _, err = out.Call( 85 ctx, 86 &transport.Request{ 87 Caller: "caller", 88 Service: "service", 89 Encoding: raw.Encoding, 90 Procedure: "hello", 91 Body: bytes.NewBufferString("body"), 92 }, 93 ) 94 require.NoError(t, err, "failed to make call") 95 assert.True(t, handlerInvoked, "handler was never called by client") 96 assert.True(t, dialerInvoked.Load(), "dialer was not called") 97 } 98 99 func TestOutboundChannelFailure(t *testing.T) { 100 transport, err := NewTransport(ServiceName("svc")) 101 require.NoError(t, err) 102 require.NoError(t, transport.Start()) 103 defer transport.Stop() 104 105 _, err = transport.createOutboundChannel(nil) 106 assert.EqualError(t, err, "tchannel outbound channel cannot be created after starting transport") 107 }