blob: 884414792d3ad06c769001cd28f69478ef2559cc [file] [log] [blame]
Jan Tattermuscha7fff862015-02-13 11:08:08 -08001#region Copyright notice and license
2
Jan Tattermusch7897ae92017-06-07 22:57:36 +02003// Copyright 2015 gRPC authors.
Craig Tiller190d3602015-02-18 09:23:38 -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 Tiller190d3602015-02-18 09:23:38 -08008//
Jan Tattermusch7897ae92017-06-07 22:57:36 +02009// http://www.apache.org/licenses/LICENSE-2.0
Craig Tiller190d3602015-02-18 09:23:38 -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.
Jan Tattermuscha7fff862015-02-13 11:08:08 -080016
17#endregion
18
Jan Tattermuscha7608b02015-02-03 17:54:38 -080019using System;
Jan Tattermusch67206422017-05-26 14:28:39 -070020using System.IO;
Jan Tattermusch31ba0632015-08-04 22:02:55 -070021using System.Linq;
Jan Tattermusch30868622015-02-19 09:22:33 -080022using Grpc.Core;
23using Grpc.Core.Internal;
24using Grpc.Core.Utils;
Jan Tattermuscha7608b02015-02-03 17:54:38 -080025using NUnit.Framework;
Jan Tattermuscha7608b02015-02-03 17:54:38 -080026
Jan Tattermusch30868622015-02-19 09:22:33 -080027namespace Grpc.Core.Tests
Jan Tattermuscha7608b02015-02-03 17:54:38 -080028{
29 public class ServerTest
30 {
31 [Test]
Jan Tattermusch23821ce2015-02-13 10:46:02 -080032 public void StartAndShutdownServer()
33 {
Jan Tattermusch31ba0632015-08-04 22:02:55 -070034 Server server = new Server
35 {
36 Ports = { new ServerPort("localhost", ServerPort.PickUnused, ServerCredentials.Insecure) }
37 };
Jan Tattermuscha7608b02015-02-03 17:54:38 -080038 server.Start();
Jan Tattermusch8ce5e8b2015-02-05 10:56:49 -080039 server.ShutdownAsync().Wait();
Jan Tattermuscha7608b02015-02-03 17:54:38 -080040 }
Jan Tattermusch31ba0632015-08-04 22:02:55 -070041
42 [Test]
Jan Tattermusch9bbb1f92015-12-14 12:37:05 -080043 public void StartAndKillServer()
44 {
45 Server server = new Server
46 {
47 Ports = { new ServerPort("localhost", ServerPort.PickUnused, ServerCredentials.Insecure) }
48 };
49 server.Start();
50 server.KillAsync().Wait();
51 }
52
53 [Test]
Jan Tattermusch31ba0632015-08-04 22:02:55 -070054 public void PickUnusedPort()
55 {
56 Server server = new Server
57 {
58 Ports = { new ServerPort("localhost", ServerPort.PickUnused, ServerCredentials.Insecure) }
59 };
60
61 var boundPort = server.Ports.Single();
62 Assert.AreEqual(0, boundPort.Port);
63 Assert.Greater(boundPort.BoundPort, 0);
64
65 server.Start();
Jan Tattermusch2b357952015-08-20 14:54:33 -070066 server.ShutdownAsync().Wait();
Jan Tattermusch31ba0632015-08-04 22:02:55 -070067 }
68
69 [Test]
Jan Tattermusch67206422017-05-26 14:28:39 -070070 public void StartThrowsWithUnboundPorts()
71 {
72 int twiceBoundPort = 9999;
73 Server server = new Server(new[] { new ChannelOption(ChannelOptions.SoReuseport, 0) })
74 {
75 Ports = {
76 new ServerPort("localhost", twiceBoundPort, ServerCredentials.Insecure),
77 new ServerPort("localhost", twiceBoundPort, ServerCredentials.Insecure)
78 }
79 };
80 Assert.Throws(typeof(IOException), () => server.Start());
81 server.ShutdownAsync().Wait();
82 }
83
84 [Test]
Jan Tattermusch31ba0632015-08-04 22:02:55 -070085 public void CannotModifyAfterStarted()
86 {
87 Server server = new Server
88 {
89 Ports = { new ServerPort("localhost", ServerPort.PickUnused, ServerCredentials.Insecure) }
90 };
91 server.Start();
92 Assert.Throws(typeof(InvalidOperationException), () => server.Ports.Add("localhost", 9999, ServerCredentials.Insecure));
Jan Tattermusch781720f2016-06-03 17:40:28 -070093 Assert.Throws(typeof(InvalidOperationException), () => server.Services.Add(ServerServiceDefinition.CreateBuilder().Build()));
Jan Tattermusch31ba0632015-08-04 22:02:55 -070094
95 server.ShutdownAsync().Wait();
Jan Tattermusch31ba0632015-08-04 22:02:55 -070096 }
Jan Tattermusch95e547e2016-06-07 17:14:42 -070097
98 [Test]
Jan Tattermusch8e935332016-06-08 19:45:54 -070099 public void UnstartedServerCanBeShutdown()
100 {
101 var server = new Server();
102 server.ShutdownAsync().Wait();
103 Assert.Throws(typeof(InvalidOperationException), () => server.Start());
104 }
105
106 [Test]
Jan Tattermusch95e547e2016-06-07 17:14:42 -0700107 public void UnstartedServerDoesNotPreventShutdown()
108 {
109 // just create a server, don't start it, and make sure it doesn't prevent shutdown.
110 var server = new Server();
111 }
Jan Tattermuscha7608b02015-02-03 17:54:38 -0800112 }
113}