Debian Codegraph @

test/cpp/server/server_builder_test.cc

raw
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>
22 #include <grpcpp/server.h>
23 #include <grpcpp/server_builder.h>
24 #include <grpcpp/support/config.h>
25
26 #include "src/proto/grpc/testing/echo.grpc.pb.h"
27 #include "test/core/util/port.h"
28 #include "test/core/util/test_config.h"
29
30 namespace grpc {
31 namespace {
32
33 testing::EchoTestService::Service g_service;
34
35 std::string MakePort() {
36 std::ostringstream s;
37 int p = grpc_pick_unused_port_or_die();
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) {
90 grpc::testing::TestEnvironment env(&argc, argv);
91 ::testing::InitGoogleTest(&argc, argv);
92 int ret = RUN_ALL_TESTS();
93 return ret;
94 }