Andrew Boyle | 00c039a | 2020-04-27 11:32:24 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | * or more contributor license agreements. See the NOTICE file |
| 4 | * distributed with this work for additional information |
| 5 | * regarding copyright ownership. The ASF licenses this file |
| 6 | * to you under the Apache License, Version 2.0 (the |
| 7 | * "License"); you may not use this file except in compliance |
| 8 | * with the License. You may obtain a copy of the License at |
| 9 | * |
| 10 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | * |
| 12 | * Unless required by applicable law or agreed to in writing, |
| 13 | * software distributed under the License is distributed on an |
| 14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | * KIND, either express or implied. See the License for the |
| 16 | * specific language governing permissions and limitations |
| 17 | * under the License. |
| 18 | */ |
| 19 | |
| 20 | package thrift |
| 21 | |
| 22 | import ( |
| 23 | "context" |
| 24 | "testing" |
| 25 | ) |
| 26 | |
| 27 | type counter struct { |
| 28 | count int |
| 29 | } |
| 30 | |
| 31 | func (c *counter) incr() { |
| 32 | c.count++ |
| 33 | } |
| 34 | |
Andrew Boyle | 5cffef9 | 2020-04-30 07:12:40 -0700 | [diff] [blame] | 35 | func newCounter(t *testing.T) *counter { |
| 36 | c := counter{} |
| 37 | if c.count != 0 { |
| 38 | t.Fatal("Unexpected initial count.") |
| 39 | } |
| 40 | return &c |
| 41 | } |
| 42 | |
| 43 | func testProcessorMiddleware(c *counter) ProcessorMiddleware { |
Andrew Boyle | 00c039a | 2020-04-27 11:32:24 -0700 | [diff] [blame] | 44 | return func(name string, next TProcessorFunction) TProcessorFunction { |
| 45 | return WrappedTProcessorFunction{ |
| 46 | Wrapped: func(ctx context.Context, seqId int32, in, out TProtocol) (bool, TException) { |
| 47 | c.incr() |
| 48 | return next.Process(ctx, seqId, in, out) |
| 49 | }, |
| 50 | } |
| 51 | } |
| 52 | } |
| 53 | |
Andrew Boyle | 5cffef9 | 2020-04-30 07:12:40 -0700 | [diff] [blame] | 54 | func testClientMiddleware(c *counter) ClientMiddleware { |
| 55 | return func(next TClient) TClient { |
| 56 | return WrappedTClient{ |
Yuxuan 'fishy' Wang | c2ddaf0 | 2021-01-22 09:37:18 -0800 | [diff] [blame] | 57 | Wrapped: func(ctx context.Context, method string, args, result TStruct) (ResponseMeta, error) { |
Andrew Boyle | 5cffef9 | 2020-04-30 07:12:40 -0700 | [diff] [blame] | 58 | c.incr() |
| 59 | return next.Call(ctx, method, args, result) |
| 60 | }, |
| 61 | } |
Andrew Boyle | 00c039a | 2020-04-27 11:32:24 -0700 | [diff] [blame] | 62 | } |
Andrew Boyle | 00c039a | 2020-04-27 11:32:24 -0700 | [diff] [blame] | 63 | } |
| 64 | |
| 65 | func TestWrapProcessor(t *testing.T) { |
| 66 | name := "test" |
| 67 | processor := &mockWrappableProcessor{ |
| 68 | ProcessorFuncs: map[string]TProcessorFunction{ |
| 69 | name: WrappedTProcessorFunction{ |
| 70 | Wrapped: func(ctx context.Context, seqId int32, in, out TProtocol) (bool, TException) { |
| 71 | return true, nil |
| 72 | }, |
| 73 | }, |
| 74 | }, |
| 75 | } |
| 76 | c := newCounter(t) |
| 77 | ctx := setMockWrappableProcessorName(context.Background(), name) |
Andrew Boyle | 5cffef9 | 2020-04-30 07:12:40 -0700 | [diff] [blame] | 78 | wrapped := WrapProcessor(processor, testProcessorMiddleware(c)) |
Andrew Boyle | 00c039a | 2020-04-27 11:32:24 -0700 | [diff] [blame] | 79 | wrapped.Process(ctx, nil, nil) |
| 80 | if c.count != 1 { |
| 81 | t.Fatalf("Unexpected count value %v", c.count) |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | func TestWrapTMultiplexedProcessor(t *testing.T) { |
| 86 | name := "test" |
| 87 | processorName := "foo" |
| 88 | c := newCounter(t) |
| 89 | processor := &TMultiplexedProcessor{} |
| 90 | processor.RegisterDefault(&mockWrappableProcessor{ |
| 91 | ProcessorFuncs: map[string]TProcessorFunction{ |
| 92 | name: WrappedTProcessorFunction{ |
| 93 | Wrapped: func(ctx context.Context, seqId int32, in, out TProtocol) (bool, TException) { |
| 94 | return true, nil |
| 95 | }, |
| 96 | }, |
| 97 | }, |
| 98 | }) |
| 99 | processor.RegisterProcessor(processorName, &mockWrappableProcessor{ |
| 100 | ProcessorFuncs: map[string]TProcessorFunction{ |
| 101 | name: WrappedTProcessorFunction{ |
| 102 | Wrapped: func(ctx context.Context, seqId int32, in, out TProtocol) (bool, TException) { |
| 103 | return true, nil |
| 104 | }, |
| 105 | }, |
| 106 | }, |
| 107 | }) |
Andrew Boyle | 5cffef9 | 2020-04-30 07:12:40 -0700 | [diff] [blame] | 108 | wrapped := WrapProcessor(processor, testProcessorMiddleware(c)) |
Andrew Boyle | 00c039a | 2020-04-27 11:32:24 -0700 | [diff] [blame] | 109 | ctx := setMockWrappableProcessorName(context.Background(), name) |
| 110 | in := NewStoredMessageProtocol(nil, name, 1, 1) |
| 111 | wrapped.Process(ctx, in, nil) |
| 112 | if c.count != 1 { |
| 113 | t.Fatalf("Unexpected count value %v", c.count) |
| 114 | } |
| 115 | |
| 116 | in = NewStoredMessageProtocol(nil, processorName+MULTIPLEXED_SEPARATOR+name, 1, 1) |
| 117 | wrapped.Process(ctx, in, nil) |
| 118 | if c.count != 2 { |
| 119 | t.Fatalf("Unexpected count value %v", c.count) |
| 120 | } |
| 121 | } |
Andrew Boyle | 5cffef9 | 2020-04-30 07:12:40 -0700 | [diff] [blame] | 122 | |
| 123 | func TestWrapClient(t *testing.T) { |
| 124 | client := WrappedTClient{ |
Yuxuan 'fishy' Wang | c2ddaf0 | 2021-01-22 09:37:18 -0800 | [diff] [blame] | 125 | Wrapped: func(ctx context.Context, method string, args, result TStruct) (ResponseMeta, error) { |
| 126 | return ResponseMeta{}, nil |
Andrew Boyle | 5cffef9 | 2020-04-30 07:12:40 -0700 | [diff] [blame] | 127 | }, |
| 128 | } |
| 129 | c := newCounter(t) |
| 130 | wrapped := WrapClient(client, testClientMiddleware(c)) |
| 131 | wrapped.Call(context.Background(), "test", nil, nil) |
| 132 | if c.count != 1 { |
| 133 | t.Fatalf("Unexpected count value %v", c.count) |
| 134 | } |
| 135 | } |