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(experimental::CallbackServerContext * ctx)37 explicit DefaultReactorTestPeer(experimental::CallbackServerContext* ctx) 38 : DefaultReactorTestPeer(ctx, [](::grpc::Status) {}) {} DefaultReactorTestPeer(experimental::CallbackServerContext * ctx,std::function<void (::grpc::Status)> finish_func)39 DefaultReactorTestPeer(experimental::CallbackServerContext* ctx, 40 std::function<void(::grpc::Status)> finish_func) 41 : ctx_(ctx) { 42 ctx->SetupTestDefaultReactor(std::move(finish_func)); 43 } reactor()44 ::grpc::experimental::ServerUnaryReactor* reactor() const { 45 return reinterpret_cast<experimental::ServerUnaryReactor*>( 46 &ctx_->default_reactor_); 47 } test_status_set()48 bool test_status_set() const { return ctx_->test_status_set(); } test_status()49 Status test_status() const { return ctx_->test_status(); } 50 51 private: 52 experimental::CallbackServerContext* const ctx_; // not owned 53 }; 54 55 } // namespace testing 56 } // namespace grpc 57 58 #endif // GRPCPP_TEST_DEFAULT_REACTOR_TEST_PEER_H 59