1 //
2 //
3 // Copyright 2019 gRPC authors.
4 //
5 // Licensed under the Apache License, Version 2.0 (the "License");
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
8 //
9 //     http://www.apache.org/licenses/LICENSE-2.0
10 //
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
16 //
17 //
18 
19 #ifndef GRPCPP_TEST_DEFAULT_REACTOR_TEST_PEER_H
20 #define GRPCPP_TEST_DEFAULT_REACTOR_TEST_PEER_H
21 
22 #include <grpcpp/server_context.h>
23 #include <grpcpp/support/server_callback.h>
24 
25 namespace grpc {
26 namespace testing {
27 
28 /// A test-only class to monitor the behavior of the ServerContext's
29 /// DefaultReactor. It is intended for allow unit-testing of a callback API
30 /// service via direct invocation of the service methods rather than through
31 /// RPCs. It is only applicable for unary RPC methods that use the
32 /// DefaultReactor rather than any user-defined reactor. If it is used, it must
33 /// be created before the RPC is invoked so that it can bind the reactor into a
34 /// test mode rather than letting it follow the normal paths.
35 class DefaultReactorTestPeer {
36  public:
DefaultReactorTestPeer(CallbackServerContext * ctx)37   explicit DefaultReactorTestPeer(CallbackServerContext* ctx)
38       : DefaultReactorTestPeer(ctx, [](Status) {}) {}
DefaultReactorTestPeer(CallbackServerContext * ctx,std::function<void (Status)> finish_func)39   DefaultReactorTestPeer(CallbackServerContext* ctx,
40                          std::function<void(Status)> finish_func)
41       : ctx_(ctx) {
42     ctx->SetupTestDefaultReactor(std::move(finish_func));
43   }
reactor()44   ServerUnaryReactor* reactor() const {
45     return reinterpret_cast<ServerUnaryReactor*>(&ctx_->default_reactor_);
46   }
test_status_set()47   bool test_status_set() const { return ctx_->test_status_set(); }
test_status()48   Status test_status() const { return ctx_->test_status(); }
49 
50  private:
51   CallbackServerContext* const ctx_;  // not owned
52 };
53 
54 }  // namespace testing
55 }  // namespace grpc
56 
57 #endif  // GRPCPP_TEST_DEFAULT_REACTOR_TEST_PEER_H
58