go.uber.org/yarpc@v1.72.1/transport/tchannel/peer_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_test
    22  
    23  import (
    24  	"context"
    25  	"net"
    26  	"testing"
    27  
    28  	"github.com/stretchr/testify/require"
    29  	"go.uber.org/yarpc/api/backoff"
    30  	"go.uber.org/yarpc/api/peer"
    31  	"go.uber.org/yarpc/api/transport"
    32  	"go.uber.org/yarpc/internal/integrationtest"
    33  	"go.uber.org/yarpc/internal/testtime"
    34  	"go.uber.org/yarpc/internal/yarpctest"
    35  	"go.uber.org/yarpc/transport/tchannel"
    36  )
    37  
    38  var spec = integrationtest.TransportSpec{
    39  	Identify: identify,
    40  	NewServerTransport: func(t *testing.T, addr string) peer.Transport {
    41  		x, err := tchannel.NewTransport(
    42  			tchannel.ServiceName("service"),
    43  			tchannel.ListenAddr(addr),
    44  		)
    45  		require.NoError(t, err, "must construct transport")
    46  		return x
    47  	},
    48  	NewInbound: func(x peer.Transport, addr string) transport.Inbound {
    49  		return x.(*tchannel.Transport).NewInbound()
    50  	},
    51  	NewClientTransport: func(t *testing.T) peer.Transport {
    52  		x, err := tchannel.NewTransport(
    53  			tchannel.ServiceName("client"),
    54  			tchannel.ConnTimeout(10*testtime.Millisecond),
    55  			tchannel.ConnBackoff(backoff.None),
    56  		)
    57  		require.NoError(t, err, "must construct transport")
    58  		return x
    59  	},
    60  	NewUnaryOutbound: func(x peer.Transport, pc peer.Chooser) transport.UnaryOutbound {
    61  		return x.(*tchannel.Transport).NewOutbound(pc)
    62  	},
    63  	Addr: func(x peer.Transport, ib transport.Inbound) string {
    64  		return yarpctest.ZeroAddrStringToHostPort(x.(*tchannel.Transport).ListenAddr())
    65  	},
    66  }
    67  
    68  // TestWithRoundRobin verifies that TChannel appropriately notifies all
    69  // subscribed peer lists when peers become available and unavailable.
    70  // It does so by constructing a round robin peer list backed by the TChannel transport,
    71  // communicating to three servers. One will always work. One will go down
    72  // temporarily. One will be a bogus TCP port that never completes a TChannel
    73  // handshake.
    74  func TestWithRoundRobin(t *testing.T) {
    75  	t.Skip()
    76  
    77  	ctx := context.Background()
    78  	ctx, cancel := context.WithTimeout(ctx, testtime.Second)
    79  	defer cancel()
    80  
    81  	permanent, permanentAddr := spec.NewServer(t, "")
    82  	defer permanent.Stop()
    83  
    84  	temporary, temporaryAddr := spec.NewServer(t, "")
    85  	defer temporary.Stop()
    86  
    87  	l, err := net.Listen("tcp", "127.0.0.1:0")
    88  	require.NoError(t, err, "listen for bogus server")
    89  	invalidAddr := l.Addr().String()
    90  	defer l.Close()
    91  
    92  	// Construct a client with a bank of peers. We will keep one running all
    93  	// the time. We'll shut one down temporarily. One will be invalid.
    94  	// The round robin peer list should only choose peers that have
    95  	// successfully connected.
    96  	client, c := spec.NewClient(t, []string{
    97  		permanentAddr,
    98  		temporaryAddr,
    99  		invalidAddr,
   100  	})
   101  	defer client.Stop()
   102  
   103  	// All requests should succeed. The invalid peer never enters the rotation.
   104  	integrationtest.Blast(ctx, t, c)
   105  
   106  	// Shut down one task in the peer list.
   107  	temporary.Stop()
   108  	// One of these requests may fail since one of the peers has gone down but
   109  	// the TChannel transport will not know until a request is attempted.
   110  	integrationtest.Call(ctx, c)
   111  	integrationtest.Call(ctx, c)
   112  	// All subsequent should succeed since the peer should be removed on
   113  	// connection fail.
   114  	integrationtest.Blast(ctx, t, c)
   115  
   116  	// Restore the server on the temporary port.
   117  	restored, _ := spec.NewServer(t, temporaryAddr)
   118  	defer restored.Stop()
   119  	integrationtest.Blast(ctx, t, c)
   120  }
   121  
   122  func TestIntegration(t *testing.T) {
   123  	spec.Test(t)
   124  }
   125  
   126  type noSub struct{}
   127  
   128  func (noSub) NotifyStatusChanged(pid peer.Identifier) {}
   129  
   130  func TestCancelMaintainConn(t *testing.T) {
   131  	transport, err := tchannel.NewTransport()
   132  	require.NoError(t, err)
   133  	transport.Start()
   134  	transport.Stop()
   135  	_, err = transport.RetainPeer(identify("127.0.0.1:66408"), noSub{})
   136  	require.NoError(t, err)
   137  }
   138  
   139  func identify(id string) peer.Identifier {
   140  	return &testIdentifier{id}
   141  }
   142  
   143  type testIdentifier struct {
   144  	id string
   145  }
   146  
   147  func (i testIdentifier) Identifier() string {
   148  	return i.id
   149  }