go.uber.org/yarpc@v1.72.1/transport/grpc/transport_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  	"net"
    25  	"testing"
    26  
    27  	"github.com/stretchr/testify/assert"
    28  	"github.com/stretchr/testify/require"
    29  	"go.uber.org/yarpc/api/peer"
    30  	"google.golang.org/grpc"
    31  )
    32  
    33  func TestTransportLifecycle(t *testing.T) {
    34  	transport := NewTransport()
    35  	assert.NoError(t, transport.Start())
    36  	assert.True(t, transport.IsRunning())
    37  	assert.NoError(t, transport.Stop())
    38  	assert.False(t, transport.IsRunning())
    39  }
    40  
    41  func TestRetainReleasePeerSuccess(t *testing.T) {
    42  	listener, err := net.Listen("tcp", "127.0.0.1:0")
    43  	require.NoError(t, err)
    44  
    45  	grpcServer := grpc.NewServer()
    46  	go grpcServer.Serve(listener)
    47  	defer grpcServer.Stop()
    48  
    49  	transport := NewTransport()
    50  	assert.NoError(t, transport.Start())
    51  	defer func() { assert.NoError(t, transport.Stop()) }()
    52  
    53  	address := listener.Addr().String()
    54  	peerSubscriber := testPeerSubscriber{}
    55  
    56  	peer, err := transport.RetainPeer(testIdentifier{address}, peerSubscriber)
    57  	assert.NoError(t, err)
    58  	assert.Equal(t, peer, transport.addressToPeer[address])
    59  	assert.NoError(t, transport.ReleasePeer(testIdentifier{address}, peerSubscriber))
    60  }
    61  
    62  func TestRetainReleasePeerErrorPeerIdentifier(t *testing.T) {
    63  	transport := NewTransport()
    64  	assert.NoError(t, transport.Start())
    65  	defer func() { assert.NoError(t, transport.Stop()) }()
    66  }
    67  
    68  func TestReleasePeerErrorNoPeer(t *testing.T) {
    69  	transport := NewTransport()
    70  	assert.NoError(t, transport.Start())
    71  	defer func() { assert.NoError(t, transport.Stop()) }()
    72  
    73  	address := "not_retained"
    74  	peerSubscriber := testPeerSubscriber{}
    75  
    76  	assert.Equal(t, peer.ErrTransportHasNoReferenceToPeer{
    77  		TransportName:  "grpc.Transport",
    78  		PeerIdentifier: address,
    79  	}, transport.ReleasePeer(testIdentifier{address}, peerSubscriber))
    80  }
    81  
    82  type testPeerSubscriber struct{}
    83  
    84  func (testPeerSubscriber) NotifyStatusChanged(peer.Identifier) {}
    85  
    86  type testIdentifier struct {
    87  	id string
    88  }
    89  
    90  func (i testIdentifier) Identifier() string {
    91  	return i.id
    92  }