go.uber.org/yarpc@v1.72.1/transport/grpc/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 grpc
    22  
    23  import (
    24  	"context"
    25  	"net"
    26  	"testing"
    27  	"time"
    28  
    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/yarpctest"
    34  	"go.uber.org/yarpc/peer/hostport"
    35  )
    36  
    37  var spec = integrationtest.TransportSpec{
    38  	Identify: hostport.Identify,
    39  	NewServerTransport: func(t *testing.T, addr string) peer.Transport {
    40  		return NewTransport(BackoffStrategy(backoff.None))
    41  	},
    42  	NewClientTransport: func(t *testing.T) peer.Transport {
    43  		return NewTransport(BackoffStrategy(backoff.None))
    44  	},
    45  	NewUnaryOutbound: func(x peer.Transport, peerChooser peer.Chooser) transport.UnaryOutbound {
    46  		return x.(*Transport).NewOutbound(peerChooser)
    47  	},
    48  	NewInbound: func(t peer.Transport, address string) transport.Inbound {
    49  		listener, err := net.Listen("tcp", address)
    50  		if err != nil {
    51  			panic(err.Error())
    52  		}
    53  		return t.(*Transport).NewInbound(listener)
    54  	},
    55  	Addr: func(_ peer.Transport, inbound transport.Inbound) string {
    56  		return yarpctest.ZeroAddrToHostPort(inbound.(*Inbound).listener.Addr())
    57  	},
    58  }
    59  
    60  func TestPeerWithRoundRobin(t *testing.T) {
    61  	ctx := context.Background()
    62  	ctx, cancel := context.WithTimeout(ctx, time.Second)
    63  	defer cancel()
    64  
    65  	permanent, permanentAddr := spec.NewServer(t, "127.0.0.1:0")
    66  	defer permanent.Stop()
    67  
    68  	temporary, temporaryAddr := spec.NewServer(t, "127.0.0.1:0")
    69  	defer temporary.Stop()
    70  
    71  	// Construct a client with a bank of peers. We will keep one running all
    72  	// the time. We'll shut one down temporarily.
    73  	// The round robin peer list should only choose peers that have
    74  	// successfully connected.
    75  	client, c := spec.NewClient(t, []string{
    76  		permanentAddr,
    77  		temporaryAddr,
    78  	})
    79  	defer client.Stop()
    80  
    81  	integrationtest.Blast(ctx, t, c)
    82  
    83  	// Shut down one task in the peer list.
    84  	temporary.Stop()
    85  	// One of these requests may fail since one of the peers has gone down but
    86  	// the gRPC transport will not know until a request is attempted.
    87  	integrationtest.Call(ctx, c)
    88  	integrationtest.Call(ctx, c)
    89  	// All subsequent should succeed since the peer should be removed on
    90  	// connection fail.
    91  	integrationtest.Blast(ctx, t, c)
    92  
    93  	// Restore the server on the temporary port.
    94  	restored, _ := spec.NewServer(t, temporaryAddr)
    95  	defer restored.Stop()
    96  	integrationtest.Blast(ctx, t, c)
    97  }
    98  
    99  func TestPeerIntegration(t *testing.T) {
   100  	t.Skip("Skipping due to test flakiness")
   101  	spec.Test(t)
   102  }