mirror of
https://github.com/wiremock/WireMock.Net.git
synced 2026-01-16 23:26:52 +01:00
HttpListener : Stop fix
This commit is contained in:
@@ -13,11 +13,11 @@ namespace WireMock.Http
|
||||
/// </summary>
|
||||
public class TinyHttpServer
|
||||
{
|
||||
private readonly Action<HttpListenerContext> _httpHandler;
|
||||
private readonly Action<HttpListenerContext, CancellationToken> _httpHandler;
|
||||
|
||||
private readonly HttpListener _listener;
|
||||
|
||||
private CancellationTokenSource _cts;
|
||||
private readonly CancellationTokenSource _cts;
|
||||
|
||||
/// <summary>
|
||||
/// Gets a value indicating whether this server is started.
|
||||
@@ -33,6 +33,7 @@ namespace WireMock.Http
|
||||
/// <value>
|
||||
/// The urls.
|
||||
/// </value>
|
||||
[PublicAPI]
|
||||
public List<Uri> Urls { get; } = new List<Uri>();
|
||||
|
||||
/// <summary>
|
||||
@@ -41,6 +42,7 @@ namespace WireMock.Http
|
||||
/// <value>
|
||||
/// The ports.
|
||||
/// </value>
|
||||
[PublicAPI]
|
||||
public List<int> Ports { get; } = new List<int>();
|
||||
|
||||
/// <summary>
|
||||
@@ -48,11 +50,13 @@ namespace WireMock.Http
|
||||
/// </summary>
|
||||
/// <param name="uriPrefixes">The uriPrefixes.</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.NotEmpty(uriPrefixes, nameof(uriPrefixes));
|
||||
|
||||
_cts = new CancellationTokenSource();
|
||||
|
||||
_httpHandler = httpHandler;
|
||||
|
||||
// Create a listener.
|
||||
@@ -70,22 +74,26 @@ namespace WireMock.Http
|
||||
/// <summary>
|
||||
/// Start the server.
|
||||
/// </summary>
|
||||
[PublicAPI]
|
||||
public void Start()
|
||||
{
|
||||
_listener.Start();
|
||||
|
||||
IsStarted = true;
|
||||
|
||||
_cts = new CancellationTokenSource();
|
||||
Task.Run(
|
||||
async () =>
|
||||
{
|
||||
using (_listener)
|
||||
//using (_listener)
|
||||
{
|
||||
while (!_cts.Token.IsCancellationRequested)
|
||||
{
|
||||
HttpListenerContext context = await _listener.GetContextAsync();
|
||||
_httpHandler(context);
|
||||
_httpHandler(context, _cts.Token);
|
||||
}
|
||||
|
||||
_listener.Stop();
|
||||
IsStarted = false;
|
||||
}
|
||||
},
|
||||
_cts.Token);
|
||||
@@ -94,8 +102,11 @@ namespace WireMock.Http
|
||||
/// <summary>
|
||||
/// Stop the server.
|
||||
/// </summary>
|
||||
[PublicAPI]
|
||||
public void Stop()
|
||||
{
|
||||
_listener?.Stop();
|
||||
|
||||
_cts.Cancel();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -13,6 +13,7 @@ using WireMock.Matchers;
|
||||
using WireMock.Matchers.Request;
|
||||
using WireMock.RequestBuilders;
|
||||
using WireMock.Validation;
|
||||
using System.Threading;
|
||||
|
||||
namespace WireMock.Server
|
||||
{
|
||||
@@ -255,7 +256,7 @@ namespace WireMock.Server
|
||||
[PublicAPI]
|
||||
public void Stop()
|
||||
{
|
||||
_httpServer.Stop();
|
||||
_httpServer?.Stop();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -432,13 +433,17 @@ namespace WireMock.Server
|
||||
/// The handle request.
|
||||
/// </summary>
|
||||
/// <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)
|
||||
{
|
||||
lock (_syncRoot)
|
||||
{
|
||||
Task.Delay(_requestProcessingDelay.Value).Wait();
|
||||
Task.Delay(_requestProcessingDelay.Value, cancel).Wait(cancel);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -30,6 +30,16 @@ namespace WireMock.Net.Tests
|
||||
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]
|
||||
public void FluentMockServer_ReadStaticMapping_WithNonGuidFilename()
|
||||
{
|
||||
@@ -346,7 +356,7 @@ namespace WireMock.Net.Tests
|
||||
[TearDown]
|
||||
public void ShutdownServer()
|
||||
{
|
||||
_server.Stop();
|
||||
_server?.Stop();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -25,7 +25,7 @@ namespace WireMock.Net.Tests.Http
|
||||
var port = PortUtil.FindFreeTcpPort();
|
||||
bool called = false;
|
||||
var urlPrefix = "http://localhost:" + port + "/";
|
||||
var server = new TinyHttpServer(ctx => called = true, urlPrefix);
|
||||
var server = new TinyHttpServer((ctx, token) => called = true, urlPrefix);
|
||||
server.Start();
|
||||
|
||||
// when
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Net;
|
||||
using System.Net.Http;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using NFluent;
|
||||
using NUnit.Framework;
|
||||
@@ -103,7 +104,7 @@ namespace WireMock.Net.Tests
|
||||
{
|
||||
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();
|
||||
UrlPrefix = "http://localhost:" + port + "/";
|
||||
var server = new MapperServer(
|
||||
context =>
|
||||
(context, token) =>
|
||||
{
|
||||
LastRequestMessage = new HttpListenerRequestMapper().Map(context.Request);
|
||||
context.Response.Close();
|
||||
|
||||
@@ -112,7 +112,7 @@ namespace WireMock.Net.Tests
|
||||
var responseReady = new AutoResetEvent(false);
|
||||
HttpListenerResponse response = null;
|
||||
_server = new TinyHttpServer(
|
||||
context =>
|
||||
(context, token) =>
|
||||
{
|
||||
response = context.Response;
|
||||
responseReady.Set();
|
||||
|
||||
Reference in New Issue
Block a user