go.uber.org/yarpc@v1.72.1/transport/transports_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 transport_test 22 23 import ( 24 "context" 25 "testing" 26 "time" 27 28 "github.com/stretchr/testify/assert" 29 "go.uber.org/yarpc" 30 "go.uber.org/yarpc/api/middleware" 31 "go.uber.org/yarpc/api/transport" 32 "go.uber.org/yarpc/yarpctest" 33 ) 34 35 func TestFirstOutboundMiddleware(t *testing.T) { 36 // This ensures that the meta middleware is applied before all user specified 37 // middleware. If the meta middleware is installed properly, user middleware 38 // should see the name of the transport, instead of an empty string. 39 40 const ( 41 transportName = "fake" 42 serviceName = "service-name" 43 ) 44 45 newOutboundConfig := func(outboundMiddleware yarpc.OutboundMiddleware) *transport.OutboundConfig { 46 outbound := yarpctest.NewFakeTransport().NewOutbound(nil) 47 48 dispatcher := yarpc.NewDispatcher(yarpc.Config{ 49 Name: serviceName, 50 Outbounds: yarpc.Outbounds{ 51 serviceName: { 52 ServiceName: serviceName, 53 Unary: outbound, 54 Oneway: outbound, 55 Stream: outbound, 56 }, 57 }, 58 OutboundMiddleware: outboundMiddleware, 59 }) 60 return dispatcher.MustOutboundConfig(serviceName) 61 } 62 63 t.Run("unary", func(t *testing.T) { 64 var gotTransportName string 65 66 out := newOutboundConfig(yarpc.OutboundMiddleware{ 67 Unary: middleware.UnaryOutboundFunc(func(ctx context.Context, req *transport.Request, next transport.UnaryOutbound) (*transport.Response, error) { 68 gotTransportName = req.Transport 69 return next.Call(ctx, req) 70 }), 71 }).GetUnaryOutbound() 72 73 ctx, cancel := context.WithTimeout(context.Background(), time.Second) 74 defer cancel() 75 76 // fields required to satisfy validator outbound 77 req := &transport.Request{ 78 Service: "foo", 79 Caller: "foo", 80 Procedure: "foo", 81 Encoding: transport.Encoding("foo"), 82 Transport: "", // unset 83 } 84 85 _, _ = out.Call(ctx, req) 86 assert.Equal(t, transportName, gotTransportName) 87 }) 88 89 t.Run("oneway", func(t *testing.T) { 90 var gotTransportName string 91 92 out := newOutboundConfig(yarpc.OutboundMiddleware{ 93 Oneway: middleware.OnewayOutboundFunc(func(ctx context.Context, req *transport.Request, next transport.OnewayOutbound) (transport.Ack, error) { 94 gotTransportName = req.Transport 95 return next.CallOneway(ctx, req) 96 }), 97 }).GetOnewayOutbound() 98 99 ctx, cancel := context.WithTimeout(context.Background(), time.Second) 100 defer cancel() 101 102 // fields required to satisfy validator outbound 103 req := &transport.Request{ 104 Service: "foo", 105 Caller: "foo", 106 Procedure: "foo", 107 Encoding: transport.Encoding("foo"), 108 Transport: "", // unset 109 } 110 111 _, _ = out.CallOneway(ctx, req) 112 assert.Equal(t, transportName, gotTransportName) 113 }) 114 115 t.Run("stream", func(t *testing.T) { 116 var gotTransportName string 117 118 out := newOutboundConfig(yarpc.OutboundMiddleware{ 119 Stream: middleware.StreamOutboundFunc(func(ctx context.Context, req *transport.StreamRequest, next transport.StreamOutbound) (*transport.ClientStream, error) { 120 gotTransportName = req.Meta.Transport 121 return next.CallStream(ctx, req) 122 }), 123 }).Outbounds.Stream 124 125 ctx, cancel := context.WithTimeout(context.Background(), time.Second) 126 defer cancel() 127 128 // fields required to satisfy validator outbound 129 req := &transport.StreamRequest{ 130 Meta: &transport.RequestMeta{ 131 Service: "foo", 132 Caller: "foo", 133 Procedure: "foo", 134 Encoding: transport.Encoding("foo"), 135 Transport: "", // unset 136 }, 137 } 138 139 _, _ = out.CallStream(ctx, req) 140 assert.Equal(t, transportName, gotTransportName) 141 }) 142 }