server_builder_test.cc
Go to the documentation of this file.
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 
19 #include <gtest/gtest.h>
20 
21 #include <grpc/grpc.h>
23 #include <grpcpp/server.h>
24 #include <grpcpp/server_builder.h>
25 
26 #include "src/proto/grpc/testing/echo.grpc.pb.h"
27 #include "test/core/util/port.h"
29 
30 namespace grpc {
31 namespace {
32 
33 testing::EchoTestService::Service g_service;
34 
35 std::string MakePort() {
36  std::ostringstream s;
38  s << "localhost:" << p;
39  return s.str();
40 }
41 
42 const std::string& GetPort() {
43  static std::string g_port = MakePort();
44  return g_port;
45 }
46 
47 class ServerBuilderTest : public ::testing::Test {
48  protected:
49  static void SetUpTestCase() { grpc_init(); }
50 
51  static void TearDownTestCase() { grpc_shutdown(); }
52 };
53 TEST_F(ServerBuilderTest, NoOp) { ServerBuilder b; }
54 
55 TEST_F(ServerBuilderTest, CreateServerNoPorts) {
56  ServerBuilder().RegisterService(&g_service).BuildAndStart()->Shutdown();
57 }
58 
59 TEST_F(ServerBuilderTest, CreateServerOnePort) {
60  ServerBuilder()
61  .RegisterService(&g_service)
62  .AddListeningPort(GetPort(), InsecureServerCredentials())
63  .BuildAndStart()
64  ->Shutdown();
65 }
66 
67 TEST_F(ServerBuilderTest, CreateServerRepeatedPort) {
68  ServerBuilder()
69  .RegisterService(&g_service)
70  .AddListeningPort(GetPort(), InsecureServerCredentials())
71  .AddListeningPort(GetPort(), InsecureServerCredentials())
72  .BuildAndStart()
73  ->Shutdown();
74 }
75 
76 TEST_F(ServerBuilderTest, CreateServerRepeatedPortWithDisallowedReusePort) {
77  EXPECT_EQ(ServerBuilder()
78  .RegisterService(&g_service)
79  .AddListeningPort(GetPort(), InsecureServerCredentials())
80  .AddListeningPort(GetPort(), InsecureServerCredentials())
81  .AddChannelArgument(GRPC_ARG_ALLOW_REUSEPORT, 0)
82  .BuildAndStart(),
83  nullptr);
84 }
85 
86 } // namespace
87 } // namespace grpc
88 
89 int main(int argc, char** argv) {
91  ::testing::InitGoogleTest(&argc, argv);
92  int ret = RUN_ALL_TESTS();
93  return ret;
94 }
port.h
generate.env
env
Definition: generate.py:37
grpc
Definition: grpcpp/alarm.h:33
NoOp
Definition: bm_call_create.cc:477
testing::internal::string
::std::string string
Definition: bloaty/third_party/protobuf/third_party/googletest/googletest/include/gtest/internal/gtest-port.h:881
absl::FormatConversionChar::s
@ s
xds_manager.p
p
Definition: xds_manager.py:60
GRPC_ARG_ALLOW_REUSEPORT
#define GRPC_ARG_ALLOW_REUSEPORT
Definition: grpc_types.h:295
config.h
testing::Test
Definition: bloaty/third_party/googletest/googletest/include/gtest/gtest.h:402
grpc.h
RUN_ALL_TESTS
int RUN_ALL_TESTS() GTEST_MUST_USE_RESULT_
Definition: bloaty/third_party/googletest/googletest/include/gtest/gtest.h:2471
grpc_pick_unused_port_or_die
int grpc_pick_unused_port_or_die(void)
b
uint64_t b
Definition: abseil-cpp/absl/container/internal/layout_test.cc:53
test_config.h
testing::InitGoogleTest
GTEST_API_ void InitGoogleTest(int *argc, char **argv)
Definition: bloaty/third_party/googletest/googletest/src/gtest.cc:6106
main
int main(int argc, char **argv)
Definition: server_builder_test.cc:89
ret
UniquePtr< SSL_SESSION > ret
Definition: ssl_x509.cc:1029
grpc::testing::TestEnvironment
Definition: test/core/util/test_config.h:54
grpc::InsecureServerCredentials
std::shared_ptr< ServerCredentials > InsecureServerCredentials()
Definition: insecure_server_credentials.cc:52
grpc::EXPECT_EQ
EXPECT_EQ(grpc::StatusCode::INVALID_ARGUMENT, status.error_code())
server.h
grpc_init
GRPCAPI void grpc_init(void)
Definition: init.cc:146
grpc_shutdown
GRPCAPI void grpc_shutdown(void)
Definition: init.cc:209
server_builder.h
TEST_F
#define TEST_F(test_fixture, test_name)
Definition: bloaty/third_party/googletest/googletest/include/gtest/gtest.h:2367


grpc
Author(s):
autogenerated on Thu Mar 13 2025 03:01:17