Page Speed Optimization Libraries  1.13.35.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
controller_grpc_mocks.h
Go to the documentation of this file.
1 // Copyright 2016 Google Inc.
16 
17 #ifndef PAGESPEED_CONTROLLER_CONTROLLER_GRPC_MOCKS_H_
18 #define PAGESPEED_CONTROLLER_CONTROLLER_GRPC_MOCKS_H_
19 
20 #include "pagespeed/controller/controller.grpc.pb.h"
21 #include "pagespeed/controller/controller.pb.h"
29 
32 
33 using testing::_;
34 using testing::Eq;
35 using testing::Invoke;
36 using testing::WithArgs;
37 using testing::Return;
38 using testing::SetArgPointee;
39 
40 namespace net_instaweb {
41 
44 
45 class NotifyFunction : public Function {
46  public:
48  : func_(func), sync_(sync) {}
49 
50  void Run() override {
51  func_->CallRun();
52  sync_->Notify();
53  }
54 
55  void Cancel() override {
56  func_->CallCancel();
57  sync_->Notify();
58  }
59 
60  private:
61  Function* func_;
63 };
64 
68 
69 template <typename RequestT, typename ResponseT>
71  : public ::grpc::ClientAsyncReaderWriterInterface<RequestT, ResponseT> {
72  public:
73  MockReaderWriterT(Sequence* sequence) : sequence_(sequence) {
74  EXPECT_CALL(*this, WritesDone(_)).Times(0);
75  EXPECT_CALL(*this, ReadInitialMetadata(_)).Times(0);
76  EXPECT_CALL(*this, Finish(_, _)).Times(0);
77  EXPECT_CALL(*this, Write(_, _)).Times(0);
78  EXPECT_CALL(*this, Write(_, _, _)).Times(0);
79  EXPECT_CALL(*this, Read(_, _)).Times(0);
80  }
81 
82  void ExpectRead(const GoogleString& asciiProto) {
83  ResponseT resp;
84  ASSERT_THAT(ParseTextFormatProtoFromString(asciiProto, &resp), Eq(true));
85 
86  EXPECT_CALL(*this, Read(_, _))
87  .WillOnce(DoAll(
88  SetArgPointee<0>(resp),
89  WithArgs<1>(Invoke(this, &MockReaderWriterT::QueueVoidFunction))));
90  }
91 
92  void ExpectReadFailure() {
93  EXPECT_CALL(*this, Read(_, _))
94  .WillOnce(WithArgs<1>(
95  Invoke(this, &MockReaderWriterT::QueueVoidFunctionForCancel)));
96  }
97 
100  template <typename Matcher>
101  void ExpectWrite(const Matcher& matcher) {
102  EXPECT_CALL(*this, Write(matcher, _))
103  .WillOnce(
104  WithArgs<1>(Invoke(this, &MockReaderWriterT::QueueVoidFunction)));
105  }
106 
107  template <typename Matcher>
108  void ExpectWriteFailure(const Matcher& matcher) {
109  EXPECT_CALL(*this, Write(matcher, _))
110  .WillOnce(WithArgs<1>(
111  Invoke(this, &MockReaderWriterT::QueueVoidFunctionForCancel)));
112  }
113 
114  void ExpectFinish(const ::grpc::Status& status) {
115  EXPECT_CALL(*this, Finish(_, _))
116  .WillOnce(DoAll(
117  SetArgPointee<0>(status),
118  WithArgs<1>(Invoke(this, &MockReaderWriterT::QueueVoidFunction))));
119  }
120 
121  void ExpectFinishAndNotify(const ::grpc::Status& status,
122  WorkerTestBase::SyncPoint* sync) {
123  EXPECT_CALL(*this, Finish(_, _))
124  .WillOnce(DoAll(SetArgPointee<0>(status),
125  WithArgs<1>(Invoke([this, sync](void* f) {
126  this->QueueVoidFunctionWithNotify(f, sync);
127  }))));
128  }
129 
130  void ExpectFinishFailure() {
131  EXPECT_CALL(*this, Finish(_, _))
132  .WillOnce(WithArgs<1>(
133  Invoke(this, &MockReaderWriterT::QueueVoidFunctionForCancel)));
134  }
135 
136  MOCK_METHOD1(WritesDone, void(void* tag));
137  MOCK_METHOD1(ReadInitialMetadata, void(void* tag));
138  MOCK_METHOD2(Finish, void(::grpc::Status* status, void* tag));
139  MOCK_METHOD2_T(Write, void(const RequestT& resp, void* tag));
140  MOCK_METHOD3_T(Write, void(const RequestT& resp, ::grpc::WriteOptions options, void* tag));
141  MOCK_METHOD2_T(Read, void(ResponseT* resp, void* tag));
142 
143  private:
144  void QueueVoidFunction(void* fv) {
145  Function* f = static_cast<Function*>(fv);
146  sequence_->Add(f);
147  }
148 
149  void QueueVoidFunctionForCancel(void* fv) {
150  Function* f = static_cast<Function*>(fv);
151  sequence_->Add(MakeFunction(f, &Function::CallCancel));
152  }
153 
154  void QueueVoidFunctionWithNotify(void* fv, WorkerTestBase::SyncPoint* sync) {
155  Function* f = static_cast<Function*>(fv);
156  sequence_->Add(new NotifyFunction(f, sync));
157  }
158 
159  Sequence* sequence_;
160 };
161 
164 
166  : public grpc::CentralControllerRpcService::StubInterface {
167  public:
169  : sequence_(sequence) {
170  EXPECT_CALL(*this, ScheduleExpensiveOperationRaw(_)).Times(0);
171  EXPECT_CALL(*this, ScheduleRewriteRaw(_)).Times(0);
172  EXPECT_CALL(*this, AsyncScheduleExpensiveOperationRaw(_, _, _)).Times(0);
173  EXPECT_CALL(*this, AsyncScheduleRewriteRaw(_, _, _)).Times(0);
174  }
175 
176  MOCK_METHOD1(
177  ScheduleExpensiveOperationRaw,
178  ::grpc::ClientReaderWriterInterface<
179  ::net_instaweb::ScheduleExpensiveOperationRequest,
180  ::net_instaweb::
181  ScheduleExpensiveOperationResponse>*(::grpc::ClientContext*));
182 
183  MOCK_METHOD1(
184  ScheduleRewriteRaw,
185  ::grpc::ClientReaderWriterInterface<
186  ::net_instaweb::ScheduleRewriteRequest,
187  ::net_instaweb::ScheduleRewriteResponse>*(::grpc::ClientContext*));
188 
189  MOCK_METHOD3(
190  AsyncScheduleExpensiveOperationRaw,
191  ::grpc::ClientAsyncReaderWriterInterface<
192  ::net_instaweb::ScheduleExpensiveOperationRequest,
193  net_instaweb::
194  ScheduleExpensiveOperationResponse>*(::grpc::ClientContext*,
195  ::grpc::CompletionQueue*,
196  void*));
197 
198  MOCK_METHOD3(
199  AsyncScheduleRewriteRaw,
200  ::grpc::ClientAsyncReaderWriterInterface<
201  ::net_instaweb::ScheduleRewriteRequest,
202  ::net_instaweb::ScheduleRewriteResponse>*(::grpc::ClientContext*,
203  ::grpc::CompletionQueue*,
204  void*));
205 
207  ::grpc::ClientAsyncReaderWriterInterface<
208  ::net_instaweb::ScheduleExpensiveOperationRequest,
209  ::net_instaweb::ScheduleExpensiveOperationResponse>* rw) {
212  EXPECT_CALL(*this,
213  AsyncScheduleExpensiveOperationRaw(_, nullptr , _))
214  .WillOnce(DoAll(WithArgs<2>(Invoke([this](void* fv) {
215  sequence_->Add(static_cast<Function*>(fv));
216  })),
217  Return(rw)));
218  }
219 
221  ::grpc::ClientAsyncReaderWriterInterface<
222  ::net_instaweb::ScheduleExpensiveOperationRequest,
223  ::net_instaweb::ScheduleExpensiveOperationResponse>* rw) {
226  EXPECT_CALL(*this,
227  AsyncScheduleExpensiveOperationRaw(_, nullptr , _))
228  .WillOnce(DoAll(WithArgs<2>(Invoke([this](void* fv) {
229  sequence_->Add(
230  MakeFunction(static_cast<Function*>(fv),
231  &Function::CallCancel));
232  })),
233  Return(rw)));
234  }
235 
237  ::grpc::ClientAsyncReaderWriterInterface<
238  ::net_instaweb::ScheduleRewriteRequest,
239  ::net_instaweb::ScheduleRewriteResponse>* rw) {
242  EXPECT_CALL(*this,
243  AsyncScheduleRewriteRaw(_, nullptr , _))
244  .WillOnce(DoAll(WithArgs<2>(Invoke([this](void* fv) {
245  sequence_->Add(static_cast<Function*>(fv));
246  })),
247  Return(rw)));
248  }
249 
251  ::grpc::ClientAsyncReaderWriterInterface<
252  ::net_instaweb::ScheduleRewriteRequest,
253  ::net_instaweb::ScheduleRewriteResponse>* rw) {
256  EXPECT_CALL(*this,
257  AsyncScheduleRewriteRaw(_, nullptr , _))
258  .WillOnce(DoAll(WithArgs<2>(Invoke([this](void* fv) {
259  sequence_->Add(
260  MakeFunction(static_cast<Function*>(fv),
261  &Function::CallCancel));
262  })),
263  Return(rw)));
264  }
265 
266  private:
267  Sequence* sequence_;
268 };
269 
270 }
271 
272 #endif
Function * MakeFunction(C *object, void(C::*run)())
Makes a Function* that calls a 0-arg class method.
Definition: function.h:291
void ExpectAsyncScheduleRewriteFailure(::grpc::ClientAsyncReaderWriterInterface< ::net_instaweb::ScheduleRewriteRequest,::net_instaweb::ScheduleRewriteResponse > *rw)
Definition: controller_grpc_mocks.h:250
void ExpectWrite(const Matcher &matcher)
Definition: controller_grpc_mocks.h:101
Definition: sequence.h:33
void ExpectAsyncScheduleRewrite(::grpc::ClientAsyncReaderWriterInterface< ::net_instaweb::ScheduleRewriteRequest,::net_instaweb::ScheduleRewriteResponse > *rw)
Definition: controller_grpc_mocks.h:236
A way for one thread to wait for another.
Definition: worker_test_base.h:71
Definition: function.h:47
std::string GoogleString
PAGESPEED_KERNEL_BASE_STRING_H_.
Definition: string.h:24
void Run() override
Definition: controller_grpc_mocks.h:50
virtual void Add(Function *function)=0
void Cancel() override
Definition: controller_grpc_mocks.h:55
void ExpectAsyncScheduleExpensiveOperation(::grpc::ClientAsyncReaderWriterInterface< ::net_instaweb::ScheduleExpensiveOperationRequest,::net_instaweb::ScheduleExpensiveOperationResponse > *rw)
Definition: controller_grpc_mocks.h:206
Definition: controller_grpc_mocks.h:70
Definition: controller_grpc_mocks.h:45
Definition: controller_grpc_mocks.h:165
void ExpectAsyncScheduleExpensiveOperationFailure(::grpc::ClientAsyncReaderWriterInterface< ::net_instaweb::ScheduleExpensiveOperationRequest,::net_instaweb::ScheduleExpensiveOperationResponse > *rw)
Definition: controller_grpc_mocks.h:220