proxygen
MockHandler.h
Go to the documentation of this file.
1 /*
2  * Copyright 2017-present Facebook, Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #pragma once
18 
19 #include <folly/MoveWrapper.h>
20 #include <gmock/gmock.h>
21 #include <wangle/channel/Handler.h>
22 
23 namespace wangle {
24 
25 template <class Rin, class Rout = Rin, class Win = Rout, class Wout = Rin>
26 class MockHandler : public Handler<Rin, Rout, Win, Wout> {
27  public:
29 
30  MockHandler() = default;
31  MockHandler(MockHandler&&) = default;
32 
33  MOCK_METHOD2_T(read_, void(Context*, Rin&));
34  MOCK_METHOD1_T(readEOF, void(Context*));
36 
37  MOCK_METHOD2_T(write_, void(Context*, Win&));
38  MOCK_METHOD1_T(close_, void(Context*));
39  MOCK_METHOD2_T(writeException_, void(Context*, folly::exception_wrapper));
40 
41  MOCK_METHOD1_T(attachPipeline, void(Context*));
42  MOCK_METHOD1_T(attachTransport, void(Context*));
43  MOCK_METHOD1_T(detachPipeline, void(Context*));
44  MOCK_METHOD1_T(detachTransport, void(Context*));
45 
46  void read(Context* ctx, Rin msg) override {
47  read_(ctx, msg);
48  }
49 
50  folly::Future<folly::Unit> write(Context* ctx, Win msg) override {
51  return folly::makeFutureWith([&](){
52  write_(ctx, msg);
53  });
54  }
55 
56  folly::Future<folly::Unit> close(Context* ctx) override {
57  return folly::makeFutureWith([&](){
58  close_(ctx);
59  });
60  }
61 
63  Context* ctx, folly::exception_wrapper ex) override {
64  return folly::makeFutureWith([&](){
65  writeException_(ctx, ex);
66  });
67  }
68 };
69 
70 template <class R, class W = R>
72 
74  public:
77  }
78 
79  MOCK_METHOD1(transportActive, void(Context*));
80  MOCK_METHOD1(transportInactive, void(Context*));
81  MOCK_METHOD2(read, void(Context*, folly::IOBufQueue&));
82  MOCK_METHOD1(readEOF, void(Context*));
86  Context*,
87  std::shared_ptr<folly::IOBuf>));
88  MOCK_METHOD1(mockClose,
90  MOCK_METHOD2(mockWriteException,
92  Context*, folly::exception_wrapper));
93 
95  std::unique_ptr<folly::IOBuf> buf) override {
96  std::shared_ptr<folly::IOBuf> sbuf(buf.release());
97  return write(ctx, sbuf).move();
98  }
99 
100  folly::Future<folly::Unit> close(Context* ctx) override {
101  return mockClose(ctx).move();
102  }
103 
105  Context* ctx, folly::exception_wrapper ex) override {
106  return mockWriteException(ctx, ex).move();
107  }
108 };
109 
110 } // namespace wangle
virtual void transportInactive(Context *ctx)
Definition: Handler.h:70
folly::MoveWrapper< folly::Future< folly::Unit > > defaultFuture()
Definition: MockHandler.h:75
Handler< Rin, Rout, Win, Wout >::Context Context
Definition: MockHandler.h:28
MOCK_METHOD2_T(read_, void(Context *, Rin &))
virtual void attachPipeline(HandlerContext< Rout, Wout > *)
Definition: Handler.h:31
folly::Future< folly::Unit > close(Context *ctx) override
Definition: MockHandler.h:100
void read(Context *ctx, Rin msg) override
Definition: MockHandler.h:46
folly::Future< folly::Unit > write(Context *ctx, Win msg) override
Definition: MockHandler.h:50
virtual void detachPipeline(HandlerContext< Rout, Wout > *)
Definition: Handler.h:32
MOCK_METHOD1_T(readEOF, void(Context *))
folly::Future< folly::Unit > writeException(Context *ctx, folly::exception_wrapper ex) override
Definition: MockHandler.h:104
folly::Future< folly::Unit > writeException(Context *ctx, folly::exception_wrapper ex) override
Definition: MockHandler.h:62
#define MOCK_METHOD1(m,...)
folly::Future< folly::Unit > write(Context *ctx, std::unique_ptr< folly::IOBuf > buf) override
Definition: MockHandler.h:94
virtual void readException(Context *ctx, folly::exception_wrapper e)
Definition: Handler.h:64
#define MOCK_METHOD2(m,...)
virtual void transportActive(Context *ctx)
Definition: Handler.h:67
folly::Future< folly::Unit > close(Context *ctx) override
Definition: MockHandler.h:56
virtual void readEOF(Context *ctx)
Definition: Handler.h:61
std::enable_if< isFuture< invoke_result_t< F > >::value, invoke_result_t< F > >::type makeFutureWith(F &&func)
Definition: Future-inl.h:1322