• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
30 namespace grpc {
31 namespace {
32 
33 testing::EchoTestService::Service g_service;
34 
MakePort()35 grpc::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 
GetPort()42 const grpc::string& GetPort() {
43   static grpc::string g_port = MakePort();
44   return g_port;
45 }
46 
TEST(ServerBuilderTest,NoOp)47 TEST(ServerBuilderTest, NoOp) { ServerBuilder b; }
48 
TEST(ServerBuilderTest,CreateServerNoPorts)49 TEST(ServerBuilderTest, CreateServerNoPorts) {
50   ServerBuilder().RegisterService(&g_service).BuildAndStart()->Shutdown();
51 }
52 
TEST(ServerBuilderTest,CreateServerOnePort)53 TEST(ServerBuilderTest, CreateServerOnePort) {
54   ServerBuilder()
55       .RegisterService(&g_service)
56       .AddListeningPort(GetPort(), InsecureServerCredentials())
57       .BuildAndStart()
58       ->Shutdown();
59 }
60 
TEST(ServerBuilderTest,CreateServerRepeatedPort)61 TEST(ServerBuilderTest, CreateServerRepeatedPort) {
62   ServerBuilder()
63       .RegisterService(&g_service)
64       .AddListeningPort(GetPort(), InsecureServerCredentials())
65       .AddListeningPort(GetPort(), InsecureServerCredentials())
66       .BuildAndStart()
67       ->Shutdown();
68 }
69 
TEST(ServerBuilderTest,CreateServerRepeatedPortWithDisallowedReusePort)70 TEST(ServerBuilderTest, CreateServerRepeatedPortWithDisallowedReusePort) {
71   EXPECT_EQ(ServerBuilder()
72                 .RegisterService(&g_service)
73                 .AddListeningPort(GetPort(), InsecureServerCredentials())
74                 .AddListeningPort(GetPort(), InsecureServerCredentials())
75                 .AddChannelArgument(GRPC_ARG_ALLOW_REUSEPORT, 0)
76                 .BuildAndStart(),
77             nullptr);
78 }
79 
80 }  // namespace
81 }  // namespace grpc
82 
main(int argc,char ** argv)83 int main(int argc, char** argv) {
84   ::testing::InitGoogleTest(&argc, argv);
85   grpc_init();
86   int ret = RUN_ALL_TESTS();
87   grpc_shutdown();
88   return ret;
89 }
90