go.uber.org/yarpc@v1.72.1/yarpcconfig/mock_transport_spec_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 yarpcconfig
    22  
    23  import (
    24  	"fmt"
    25  	"reflect"
    26  
    27  	"github.com/golang/mock/gomock"
    28  	"go.uber.org/yarpc/api/transport"
    29  )
    30  
    31  // Builds mockTransportSpec objects.
    32  //
    33  // 	mockSpec := mockTransportSpecBuilder{
    34  // 		Name: "...",
    35  // 		TransportConfig: reflect.TypeOf(myConfig{}),
    36  // 	}.Build()
    37  //
    38  // 	mockSpec.EXPECT().BuildTransport(myConfig{...}).Return(...)
    39  // 	mockSpec.Spec()
    40  type mockTransportSpecBuilder struct {
    41  	Name            string
    42  	TransportConfig reflect.Type
    43  
    44  	// Any of the following may be nil to indicate that the transport does not
    45  	// support that functionality.
    46  
    47  	InboundConfig        reflect.Type
    48  	UnaryOutboundConfig  reflect.Type
    49  	OnewayOutboundConfig reflect.Type
    50  	StreamOutboundConfig reflect.Type
    51  }
    52  
    53  // build the mockTransportSpec.
    54  func (b mockTransportSpecBuilder) Build(ctrl *gomock.Controller) *mockTransportSpec {
    55  	s := TransportSpec{Name: b.Name}
    56  	m := mockTransportSpec{ctrl: ctrl, spec: &s}
    57  
    58  	// Build a Spec where the Build* functions point to a dummy function
    59  	// (generated by builderFunc) which calls into the mock controller to get
    60  	// the value to return.
    61  
    62  	s.BuildTransport = builderFunc(ctrl, &m, "BuildTransport", []reflect.Type{b.TransportConfig, _typeOfKit}, _typeOfTransport)
    63  
    64  	if b.InboundConfig != nil {
    65  		s.BuildInbound = builderFunc(ctrl, &m, "BuildInbound", []reflect.Type{b.InboundConfig, _typeOfTransport, _typeOfKit}, _typeOfInbound)
    66  	}
    67  	if b.UnaryOutboundConfig != nil {
    68  		s.BuildUnaryOutbound = builderFunc(ctrl, &m, "BuildUnaryOutbound", []reflect.Type{b.UnaryOutboundConfig, _typeOfTransport, _typeOfKit}, _typeOfUnaryOutbound)
    69  	}
    70  	if b.OnewayOutboundConfig != nil {
    71  		s.BuildOnewayOutbound = builderFunc(ctrl, &m, "BuildOnewayOutbound", []reflect.Type{b.OnewayOutboundConfig, _typeOfTransport, _typeOfKit}, _typeOfOnewayOutbound)
    72  	}
    73  	if b.StreamOutboundConfig != nil {
    74  		s.BuildStreamOutbound = builderFunc(ctrl, &m, "BuildStreamOutbound", []reflect.Type{b.StreamOutboundConfig, _typeOfTransport, _typeOfKit}, _typeOfStreamOutbound)
    75  	}
    76  
    77  	return &m
    78  }
    79  
    80  // mockTransportSpec sets up a fake TransportSpec. The underlying Spec can be
    81  // obtained using the Spec function.
    82  type mockTransportSpec struct {
    83  	spec *TransportSpec
    84  	ctrl *gomock.Controller
    85  }
    86  
    87  // The following Build* functions are never called directly. They're just
    88  // there to let gomock verify the signatures and return types.
    89  
    90  func (m *mockTransportSpec) BuildTransport(interface{}, gomock.Matcher) (transport.Transport, error) {
    91  	panic("This function should never be called")
    92  }
    93  
    94  func (m *mockTransportSpec) BuildInbound(interface{}, transport.Transport, gomock.Matcher) (transport.Inbound, error) {
    95  	panic("This function should never be called")
    96  }
    97  
    98  func (m *mockTransportSpec) BuildUnaryOutbound(interface{}, transport.Transport, gomock.Matcher) (transport.UnaryOutbound, error) {
    99  	panic("This function should never be called")
   100  }
   101  
   102  func (m *mockTransportSpec) BuildOnewayOutbound(interface{}, transport.Transport, gomock.Matcher) (transport.OnewayOutbound, error) {
   103  	panic("This function should never be called")
   104  }
   105  
   106  func (m *mockTransportSpec) BuildStreamOutbound(interface{}, transport.Transport, gomock.Matcher) (transport.StreamOutbound, error) {
   107  	panic("This function should never be called")
   108  }
   109  
   110  // EXPECT may be used to define expectations on the TransportSpec.
   111  func (m *mockTransportSpec) EXPECT() *_transportSpecRecorder {
   112  	return &_transportSpecRecorder{m: m, ctrl: m.ctrl}
   113  }
   114  
   115  // Spec returns a TransportSpec based on the expectations set on this
   116  // mockTransportSpec.
   117  func (m *mockTransportSpec) Spec() TransportSpec {
   118  	return *m.spec
   119  }
   120  
   121  // Provides functions to record TransportSpec expectations.
   122  type _transportSpecRecorder struct {
   123  	m    *mockTransportSpec
   124  	ctrl *gomock.Controller
   125  }
   126  
   127  func (r *_transportSpecRecorder) BuildTransport(cfg interface{}, kit gomock.Matcher) *gomock.Call {
   128  	return r.ctrl.RecordCall(r.m, "BuildTransport", cfg, kit)
   129  }
   130  
   131  func (r *_transportSpecRecorder) BuildInbound(cfg interface{}, t transport.Transport, kit gomock.Matcher) *gomock.Call {
   132  	return r.ctrl.RecordCall(r.m, "BuildInbound", cfg, t, kit)
   133  }
   134  
   135  func (r *_transportSpecRecorder) BuildUnaryOutbound(cfg interface{}, t transport.Transport, kit gomock.Matcher) *gomock.Call {
   136  	return r.ctrl.RecordCall(r.m, "BuildUnaryOutbound", cfg, t, kit)
   137  }
   138  
   139  func (r *_transportSpecRecorder) BuildOnewayOutbound(cfg interface{}, t transport.Transport, kit gomock.Matcher) *gomock.Call {
   140  	return r.ctrl.RecordCall(r.m, "BuildOnewayOutbound", cfg, t, kit)
   141  }
   142  
   143  func (r *_transportSpecRecorder) BuildStreamOutbound(cfg interface{}, t transport.Transport, kit gomock.Matcher) *gomock.Call {
   144  	return r.ctrl.RecordCall(r.m, "BuildStreamOutbound", cfg, t, kit)
   145  }
   146  
   147  // kitMatcher matches attributes of a kit
   148  type kitMatcher struct {
   149  	ServiceName         string
   150  	OutboundServiceName string
   151  }
   152  
   153  func (m kitMatcher) Matches(x interface{}) bool {
   154  	k, ok := x.(*Kit)
   155  	if !ok {
   156  		return false
   157  	}
   158  
   159  	return k.ServiceName() == m.ServiceName && k.OutboundServiceName() == m.OutboundServiceName
   160  }
   161  
   162  func (m kitMatcher) String() string {
   163  	return fmt.Sprintf("kit{name: %q, outboundName: %q}", m.ServiceName, m.OutboundServiceName)
   164  }