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