go.uber.org/yarpc@v1.72.1/api/peer/errors_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 peer_test 22 23 import ( 24 "fmt" 25 "testing" 26 27 "go.uber.org/yarpc/api/peer" 28 "go.uber.org/yarpc/api/peer/peertest" 29 30 "github.com/golang/mock/gomock" 31 "github.com/stretchr/testify/assert" 32 ) 33 34 func TestErrPeerHasNoReferenceToSubscriber(t *testing.T) { 35 ctrl := gomock.NewController(t) 36 identifier := peertest.NewMockIdentifier(ctrl) 37 subscriber := peertest.NewMockSubscriber(ctrl) 38 39 wantErr := fmt.Sprintf("peer (%v) has no reference to peer subscriber (%v)", identifier, subscriber) 40 41 err := &peer.ErrPeerHasNoReferenceToSubscriber{PeerIdentifier: identifier, PeerSubscriber: subscriber} 42 assert.Equal(t, wantErr, err.Error()) 43 } 44 45 func TestErrTransportHasNoReferenceToPeer2(t *testing.T) { 46 transportName := "test-transport" 47 peerIdentifier := "test-peer-id" 48 49 wantErr := fmt.Sprintf("transport %q has no reference to peer %q", transportName, peerIdentifier) 50 51 err := &peer.ErrTransportHasNoReferenceToPeer{TransportName: transportName, PeerIdentifier: peerIdentifier} 52 assert.Equal(t, wantErr, err.Error()) 53 } 54 55 func TestErrInvalidPeerType(t *testing.T) { 56 expectedType := "test-type" 57 peerIdentifier := peertest.NewMockIdentifier(gomock.NewController(t)) 58 59 wantErr := fmt.Sprintf("expected peer type (%s) but got peer (%v)", expectedType, peerIdentifier) 60 61 err := &peer.ErrInvalidPeerType{ExpectedType: expectedType, PeerIdentifier: peerIdentifier} 62 assert.Equal(t, wantErr, err.Error()) 63 } 64 65 func TestErrPeerListAlreadyStarted(t *testing.T) { 66 peerList := "test-peer-list" 67 wantErr := fmt.Sprintf("%s has already been started", peerList) 68 69 err := peer.ErrPeerListAlreadyStarted(peerList) 70 assert.Equal(t, wantErr, err.Error()) 71 } 72 73 func TestErrPeerListNotStarted(t *testing.T) { 74 peerList := "test-peer-list" 75 wantErr := fmt.Sprintf("%s has not been started or was stopped", peerList) 76 77 err := peer.ErrPeerListNotStarted(peerList) 78 assert.Equal(t, wantErr, err.Error()) 79 } 80 81 func TestErrInvalidPeerConversion(t *testing.T) { 82 p := peertest.NewMockPeer(gomock.NewController(t)) 83 expectedType := "test-type" 84 85 wantErr := fmt.Sprintf("cannot convert peer (%v) to type %s", p, expectedType) 86 87 err := &peer.ErrInvalidPeerConversion{Peer: p, ExpectedType: expectedType} 88 assert.Equal(t, wantErr, err.Error()) 89 } 90 91 func TestErrInvalidTransportConversion(t *testing.T) { 92 transport := peertest.NewMockTransport(gomock.NewController(t)) 93 expectedType := "test-type" 94 95 wantErr := fmt.Sprintf("cannot convert transport (%v) to type %s", transport, expectedType) 96 97 err := &peer.ErrInvalidTransportConversion{Transport: transport, ExpectedType: expectedType} 98 assert.Equal(t, wantErr, err.Error()) 99 } 100 101 func TestErrPeerAddAlreadyInList(t *testing.T) { 102 p := "test-peer" 103 wantErr := fmt.Sprintf("can't add peer %q because is already in peerlist", p) 104 105 err := peer.ErrPeerAddAlreadyInList(p) 106 assert.Equal(t, wantErr, err.Error()) 107 } 108 109 func TestErrPeerRemoveNotInList(t *testing.T) { 110 p := "test-peer" 111 wantErr := fmt.Sprintf("can't remove peer (%s) because it is not in peerlist", p) 112 113 err := peer.ErrPeerRemoveNotInList(p) 114 assert.Equal(t, wantErr, err.Error()) 115 } 116 117 func TestErrChooseContextHasNoDeadline(t *testing.T) { 118 peerList := "test-peer" 119 wantErr := fmt.Sprintf("can't wait for peer without a context deadline for peerlist %q", peerList) 120 121 err := peer.ErrChooseContextHasNoDeadline(peerList) 122 assert.Equal(t, wantErr, err.Error()) 123 }