server_helper.h 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. /*
  2. *
  3. * Copyright 2015 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 GRPC_TEST_CPP_INTEROP_SERVER_HELPER_H
  19. #define GRPC_TEST_CPP_INTEROP_SERVER_HELPER_H
  20. #include <condition_variable>
  21. #include <memory>
  22. #include <grpc/compression.h>
  23. #include <grpc/impl/codegen/atm.h>
  24. #include <grpcpp/security/server_credentials.h>
  25. #include <grpcpp/server.h>
  26. #include <grpcpp/server_builder.h>
  27. #include <grpcpp/server_context.h>
  28. namespace grpc {
  29. namespace testing {
  30. std::shared_ptr<ServerCredentials> CreateInteropServerCredentials();
  31. class InteropServerContextInspector {
  32. public:
  33. explicit InteropServerContextInspector(const grpc::ServerContext& context);
  34. // Inspector methods, able to peek inside ServerContext, follow.
  35. std::shared_ptr<const AuthContext> GetAuthContext() const;
  36. bool IsCancelled() const;
  37. grpc_compression_algorithm GetCallCompressionAlgorithm() const;
  38. uint32_t GetEncodingsAcceptedByClient() const;
  39. bool WasCompressed() const;
  40. private:
  41. const grpc::ServerContext& context_;
  42. };
  43. namespace interop {
  44. extern gpr_atm g_got_sigint;
  45. struct ServerStartedCondition {
  46. std::mutex mutex;
  47. std::condition_variable condition;
  48. bool server_started = false;
  49. };
  50. /// Run gRPC interop server using port FLAGS_port.
  51. ///
  52. /// \param creds The credentials associated with the server.
  53. void RunServer(const std::shared_ptr<ServerCredentials>& creds);
  54. /// Run gRPC interop server.
  55. ///
  56. /// \param creds The credentials associated with the server.
  57. /// \param port Port to use for the server.
  58. /// \param server_started_condition (optional) Struct holding mutex, condition
  59. /// variable, and condition used to notify when the server has started.
  60. void RunServer(const std::shared_ptr<ServerCredentials>& creds, int port,
  61. ServerStartedCondition* server_started_condition);
  62. /// Run gRPC interop server.
  63. ///
  64. /// \param creds The credentials associated with the server.
  65. /// \param server_options List of options to set when building the server.
  66. void RunServer(
  67. const std::shared_ptr<ServerCredentials>& creds,
  68. std::unique_ptr<std::vector<std::unique_ptr<ServerBuilderOption>>>
  69. server_options);
  70. /// Run gRPC interop server.
  71. ///
  72. /// \param creds The credentials associated with the server.
  73. /// \param port Port to use for the server.
  74. /// \param server_options List of options to set when building the server.
  75. /// \param server_started_condition (optional) Struct holding mutex, condition
  76. // variable, and condition used to notify when the server has started.
  77. void RunServer(
  78. const std::shared_ptr<ServerCredentials>& creds, const int port,
  79. ServerStartedCondition* server_started_condition,
  80. std::unique_ptr<std::vector<std::unique_ptr<grpc::ServerBuilderOption>>>
  81. server_options);
  82. } // namespace interop
  83. } // namespace testing
  84. } // namespace grpc
  85. #endif // GRPC_TEST_CPP_INTEROP_SERVER_HELPER_H