go.uber.org/yarpc@v1.72.1/api/peer/peertest/transportaction.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 peertest 22 23 import ( 24 "fmt" 25 "testing" 26 27 "github.com/stretchr/testify/assert" 28 "go.uber.org/yarpc/api/peer" 29 ) 30 31 // TransportDeps are passed through all the TransportActions in order to pass certain 32 // state in between Actions 33 type TransportDeps struct { 34 PeerIdentifiers map[string]peer.Identifier 35 Subscribers map[string]peer.Subscriber 36 } 37 38 // TransportAction defines actions that can be applied to an Transport 39 type TransportAction interface { 40 // Apply runs a function on the Transport and asserts the result 41 Apply(*testing.T, peer.Transport, TransportDeps) 42 } 43 44 // RetainAction will execute the RetainPeer method on the Transport 45 type RetainAction struct { 46 InputIdentifierID string 47 InputSubscriberID string 48 ExpectedErr error 49 ExpectedPeerID string 50 } 51 52 // Apply will execute the RetainPeer method on the Transport 53 func (a RetainAction) Apply(t *testing.T, transport peer.Transport, deps TransportDeps) { 54 peerID := deps.PeerIdentifiers[a.InputIdentifierID] 55 sub := deps.Subscribers[a.InputSubscriberID] 56 57 p, err := transport.RetainPeer(peerID, sub) 58 59 if a.ExpectedErr != nil { 60 assert.Equal(t, a.ExpectedErr, err) 61 assert.Nil(t, p) 62 return 63 } 64 65 if assert.NoError(t, err) && assert.NotNil(t, p) { 66 assert.Equal(t, a.ExpectedPeerID, p.Identifier()) 67 } 68 } 69 70 // ReleaseAction will execute the ReleasePeer method on the Transport 71 type ReleaseAction struct { 72 InputIdentifierID string 73 InputSubscriberID string 74 ExpectedErrType error 75 } 76 77 // Apply will execute the ReleasePeer method on the Transport 78 func (a ReleaseAction) Apply(t *testing.T, transport peer.Transport, deps TransportDeps) { 79 peerID := deps.PeerIdentifiers[a.InputIdentifierID] 80 sub := deps.Subscribers[a.InputSubscriberID] 81 82 err := transport.ReleasePeer(peerID, sub) 83 84 if a.ExpectedErrType != nil && assert.Error(t, err) { 85 assert.IsType(t, a.ExpectedErrType, err) 86 } else { 87 assert.Nil(t, err) 88 } 89 } 90 91 // ApplyTransportActions runs all the TransportActions on the peer Transport 92 func ApplyTransportActions(t *testing.T, transport peer.Transport, actions []TransportAction, d TransportDeps) { 93 for i, action := range actions { 94 t.Run(fmt.Sprintf("action #%d: %T", i, action), func(t *testing.T) { 95 action.Apply(t, transport, d) 96 }) 97 } 98 }