Add start delay from 100ms to fix startup issue on MacOS (#44)

This commit is contained in:
Stef Heyenrath
2017-09-30 10:03:00 +02:00
parent 881e4b2af3
commit 8827531391
6 changed files with 55 additions and 57 deletions

View File

@@ -5,8 +5,6 @@ using System.Threading.Tasks;
using JetBrains.Annotations;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using WireMock.Http;
using WireMock.Validation;
@@ -18,6 +16,8 @@ namespace WireMock.Owin
private readonly WireMockMiddlewareOptions _options;
private readonly string[] _uriPrefixes;
private IWebHost _host;
public bool IsStarted { get; private set; }
public List<string> Urls { get; } = new List<string>();
@@ -33,9 +33,7 @@ namespace WireMock.Owin
{
Urls.Add(uriPrefix);
int port;
string host;
PortUtil.TryExtractProtocolAndPort(uriPrefix, out host, out port);
PortUtil.TryExtractProtocolAndPort(uriPrefix, out string host, out int port);
Ports.Add(port);
}
@@ -45,27 +43,26 @@ namespace WireMock.Owin
public Task StartAsync()
{
IWebHost host = new WebHostBuilder()
// .ConfigureLogging(factory => factory.AddConsole(LogLevel.None))
.Configure(appBuilder =>
{
appBuilder.UseMiddleware<WireMockMiddleware>(_options);
})
.UseKestrel()
.UseUrls(_uriPrefixes)
.Build();
_host = new WebHostBuilder()
.Configure(appBuilder =>
{
appBuilder.UseMiddleware<WireMockMiddleware>(_options);
})
.UseKestrel()
.UseUrls(_uriPrefixes)
.Build();
#if NETSTANDARD1_3
System.Console.WriteLine("WireMock.Net server using netstandard1.3");
return Task.Run(() =>
{
host.Run(_cts.Token);
_host.Run(_cts.Token);
IsStarted = true;
}, _cts.Token);
#else
System.Console.WriteLine("WireMock.Net server using netstandard2.0");
IsStarted = true;
return host.RunAsync(_cts.Token);
return _host.RunAsync(_cts.Token);
#endif
}
@@ -74,8 +71,11 @@ namespace WireMock.Owin
_cts.Cancel();
IsStarted = false;
#if NETSTANDARD1_3
return Task.FromResult(true);
#else
return _host.WaitForShutdownAsync();
#endif
}
}
}

View File

@@ -4,6 +4,7 @@ using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using JetBrains.Annotations;
using WireMock.Http;
using WireMock.Matchers;
@@ -20,6 +21,7 @@ namespace WireMock.Server
/// </summary>
public partial class FluentMockServer : IDisposable
{
private const int ServerStartDelay = 100;
private readonly IOwinSelfHost _httpServer;
private readonly object _syncRoot = new object();
private readonly WireMockMiddlewareOptions _options = new WireMockMiddlewareOptions();
@@ -173,6 +175,9 @@ namespace WireMock.Server
_httpServer.StartAsync();
// Fix for 'Bug: Server not listening after Start() returns (on macOS)'
Task.Delay(ServerStartDelay).Wait();
if (settings.AllowPartialMapping == true)
{
AllowPartialMapping();
@@ -318,7 +323,10 @@ namespace WireMock.Server
Check.NotNull(password, nameof(password));
string authorization = Convert.ToBase64String(Encoding.GetEncoding("ISO-8859-1").GetBytes(username + ":" + password));
_options.AuthorizationMatcher = new RegexMatcher("^(?i)BASIC " + authorization + "$");
lock (_syncRoot)
{
_options.AuthorizationMatcher = new RegexMatcher("^(?i)BASIC " + authorization + "$");
}
}
/// <summary>
@@ -327,7 +335,10 @@ namespace WireMock.Server
[PublicAPI]
public void RemoveBasicAuthentication()
{
_options.AuthorizationMatcher = null;
lock (_syncRoot)
{
_options.AuthorizationMatcher = null;
}
}
/// <summary>
@@ -337,7 +348,10 @@ namespace WireMock.Server
[PublicAPI]
public void SetMaxRequestLogCount([CanBeNull] int? maxRequestLogCount)
{
_options.MaxRequestLogCount = maxRequestLogCount;
lock (_syncRoot)
{
_options.MaxRequestLogCount = maxRequestLogCount;
}
}
/// <summary>
@@ -347,7 +361,10 @@ namespace WireMock.Server
[PublicAPI]
public void SetRequestLogExpirationDuration([CanBeNull] int? requestLogExpirationDuration)
{
_options.RequestLogExpirationDuration = requestLogExpirationDuration;
lock (_syncRoot)
{
_options.RequestLogExpirationDuration = requestLogExpirationDuration;
}
}
/// <summary>