1 #region Copyright notice and license 2 3 // Copyright 2015 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 #endregion 18 19 using System; 20 using System.IO; 21 using System.Linq; 22 using Grpc.Core; 23 using Grpc.Core.Internal; 24 using Grpc.Core.Utils; 25 using NUnit.Framework; 26 27 namespace Grpc.Core.Tests 28 { 29 public class ServerTest 30 { 31 [Test] StartAndShutdownServer()32 public void StartAndShutdownServer() 33 { 34 Server server = new Server 35 { 36 Ports = { new ServerPort("localhost", ServerPort.PickUnused, ServerCredentials.Insecure) } 37 }; 38 server.Start(); 39 server.ShutdownAsync().Wait(); 40 } 41 42 [Test] StartAndKillServer()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] PickUnusedPort()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(); 66 server.ShutdownAsync().Wait(); 67 } 68 69 [Test] StartThrowsWithUnboundPorts()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] CannotModifyAfterStarted()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)); 93 Assert.Throws(typeof(InvalidOperationException), () => server.Services.Add(ServerServiceDefinition.CreateBuilder().Build())); 94 95 server.ShutdownAsync().Wait(); 96 } 97 98 [Test] UnstartedServerCanBeShutdown()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] UnstartedServerDoesNotPreventShutdown()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 } 112 } 113 } 114