Jan Tattermusch | a7fff86 | 2015-02-13 11:08:08 -0800 | [diff] [blame] | 1 | #region Copyright notice and license |
| 2 | |
Jan Tattermusch | 7897ae9 | 2017-06-07 22:57:36 +0200 | [diff] [blame] | 3 | // Copyright 2015 gRPC authors. |
Craig Tiller | 190d360 | 2015-02-18 09:23:38 -0800 | [diff] [blame] | 4 | // |
Jan Tattermusch | 7897ae9 | 2017-06-07 22:57:36 +0200 | [diff] [blame] | 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 |
Craig Tiller | 190d360 | 2015-02-18 09:23:38 -0800 | [diff] [blame] | 8 | // |
Jan Tattermusch | 7897ae9 | 2017-06-07 22:57:36 +0200 | [diff] [blame] | 9 | // http://www.apache.org/licenses/LICENSE-2.0 |
Craig Tiller | 190d360 | 2015-02-18 09:23:38 -0800 | [diff] [blame] | 10 | // |
Jan Tattermusch | 7897ae9 | 2017-06-07 22:57:36 +0200 | [diff] [blame] | 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. |
Jan Tattermusch | a7fff86 | 2015-02-13 11:08:08 -0800 | [diff] [blame] | 16 | |
| 17 | #endregion |
| 18 | |
Jan Tattermusch | a7608b0 | 2015-02-03 17:54:38 -0800 | [diff] [blame] | 19 | using System; |
Jan Tattermusch | 6720642 | 2017-05-26 14:28:39 -0700 | [diff] [blame] | 20 | using System.IO; |
Jan Tattermusch | 31ba063 | 2015-08-04 22:02:55 -0700 | [diff] [blame] | 21 | using System.Linq; |
Jan Tattermusch | 3086862 | 2015-02-19 09:22:33 -0800 | [diff] [blame] | 22 | using Grpc.Core; |
| 23 | using Grpc.Core.Internal; |
| 24 | using Grpc.Core.Utils; |
Jan Tattermusch | a7608b0 | 2015-02-03 17:54:38 -0800 | [diff] [blame] | 25 | using NUnit.Framework; |
Jan Tattermusch | a7608b0 | 2015-02-03 17:54:38 -0800 | [diff] [blame] | 26 | |
Jan Tattermusch | 3086862 | 2015-02-19 09:22:33 -0800 | [diff] [blame] | 27 | namespace Grpc.Core.Tests |
Jan Tattermusch | a7608b0 | 2015-02-03 17:54:38 -0800 | [diff] [blame] | 28 | { |
| 29 | public class ServerTest |
| 30 | { |
| 31 | [Test] |
Jan Tattermusch | 23821ce | 2015-02-13 10:46:02 -0800 | [diff] [blame] | 32 | public void StartAndShutdownServer() |
| 33 | { |
Jan Tattermusch | 31ba063 | 2015-08-04 22:02:55 -0700 | [diff] [blame] | 34 | Server server = new Server |
| 35 | { |
| 36 | Ports = { new ServerPort("localhost", ServerPort.PickUnused, ServerCredentials.Insecure) } |
| 37 | }; |
Jan Tattermusch | a7608b0 | 2015-02-03 17:54:38 -0800 | [diff] [blame] | 38 | server.Start(); |
Jan Tattermusch | 8ce5e8b | 2015-02-05 10:56:49 -0800 | [diff] [blame] | 39 | server.ShutdownAsync().Wait(); |
Jan Tattermusch | a7608b0 | 2015-02-03 17:54:38 -0800 | [diff] [blame] | 40 | } |
Jan Tattermusch | 31ba063 | 2015-08-04 22:02:55 -0700 | [diff] [blame] | 41 | |
| 42 | [Test] |
Jan Tattermusch | 9bbb1f9 | 2015-12-14 12:37:05 -0800 | [diff] [blame] | 43 | 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 Tattermusch | 31ba063 | 2015-08-04 22:02:55 -0700 | [diff] [blame] | 54 | 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 Tattermusch | 2b35795 | 2015-08-20 14:54:33 -0700 | [diff] [blame] | 66 | server.ShutdownAsync().Wait(); |
Jan Tattermusch | 31ba063 | 2015-08-04 22:02:55 -0700 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | [Test] |
Jan Tattermusch | 6720642 | 2017-05-26 14:28:39 -0700 | [diff] [blame] | 70 | 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 Tattermusch | 31ba063 | 2015-08-04 22:02:55 -0700 | [diff] [blame] | 85 | 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 Tattermusch | 781720f | 2016-06-03 17:40:28 -0700 | [diff] [blame] | 93 | Assert.Throws(typeof(InvalidOperationException), () => server.Services.Add(ServerServiceDefinition.CreateBuilder().Build())); |
Jan Tattermusch | 31ba063 | 2015-08-04 22:02:55 -0700 | [diff] [blame] | 94 | |
| 95 | server.ShutdownAsync().Wait(); |
Jan Tattermusch | 31ba063 | 2015-08-04 22:02:55 -0700 | [diff] [blame] | 96 | } |
Jan Tattermusch | 95e547e | 2016-06-07 17:14:42 -0700 | [diff] [blame] | 97 | |
| 98 | [Test] |
Jan Tattermusch | 8e93533 | 2016-06-08 19:45:54 -0700 | [diff] [blame] | 99 | 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 Tattermusch | 95e547e | 2016-06-07 17:14:42 -0700 | [diff] [blame] | 107 | 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 Tattermusch | a7608b0 | 2015-02-03 17:54:38 -0800 | [diff] [blame] | 112 | } |
| 113 | } |