HttpListener : Stop fix

This commit is contained in:
Stef Heyenrath
2017-03-21 14:45:34 +01:00
parent 9fcfb3109f
commit e9ee4e91f9
6 changed files with 41 additions and 14 deletions

View File

@@ -13,11 +13,11 @@ namespace WireMock.Http
/// </summary> /// </summary>
public class TinyHttpServer public class TinyHttpServer
{ {
private readonly Action<HttpListenerContext> _httpHandler; private readonly Action<HttpListenerContext, CancellationToken> _httpHandler;
private readonly HttpListener _listener; private readonly HttpListener _listener;
private CancellationTokenSource _cts; private readonly CancellationTokenSource _cts;
/// <summary> /// <summary>
/// Gets a value indicating whether this server is started. /// Gets a value indicating whether this server is started.
@@ -33,6 +33,7 @@ namespace WireMock.Http
/// <value> /// <value>
/// The urls. /// The urls.
/// </value> /// </value>
[PublicAPI]
public List<Uri> Urls { get; } = new List<Uri>(); public List<Uri> Urls { get; } = new List<Uri>();
/// <summary> /// <summary>
@@ -41,6 +42,7 @@ namespace WireMock.Http
/// <value> /// <value>
/// The ports. /// The ports.
/// </value> /// </value>
[PublicAPI]
public List<int> Ports { get; } = new List<int>(); public List<int> Ports { get; } = new List<int>();
/// <summary> /// <summary>
@@ -48,11 +50,13 @@ namespace WireMock.Http
/// </summary> /// </summary>
/// <param name="uriPrefixes">The uriPrefixes.</param> /// <param name="uriPrefixes">The uriPrefixes.</param>
/// <param name="httpHandler">The http handler.</param> /// <param name="httpHandler">The http handler.</param>
public TinyHttpServer([NotNull] Action<HttpListenerContext> httpHandler, [NotNull] params string[] uriPrefixes) public TinyHttpServer([NotNull] Action<HttpListenerContext, CancellationToken> httpHandler, [NotNull] params string[] uriPrefixes)
{ {
Check.NotNull(httpHandler, nameof(httpHandler)); Check.NotNull(httpHandler, nameof(httpHandler));
Check.NotEmpty(uriPrefixes, nameof(uriPrefixes)); Check.NotEmpty(uriPrefixes, nameof(uriPrefixes));
_cts = new CancellationTokenSource();
_httpHandler = httpHandler; _httpHandler = httpHandler;
// Create a listener. // Create a listener.
@@ -70,22 +74,26 @@ namespace WireMock.Http
/// <summary> /// <summary>
/// Start the server. /// Start the server.
/// </summary> /// </summary>
[PublicAPI]
public void Start() public void Start()
{ {
_listener.Start(); _listener.Start();
IsStarted = true; IsStarted = true;
_cts = new CancellationTokenSource();
Task.Run( Task.Run(
async () => async () =>
{ {
using (_listener) //using (_listener)
{ {
while (!_cts.Token.IsCancellationRequested) while (!_cts.Token.IsCancellationRequested)
{ {
HttpListenerContext context = await _listener.GetContextAsync(); HttpListenerContext context = await _listener.GetContextAsync();
_httpHandler(context); _httpHandler(context, _cts.Token);
} }
_listener.Stop();
IsStarted = false;
} }
}, },
_cts.Token); _cts.Token);
@@ -94,8 +102,11 @@ namespace WireMock.Http
/// <summary> /// <summary>
/// Stop the server. /// Stop the server.
/// </summary> /// </summary>
[PublicAPI]
public void Stop() public void Stop()
{ {
_listener?.Stop();
_cts.Cancel(); _cts.Cancel();
} }
} }

View File

@@ -13,6 +13,7 @@ using WireMock.Matchers;
using WireMock.Matchers.Request; using WireMock.Matchers.Request;
using WireMock.RequestBuilders; using WireMock.RequestBuilders;
using WireMock.Validation; using WireMock.Validation;
using System.Threading;
namespace WireMock.Server namespace WireMock.Server
{ {
@@ -255,7 +256,7 @@ namespace WireMock.Server
[PublicAPI] [PublicAPI]
public void Stop() public void Stop()
{ {
_httpServer.Stop(); _httpServer?.Stop();
} }
/// <summary> /// <summary>
@@ -432,13 +433,17 @@ namespace WireMock.Server
/// The handle request. /// The handle request.
/// </summary> /// </summary>
/// <param name="ctx">The HttpListenerContext.</param> /// <param name="ctx">The HttpListenerContext.</param>
private async void HandleRequestAsync(HttpListenerContext ctx) /// <param name="cancel">The CancellationToken.</param>
private async void HandleRequestAsync(HttpListenerContext ctx, CancellationToken cancel)
{ {
if (cancel.IsCancellationRequested)
return;
if (_requestProcessingDelay > TimeSpan.Zero) if (_requestProcessingDelay > TimeSpan.Zero)
{ {
lock (_syncRoot) lock (_syncRoot)
{ {
Task.Delay(_requestProcessingDelay.Value).Wait(); Task.Delay(_requestProcessingDelay.Value, cancel).Wait(cancel);
} }
} }

View File

@@ -30,6 +30,16 @@ namespace WireMock.Net.Tests
return current; return current;
} }
[Test]
public void FluentMockServer_StartStop()
{
var server1 = FluentMockServer.Start("http://localhost:9090/");
server1.Stop();
var server2 = FluentMockServer.Start("http://localhost:9090/");
server2.Stop();
}
[Test] [Test]
public void FluentMockServer_ReadStaticMapping_WithNonGuidFilename() public void FluentMockServer_ReadStaticMapping_WithNonGuidFilename()
{ {
@@ -346,7 +356,7 @@ namespace WireMock.Net.Tests
[TearDown] [TearDown]
public void ShutdownServer() public void ShutdownServer()
{ {
_server.Stop(); _server?.Stop();
} }
} }
} }

View File

@@ -25,7 +25,7 @@ namespace WireMock.Net.Tests.Http
var port = PortUtil.FindFreeTcpPort(); var port = PortUtil.FindFreeTcpPort();
bool called = false; bool called = false;
var urlPrefix = "http://localhost:" + port + "/"; var urlPrefix = "http://localhost:" + port + "/";
var server = new TinyHttpServer(ctx => called = true, urlPrefix); var server = new TinyHttpServer((ctx, token) => called = true, urlPrefix);
server.Start(); server.Start();
// when // when

View File

@@ -2,6 +2,7 @@
using System.Collections.Generic; using System.Collections.Generic;
using System.Net; using System.Net;
using System.Net.Http; using System.Net.Http;
using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
using NFluent; using NFluent;
using NUnit.Framework; using NUnit.Framework;
@@ -103,7 +104,7 @@ namespace WireMock.Net.Tests
{ {
private static volatile RequestMessage _lastRequestMessage; private static volatile RequestMessage _lastRequestMessage;
private MapperServer(Action<HttpListenerContext> httpHandler, string urlPrefix) : base(httpHandler, urlPrefix) private MapperServer(Action<HttpListenerContext, CancellationToken> httpHandler, string urlPrefix) : base(httpHandler, urlPrefix)
{ {
} }
@@ -127,7 +128,7 @@ namespace WireMock.Net.Tests
int port = PortUtil.FindFreeTcpPort(); int port = PortUtil.FindFreeTcpPort();
UrlPrefix = "http://localhost:" + port + "/"; UrlPrefix = "http://localhost:" + port + "/";
var server = new MapperServer( var server = new MapperServer(
context => (context, token) =>
{ {
LastRequestMessage = new HttpListenerRequestMapper().Map(context.Request); LastRequestMessage = new HttpListenerRequestMapper().Map(context.Request);
context.Response.Close(); context.Response.Close();

View File

@@ -112,7 +112,7 @@ namespace WireMock.Net.Tests
var responseReady = new AutoResetEvent(false); var responseReady = new AutoResetEvent(false);
HttpListenerResponse response = null; HttpListenerResponse response = null;
_server = new TinyHttpServer( _server = new TinyHttpServer(
context => (context, token) =>
{ {
response = context.Response; response = context.Response;
responseReady.Set(); responseReady.Set();