blob: 067a184377ee33aaed46d786868b27e596cdb93d [file] [log] [blame]
Craig Tiller12352b22017-01-10 15:16:14 -08001/*
2 *
Jan Tattermusch7897ae92017-06-07 22:57:36 +02003 * Copyright 2017 gRPC authors.
Craig Tiller12352b22017-01-10 15:16:14 -08004 *
Jan Tattermusch7897ae92017-06-07 22:57:36 +02005 * 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
Craig Tiller12352b22017-01-10 15:16:14 -08008 *
Jan Tattermusch7897ae92017-06-07 22:57:36 +02009 * http://www.apache.org/licenses/LICENSE-2.0
Craig Tiller12352b22017-01-10 15:16:14 -080010 *
Jan Tattermusch7897ae92017-06-07 22:57:36 +020011 * 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.
Craig Tiller12352b22017-01-10 15:16:14 -080016 *
17 */
18
Vijay Paic90a8562018-03-08 21:20:24 -080019#include <grpcpp/impl/codegen/config.h>
Craig Tiller12352b22017-01-10 15:16:14 -080020#include <gtest/gtest.h>
21
Vijay Paic90a8562018-03-08 21:20:24 -080022#include <grpcpp/server.h>
23#include <grpcpp/server_builder.h>
Craig Tiller12352b22017-01-10 15:16:14 -080024
Yash Tibrewal8cf14702017-12-06 09:47:54 -080025#include <grpc/grpc.h>
26
Craig Tiller12352b22017-01-10 15:16:14 -080027#include "src/proto/grpc/testing/echo.grpc.pb.h"
28#include "test/core/util/port.h"
29
30namespace grpc {
31namespace {
32
33testing::EchoTestService::Service g_service;
34
35grpc::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
Ara Ayvazyanbd5dae22018-03-30 10:59:59 -070042const grpc::string& GetPort() {
43 static grpc::string g_port = MakePort();
44 return g_port;
45}
Craig Tiller12352b22017-01-10 15:16:14 -080046
47TEST(ServerBuilderTest, NoOp) { ServerBuilder b; }
48
49TEST(ServerBuilderTest, CreateServerNoPorts) {
50 ServerBuilder().RegisterService(&g_service).BuildAndStart()->Shutdown();
51}
52
53TEST(ServerBuilderTest, CreateServerOnePort) {
54 ServerBuilder()
55 .RegisterService(&g_service)
Ara Ayvazyanbd5dae22018-03-30 10:59:59 -070056 .AddListeningPort(GetPort(), InsecureServerCredentials())
Craig Tiller12352b22017-01-10 15:16:14 -080057 .BuildAndStart()
58 ->Shutdown();
59}
60
61TEST(ServerBuilderTest, CreateServerRepeatedPort) {
62 ServerBuilder()
63 .RegisterService(&g_service)
Ara Ayvazyanbd5dae22018-03-30 10:59:59 -070064 .AddListeningPort(GetPort(), InsecureServerCredentials())
65 .AddListeningPort(GetPort(), InsecureServerCredentials())
Craig Tiller12352b22017-01-10 15:16:14 -080066 .BuildAndStart()
67 ->Shutdown();
68}
69
70TEST(ServerBuilderTest, CreateServerRepeatedPortWithDisallowedReusePort) {
71 EXPECT_EQ(ServerBuilder()
72 .RegisterService(&g_service)
Ara Ayvazyanbd5dae22018-03-30 10:59:59 -070073 .AddListeningPort(GetPort(), InsecureServerCredentials())
74 .AddListeningPort(GetPort(), InsecureServerCredentials())
Craig Tiller12352b22017-01-10 15:16:14 -080075 .AddChannelArgument(GRPC_ARG_ALLOW_REUSEPORT, 0)
76 .BuildAndStart(),
77 nullptr);
78}
79
80} // namespace
81} // namespace grpc
82
83int main(int argc, char** argv) {
84 ::testing::InitGoogleTest(&argc, argv);
Yash Tibrewal8cf14702017-12-06 09:47:54 -080085 grpc_init();
86 int ret = RUN_ALL_TESTS();
87 grpc_shutdown();
88 return ret;
Craig Tiller12352b22017-01-10 15:16:14 -080089}