server_builder_test.cc 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. /*
  2. *
  3. * Copyright 2017 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. #include <gtest/gtest.h>
  19. #include <grpc/grpc.h>
  20. #include <grpcpp/impl/codegen/config.h>
  21. #include <grpcpp/server.h>
  22. #include <grpcpp/server_builder.h>
  23. #include "src/proto/grpc/testing/echo.grpc.pb.h"
  24. #include "test/core/util/port.h"
  25. #include "test/core/util/test_config.h"
  26. namespace grpc {
  27. namespace {
  28. testing::EchoTestService::Service g_service;
  29. std::string MakePort() {
  30. std::ostringstream s;
  31. int p = grpc_pick_unused_port_or_die();
  32. s << "localhost:" << p;
  33. return s.str();
  34. }
  35. const std::string& GetPort() {
  36. static std::string g_port = MakePort();
  37. return g_port;
  38. }
  39. class ServerBuilderTest : public ::testing::Test {
  40. protected:
  41. static void SetUpTestCase() { grpc_init(); }
  42. static void TearDownTestCase() { grpc_shutdown(); }
  43. };
  44. TEST_F(ServerBuilderTest, NoOp) { ServerBuilder b; }
  45. TEST_F(ServerBuilderTest, CreateServerNoPorts) {
  46. ServerBuilder().RegisterService(&g_service).BuildAndStart()->Shutdown();
  47. }
  48. TEST_F(ServerBuilderTest, CreateServerOnePort) {
  49. ServerBuilder()
  50. .RegisterService(&g_service)
  51. .AddListeningPort(GetPort(), InsecureServerCredentials())
  52. .BuildAndStart()
  53. ->Shutdown();
  54. }
  55. TEST_F(ServerBuilderTest, CreateServerRepeatedPort) {
  56. ServerBuilder()
  57. .RegisterService(&g_service)
  58. .AddListeningPort(GetPort(), InsecureServerCredentials())
  59. .AddListeningPort(GetPort(), InsecureServerCredentials())
  60. .BuildAndStart()
  61. ->Shutdown();
  62. }
  63. TEST_F(ServerBuilderTest, CreateServerRepeatedPortWithDisallowedReusePort) {
  64. EXPECT_EQ(ServerBuilder()
  65. .RegisterService(&g_service)
  66. .AddListeningPort(GetPort(), InsecureServerCredentials())
  67. .AddListeningPort(GetPort(), InsecureServerCredentials())
  68. .AddChannelArgument(GRPC_ARG_ALLOW_REUSEPORT, 0)
  69. .BuildAndStart(),
  70. nullptr);
  71. }
  72. } // namespace
  73. } // namespace grpc
  74. int main(int argc, char** argv) {
  75. grpc::testing::TestEnvironment env(argc, argv);
  76. ::testing::InitGoogleTest(&argc, argv);
  77. int ret = RUN_ALL_TESTS();
  78. return ret;
  79. }