proxygen
Mocks.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 #pragma once
17 
18 #include <folly/MoveWrapper.h>
19 #include <gmock/gmock.h>
20 #include <gtest/gtest.h>
26 
27 namespace wangle {
28 
29 template <typename T, typename R>
30 class MockSubscriber : public Subscriber<T, R> {
31  public:
32  MOCK_METHOD1_T(onNext, void(const T&));
34  MOCK_METHOD0(onCompleted, void());
35 
37 
39 };
40 
41 template <typename T>
43  public:
45 
46  MOCK_METHOD4_T(decode, bool(Context*, folly::IOBufQueue&, T&, size_t&));
47 };
48 
49 template <typename T>
51  public:
53 
54  MOCK_METHOD1_T(mockEncode,
55  folly::MoveWrapper<std::unique_ptr<folly::IOBuf>>(T&));
56 
57  std::unique_ptr<folly::IOBuf> encode(T& data) override {
58  return mockEncode(data).move();
59  }
60 };
61 
62 class MockServerPool : public ServerPool<std::string> {
63  public:
64  explicit MockServerPool(std::shared_ptr<folly::SocketAddress> addr)
65  : ServerPool(), addr_(addr) {}
66 
69  const std::string& /* routingData */) noexcept override {
70  return failConnect_ ? folly::makeFuture<DefaultPipeline*>(std::exception())
71  : client->connect(*addr_);
72  }
73 
74  void failConnect() { failConnect_ = true; }
75 
76  private:
77  std::shared_ptr<folly::SocketAddress> addr_;
78  bool failConnect_{false};
79 };
80 
81 class MockBroadcastPool : public BroadcastPool<int, std::string> {
82  public:
84  : BroadcastPool<int, std::string>(
85  nullptr,
86  nullptr,
87  std::make_shared<ClientBootstrapFactory>()) {}
88 
90  mockGetHandler,
92  const std::string&));
93 
95  const std::string& routingData) override {
96  return mockGetHandler(routingData).move();
97  }
98 };
99 
100 class MockObservingHandler : public ObservingHandler<int, std::string> {
101  public:
103  : ObservingHandler<int, std::string>("", broadcastPool) {}
104 
105  MOCK_METHOD2(mockWrite,
107  MOCK_METHOD1(mockClose,
109 
110  folly::Future<folly::Unit> write(Context* ctx, int data) override {
111  return mockWrite(ctx, data).move();
112  }
113 
114  folly::Future<folly::Unit> close(Context* ctx) override {
115  return mockClose(ctx).move();
116  }
117 };
118 
119 class MockBroadcastHandler : public BroadcastHandler<int, std::string> {
120  public:
122  MOCK_METHOD1(unsubscribe, void(uint64_t));
123 };
124 
126  : public BroadcastPipelineFactory<int, std::string> {
127  public:
129  std::shared_ptr<folly::AsyncTransportWrapper> socket) override {
130  auto pipeline = DefaultPipeline::create();
131  pipeline->addBack(AsyncSocketHandler(socket));
132  pipeline->addBack(std::make_shared<MockByteToMessageDecoder<int>>());
133  pipeline->addBack(BroadcastHandler<int, std::string>());
134  pipeline->finalize();
135 
136  return pipeline;
137  }
138 
140  DefaultPipeline* pipeline) noexcept override {
141  return pipeline->getHandler<BroadcastHandler<int, std::string>>(2);
142  }
143 
144  MOCK_METHOD2(setRoutingData, void(DefaultPipeline*, const std::string&));
145 };
146 
148  : public ObservingPipelineFactory<int, std::string> {
149  public:
151  std::shared_ptr<ServerPool<std::string>> serverPool,
153  broadcastPipelineFactory)
154  : ObservingPipelineFactory(serverPool, broadcastPipelineFactory) {}
155 
157  std::shared_ptr<folly::AsyncTransportWrapper>,
158  const std::string& routingData,
160  std::shared_ptr<TransportInfo>) override {
161  auto pipeline = ObservingPipeline<int>::create();
162  pipeline->addBack(std::make_shared<wangle::BytesToBytesHandler>());
163  pipeline->addBack(std::make_shared<MockMessageToByteEncoder<int>>());
164  auto handler = std::make_shared<ObservingHandler<int, std::string>>(
165  routingData, broadcastPool());
166  pipeline->addBack(handler);
167  pipeline->finalize();
168 
169  return pipeline;
170  }
171 };
172 
173 } // namespace wangle
#define MOCK_METHOD4_T(m,...)
void failConnect()
Definition: Mocks.h:74
#define T(v)
Definition: http_parser.c:233
virtual void onError(folly::exception_wrapper ex)=0
MockObservingPipelineFactory(std::shared_ptr< ServerPool< std::string >> serverPool, std::shared_ptr< BroadcastPipelineFactory< int, std::string >> broadcastPipelineFactory)
Definition: Mocks.h:150
ObservingPipeline< int >::Ptr newPipeline(std::shared_ptr< folly::AsyncTransportWrapper >, const std::string &routingData, RoutingDataHandler< std::string > *, std::shared_ptr< TransportInfo >) override
Definition: Mocks.h:156
MOCK_METHOD0(onCompleted, void())
folly::Future< BroadcastHandler< int, std::string > * > getHandler()
DefaultPipeline::Ptr newPipeline(std::shared_ptr< folly::AsyncTransportWrapper > socket) override
Definition: Mocks.h:128
void subscribe(uint32_t iters, int N)
Definition: RxBenchmark.cpp:54
folly::Future< folly::Unit > write(Context *ctx, int data) override
Definition: Mocks.h:110
TokenBindingMessage decode(folly::io::Cursor &cursor)
Definition: Types.cpp:132
STL namespace.
MOCK_METHOD1_T(onNext, void(const T &))
MOCK_METHOD1(onError, void(const folly::exception_wrapper ex))
requires E e noexcept(noexcept(s.error(std::move(e))))
MessageToByteEncoder< T >::Context Context
Definition: Mocks.h:52
#define nullptr
Definition: http_parser.c:41
virtual void onNext(const T &)=0
folly::Future< BroadcastHandler< int, std::string > * > getHandler(const std::string &routingData) override
Definition: Mocks.h:94
std::shared_ptr< folly::SocketAddress > addr_
Definition: Mocks.h:77
MOCK_METHOD0_T(routingData, R &())
void handler(int, siginfo_t *, void *)
MockObservingHandler(BroadcastPool< int, std::string > *broadcastPool)
Definition: Mocks.h:102
virtual folly::Future< P * > connect(const folly::SocketAddress &address, std::chrono::milliseconds timeout=std::chrono::milliseconds(0))=0
ByteToMessageDecoder< T >::Context Context
Definition: Mocks.h:44
NetworkSocket socket(int af, int type, int protocol)
Definition: NetOps.cpp:412
virtual void onCompleted()=0
virtual R & routingData()=0
std::unique_ptr< folly::IOBuf > encode(T &data) override
Definition: Mocks.h:57
MockServerPool(std::shared_ptr< folly::SocketAddress > addr)
Definition: Mocks.h:64
const char * string
Definition: Conv.cpp:212
static Ptr create()
Definition: Pipeline.h:174
BroadcastHandler< int, std::string > * getBroadcastHandler(DefaultPipeline *pipeline) noexceptoverride
Definition: Mocks.h:139
std::shared_ptr< Pipeline > Ptr
Definition: Pipeline.h:172
folly::Future< folly::Unit > close(Context *ctx) override
Definition: Mocks.h:114
folly::Future< DefaultPipeline * > connect(BaseClientBootstrap< DefaultPipeline > *client, const std::string &) noexceptoverride
Definition: Mocks.h:67
#define MOCK_METHOD2(m,...)
static constexpr uint64_t data[1]
Definition: Fingerprint.cpp:43
ThreadPoolListHook * addr