taozle | c0d384a | 2017-07-17 18:40:42 +0200 | [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 | |
Andrew Boyle | 00c039a | 2020-04-27 11:32:24 -0700 | [diff] [blame] | 22 | import ( |
| 23 | "context" |
| 24 | "fmt" |
| 25 | ) |
taozle | c0d384a | 2017-07-17 18:40:42 +0200 | [diff] [blame] | 26 | |
| 27 | type mockProcessor struct { |
| 28 | ProcessFunc func(in, out TProtocol) (bool, TException) |
| 29 | } |
| 30 | |
| 31 | func (m *mockProcessor) Process(ctx context.Context, in, out TProtocol) (bool, TException) { |
| 32 | return m.ProcessFunc(in, out) |
| 33 | } |
Andrew Boyle | 00c039a | 2020-04-27 11:32:24 -0700 | [diff] [blame] | 34 | |
| 35 | func (m *mockProcessor) ProcessorMap() map[string]TProcessorFunction { |
| 36 | return map[string]TProcessorFunction{ |
| 37 | "mock": WrappedTProcessorFunction{ |
| 38 | Wrapped: func(ctx context.Context, seqId int32, in, out TProtocol) (bool, TException) { |
| 39 | return m.ProcessFunc(in, out) |
| 40 | }, |
| 41 | }, |
| 42 | } |
| 43 | } |
| 44 | |
| 45 | func (m *mockProcessor) AddToProcessorMap(name string, processorFunc TProcessorFunction) {} |
| 46 | |
| 47 | type mockWrappedProcessorContextKey int |
| 48 | |
| 49 | const ( |
| 50 | processorName mockWrappedProcessorContextKey = iota |
| 51 | ) |
| 52 | |
| 53 | // setMockWrappableProcessorName sets the "name" of the TProcessorFunction to |
| 54 | // call on a mockWrappableProcessor when calling Process. |
| 55 | // |
| 56 | // In a normal TProcessor, the request name is read from the request itself |
| 57 | // which happens in TProcessor.Process, so it is not passed into the call to |
| 58 | // Process itself, to get around this in testing, mockWrappableProcessor calls |
| 59 | // getMockWrappableProcessorName to get the name to use from the context |
| 60 | // object. |
| 61 | func setMockWrappableProcessorName(ctx context.Context, name string) context.Context { |
| 62 | return context.WithValue(ctx, processorName, name) |
| 63 | } |
| 64 | |
| 65 | // getMockWrappableProcessorName gets the "name" of the TProcessorFunction to |
| 66 | // call on a mockWrappableProcessor when calling Process. |
| 67 | func getMockWrappableProcessorName(ctx context.Context) (string, bool) { |
| 68 | val, ok := ctx.Value(processorName).(string) |
| 69 | return val, ok |
| 70 | } |
| 71 | |
| 72 | // mockWrappableProcessor can be used to create a mock object that fufills the |
| 73 | // TProcessor interface in testing. |
| 74 | type mockWrappableProcessor struct { |
| 75 | ProcessorFuncs map[string]TProcessorFunction |
| 76 | } |
| 77 | |
| 78 | // Process calls the TProcessorFunction assigned to the "name" set on the |
| 79 | // context object by setMockWrappableProcessorName. |
| 80 | // |
| 81 | // If no name is set on the context or there is no TProcessorFunction mapped to |
| 82 | // that name, the call will panic. |
| 83 | func (p *mockWrappableProcessor) Process(ctx context.Context, in, out TProtocol) (bool, TException) { |
| 84 | name, ok := getMockWrappableProcessorName(ctx) |
| 85 | if !ok { |
| 86 | panic("MockWrappableProcessorName not set on context") |
| 87 | } |
| 88 | processor, ok := p.ProcessorMap()[name] |
| 89 | if !ok { |
| 90 | panic(fmt.Sprintf("No processor set for name %q", name)) |
| 91 | } |
| 92 | return processor.Process(ctx, 0, in, out) |
| 93 | } |
| 94 | |
| 95 | func (p *mockWrappableProcessor) ProcessorMap() map[string]TProcessorFunction { |
| 96 | return p.ProcessorFuncs |
| 97 | } |
| 98 | |
| 99 | func (p *mockWrappableProcessor) AddToProcessorMap(name string, processorFunc TProcessorFunction) { |
| 100 | p.ProcessorFuncs[name] = processorFunc |
| 101 | } |
| 102 | |
| 103 | var ( |
| 104 | _ TProcessor = (*mockProcessor)(nil) |
| 105 | _ TProcessor = (*mockWrappableProcessor)(nil) |
| 106 | ) |