// convertOutbounds applys filters and creates validator outbounds func convertOutbounds(outbounds Outbounds, filter transport.Filter) Outbounds { //TODO(apb): ensure we're not given the same underlying outbound for each RPC type convertedOutbounds := make(Outbounds, len(outbounds)) for service, outs := range outbounds { var ( unaryOutbound transport.UnaryOutbound onewayOutbound transport.OnewayOutbound ) // apply filters and create ValidatorOutbounds if outs.Unary != nil { unaryOutbound = transport.ApplyFilter(outs.Unary, filter) unaryOutbound = request.UnaryValidatorOutbound{UnaryOutbound: unaryOutbound} } // TODO(apb): apply oneway outbound filter if outs.Oneway != nil { onewayOutbound = request.OnewayValidatorOutbound{OnewayOutbound: outs.Oneway} } convertedOutbounds[service] = transport.Outbounds{ Unary: unaryOutbound, Oneway: onewayOutbound, } } return convertedOutbounds }
func TestNopFilter(t *testing.T) { mockCtrl := gomock.NewController(t) defer mockCtrl.Finish() o := transporttest.NewMockUnaryOutbound(mockCtrl) wrappedO := transport.ApplyFilter(o, transport.NopFilter) ctx, cancel := context.WithTimeout(context.Background(), time.Second) defer cancel() req := &transport.Request{ Caller: "somecaller", Service: "someservice", Encoding: raw.Encoding, Procedure: "hello", Body: bytes.NewReader([]byte{1, 2, 3}), } res := &transport.Response{Body: ioutil.NopCloser(bytes.NewReader([]byte{4, 5, 6}))} o.EXPECT().Call(ctx, req).Return(res, nil) got, err := wrappedO.Call(ctx, req) if assert.NoError(t, err) { assert.Equal(t, res, got) } }
func TestChain(t *testing.T) { mockCtrl := gomock.NewController(t) defer mockCtrl.Finish() ctx, cancel := context.WithTimeout(context.Background(), time.Second) defer cancel() req := &transport.Request{ Caller: "somecaller", Service: "someservice", Encoding: transport.Encoding("raw"), Procedure: "hello", Body: bytes.NewReader([]byte{1, 2, 3}), } res := &transport.Response{ Body: ioutil.NopCloser(bytes.NewReader([]byte{4, 5, 6})), } o := transporttest.NewMockUnaryOutbound(mockCtrl) o.EXPECT().Call(ctx, req).After( o.EXPECT().Call(ctx, req).Return(nil, errors.New("great sadness")), ).Return(res, nil) before := &countFilter{} after := &countFilter{} gotRes, err := transport.ApplyFilter( o, Chain(before, retryFilter, after)).Call(ctx, req) assert.NoError(t, err, "expected success") assert.Equal(t, 1, before.Count, "expected outer filter to be called once") assert.Equal(t, 2, after.Count, "expected inner filter to be called twice") assert.Equal(t, res, gotRes, "expected response to match") }