throw if server started with unbound ports

pull/11353/head
Jan Tattermusch 8 years ago
parent 5cae37ab5e
commit 9d56717be4
  1. 19
      src/csharp/Grpc.Core/Server.cs

@ -34,6 +34,7 @@
using System; using System;
using System.Collections; using System.Collections;
using System.Collections.Generic; using System.Collections.Generic;
using System.IO;
using System.Linq; using System.Linq;
using System.Threading.Tasks; using System.Threading.Tasks;
using Grpc.Core.Internal; using Grpc.Core.Internal;
@ -155,6 +156,7 @@ namespace Grpc.Core
/// <summary> /// <summary>
/// Starts the server. /// Starts the server.
/// Throws <c>IOException</c> if not successful.
/// </summary> /// </summary>
public void Start() public void Start()
{ {
@ -163,7 +165,8 @@ namespace Grpc.Core
GrpcPreconditions.CheckState(!startRequested); GrpcPreconditions.CheckState(!startRequested);
GrpcPreconditions.CheckState(!shutdownRequested); GrpcPreconditions.CheckState(!shutdownRequested);
startRequested = true; startRequested = true;
CheckPortsBoundSuccessfully();
handle.Start(); handle.Start();
for (int i = 0; i < requestCallTokensPerCq; i++) for (int i = 0; i < requestCallTokensPerCq; i++)
@ -316,6 +319,20 @@ namespace Grpc.Core
} }
} }
/// <summary>
/// Checks that all ports have been bound successfully.
/// </summary>
private void CheckPortsBoundSuccessfully()
{
lock (myLock)
{
if (!ports.All((port) => port.BoundPort != 0))
{
throw new IOException("Failed to bind some of ports exposed by the server.");
}
}
}
private void DisposeHandle() private void DisposeHandle()
{ {
var activeCallCount = activeCallCounter.Count; var activeCallCount = activeCallCounter.Count;

Loading…
Cancel
Save