default_reactor_test_peer.h 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  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. #ifndef GRPCPP_TEST_DEFAULT_REACTOR_TEST_PEER_H
  19. #define GRPCPP_TEST_DEFAULT_REACTOR_TEST_PEER_H
  20. #include <grpcpp/server_context.h>
  21. #include <grpcpp/support/server_callback.h>
  22. namespace grpc {
  23. namespace testing {
  24. /// A test-only class to monitor the behavior of the ServerContext's
  25. /// DefaultReactor. It is intended for allow unit-testing of a callback API
  26. /// service via direct invocation of the service methods rather than through
  27. /// RPCs. It is only applicable for unary RPC methods that use the
  28. /// DefaultReactor rather than any user-defined reactor. If it is used, it must
  29. /// be created before the RPC is invoked so that it can bind the reactor into a
  30. /// test mode rather than letting it follow the normal paths.
  31. class DefaultReactorTestPeer {
  32. public:
  33. explicit DefaultReactorTestPeer(CallbackServerContext* ctx)
  34. : DefaultReactorTestPeer(ctx, [](Status) {}) {}
  35. DefaultReactorTestPeer(CallbackServerContext* ctx,
  36. std::function<void(Status)> finish_func)
  37. : ctx_(ctx) {
  38. ctx->SetupTestDefaultReactor(std::move(finish_func));
  39. }
  40. ServerUnaryReactor* reactor() const {
  41. return reinterpret_cast<ServerUnaryReactor*>(&ctx_->default_reactor_);
  42. }
  43. bool test_status_set() const { return ctx_->test_status_set(); }
  44. Status test_status() const { return ctx_->test_status(); }
  45. private:
  46. CallbackServerContext* const ctx_; // not owned
  47. };
  48. } // namespace testing
  49. } // namespace grpc
  50. #endif // GRPCPP_TEST_DEFAULT_REACTOR_TEST_PEER_H