ServerTest.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. #region Copyright notice and license
  2. // Copyright 2015 gRPC authors.
  3. //
  4. // Licensed under the Apache License, Version 2.0 (the "License");
  5. // you may not use this file except in compliance with the License.
  6. // You may obtain a copy of the License at
  7. //
  8. // http://www.apache.org/licenses/LICENSE-2.0
  9. //
  10. // Unless required by applicable law or agreed to in writing, software
  11. // distributed under the License is distributed on an "AS IS" BASIS,
  12. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. // See the License for the specific language governing permissions and
  14. // limitations under the License.
  15. #endregion
  16. using System;
  17. using System.IO;
  18. using System.Linq;
  19. using Grpc.Core;
  20. using Grpc.Core.Internal;
  21. using Grpc.Core.Utils;
  22. using NUnit.Framework;
  23. namespace Grpc.Core.Tests
  24. {
  25. public class ServerTest
  26. {
  27. [Test]
  28. public void StartAndShutdownServer()
  29. {
  30. Server server = new Server
  31. {
  32. Ports = { new ServerPort("localhost", ServerPort.PickUnused, ServerCredentials.Insecure) }
  33. };
  34. server.Start();
  35. server.ShutdownAsync().Wait();
  36. }
  37. [Test]
  38. public void StartAndKillServer()
  39. {
  40. Server server = new Server
  41. {
  42. Ports = { new ServerPort("localhost", ServerPort.PickUnused, ServerCredentials.Insecure) }
  43. };
  44. server.Start();
  45. server.KillAsync().Wait();
  46. }
  47. [Test]
  48. public void PickUnusedPort()
  49. {
  50. Server server = new Server
  51. {
  52. Ports = { new ServerPort("localhost", ServerPort.PickUnused, ServerCredentials.Insecure) }
  53. };
  54. var boundPort = server.Ports.Single();
  55. Assert.AreEqual(0, boundPort.Port);
  56. Assert.Greater(boundPort.BoundPort, 0);
  57. server.Start();
  58. server.ShutdownAsync().Wait();
  59. }
  60. [Test]
  61. public void StartThrowsWithUnboundPorts()
  62. {
  63. int twiceBoundPort = 9999;
  64. Server server = new Server(new[] { new ChannelOption(ChannelOptions.SoReuseport, 0) })
  65. {
  66. Ports = {
  67. new ServerPort("localhost", twiceBoundPort, ServerCredentials.Insecure),
  68. new ServerPort("localhost", twiceBoundPort, ServerCredentials.Insecure)
  69. }
  70. };
  71. Assert.Throws(typeof(IOException), () => server.Start());
  72. server.ShutdownAsync().Wait();
  73. }
  74. [Test]
  75. public void CannotModifyAfterStarted()
  76. {
  77. Server server = new Server
  78. {
  79. Ports = { new ServerPort("localhost", ServerPort.PickUnused, ServerCredentials.Insecure) }
  80. };
  81. server.Start();
  82. Assert.Throws(typeof(InvalidOperationException), () => server.Ports.Add("localhost", 9999, ServerCredentials.Insecure));
  83. Assert.Throws(typeof(InvalidOperationException), () => server.Services.Add(ServerServiceDefinition.CreateBuilder().Build()));
  84. server.ShutdownAsync().Wait();
  85. }
  86. [Test]
  87. public void UnstartedServerCanBeShutdown()
  88. {
  89. var server = new Server();
  90. server.ShutdownAsync().Wait();
  91. Assert.Throws(typeof(InvalidOperationException), () => server.Start());
  92. }
  93. [Test]
  94. public void UnstartedServerDoesNotPreventShutdown()
  95. {
  96. // just create a server, don't start it, and make sure it doesn't prevent shutdown.
  97. var server = new Server();
  98. }
  99. }
  100. }