blob: 6c4058fe1335ead0b98a44cb59e25320568506ea [file] [log] [blame]
Andrew Boyle00c039a2020-04-27 11:32:24 -07001/*
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
20package thrift
21
22import (
23 "context"
24 "testing"
25)
26
27type counter struct {
28 count int
29}
30
31func (c *counter) incr() {
32 c.count++
33}
34
Andrew Boyle5cffef92020-04-30 07:12:40 -070035func 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
43func testProcessorMiddleware(c *counter) ProcessorMiddleware {
Andrew Boyle00c039a2020-04-27 11:32:24 -070044 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 Boyle5cffef92020-04-30 07:12:40 -070054func testClientMiddleware(c *counter) ClientMiddleware {
55 return func(next TClient) TClient {
56 return WrappedTClient{
Yuxuan 'fishy' Wangc2ddaf02021-01-22 09:37:18 -080057 Wrapped: func(ctx context.Context, method string, args, result TStruct) (ResponseMeta, error) {
Andrew Boyle5cffef92020-04-30 07:12:40 -070058 c.incr()
59 return next.Call(ctx, method, args, result)
60 },
61 }
Andrew Boyle00c039a2020-04-27 11:32:24 -070062 }
Andrew Boyle00c039a2020-04-27 11:32:24 -070063}
64
65func 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 Boyle5cffef92020-04-30 07:12:40 -070078 wrapped := WrapProcessor(processor, testProcessorMiddleware(c))
Andrew Boyle00c039a2020-04-27 11:32:24 -070079 wrapped.Process(ctx, nil, nil)
80 if c.count != 1 {
81 t.Fatalf("Unexpected count value %v", c.count)
82 }
83}
84
85func 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 Boyle5cffef92020-04-30 07:12:40 -0700108 wrapped := WrapProcessor(processor, testProcessorMiddleware(c))
Andrew Boyle00c039a2020-04-27 11:32:24 -0700109 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 Boyle5cffef92020-04-30 07:12:40 -0700122
123func TestWrapClient(t *testing.T) {
124 client := WrappedTClient{
Yuxuan 'fishy' Wangc2ddaf02021-01-22 09:37:18 -0800125 Wrapped: func(ctx context.Context, method string, args, result TStruct) (ResponseMeta, error) {
126 return ResponseMeta{}, nil
Andrew Boyle5cffef92020-04-30 07:12:40 -0700127 },
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}