Skip to content

Commit

Permalink
Add start delay from 100ms to fix startup issue on MacOS (#44)
Browse files Browse the repository at this point in the history
  • Loading branch information
StefH committed Sep 30, 2017
1 parent 881e4b2 commit 8827531
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 57 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFrameworks>netcoreapp1.1;netcoreapp2.0</TargetFrameworks>
<TargetFrameworks>netcoreapp2.0;netcoreapp1.1</TargetFrameworks>
<ApplicationIcon>../../WireMock.Net-Logo.ico</ApplicationIcon>
</PropertyGroup>

Expand Down
2 changes: 1 addition & 1 deletion examples/WireMock.Net.StandAlone.Net452/Program.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using System;

namespace WireMock.Net.StandAlone
namespace WireMock.Net.StandAlone.Net452
{
public class Program
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
<ProjectGuid>{668F689E-57B4-422E-8846-C0FF643CA999}</ProjectGuid>
<OutputType>Exe</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>WireMock.Net.StandAlone</RootNamespace>
<AssemblyName>WireMock.Net.StandAlone</AssemblyName>
<RootNamespace>WireMock.Net.StandAlone.Net452</RootNamespace>
<AssemblyName>WireMock.Net.StandAlone.Net452</AssemblyName>
<TargetFrameworkVersion>v4.5.2</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
Expand All @@ -35,38 +35,15 @@
<PropertyGroup>
<ApplicationIcon>..\..\WireMock.Net-Logo.ico</ApplicationIcon>
</PropertyGroup>
<PropertyGroup>
<StartupObject>WireMock.Net.StandAlone.Net452.Program</StartupObject>
</PropertyGroup>
<ItemGroup>
<Reference Include="Handlebars, Version=1.8.1.0, Culture=neutral, PublicKeyToken=22225d0bf33cd661, processorArchitecture=MSIL">
<HintPath>..\..\packages\Handlebars.Net.1.9.0\lib\net40\Handlebars.dll</HintPath>
</Reference>
<Reference Include="Microsoft.Owin.Host.HttpListener, Version=3.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
<HintPath>..\..\packages\Microsoft.Owin.Host.HttpListener.3.1.0\lib\net45\Microsoft.Owin.Host.HttpListener.dll</HintPath>
</Reference>
<Reference Include="Newtonsoft.Json, Version=9.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
<HintPath>..\..\packages\Newtonsoft.Json.9.0.1\lib\net45\Newtonsoft.Json.dll</HintPath>
</Reference>
<Reference Include="RestEase, Version=1.4.1.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\..\packages\RestEase.1.4.1\lib\net45\RestEase.dll</HintPath>
</Reference>
<Reference Include="SimMetrics.Net, Version=1.0.3.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\..\packages\SimMetrics.Net.1.0.3\lib\net45\SimMetrics.Net.dll</HintPath>
</Reference>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Numerics" />
<Reference Include="System.Xml" />
<Reference Include="System.Xml.Linq" />
<Reference Include="WireMock.Net, Version=1.0.2.0, Culture=neutral, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\..\src\WireMock.Net.StandAlone\bin\$(Configuration)\net452\WireMock.Net.dll</HintPath>
</Reference>
<Reference Include="WireMock.Net.StandAlone, Version=1.0.0.0, Culture=neutral, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\..\src\WireMock.Net.StandAlone\bin\$(Configuration)\net452\WireMock.Net.StandAlone.dll</HintPath>
</Reference>
<Reference Include="XPath2, Version=1.0.3.1, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\..\packages\XPath2.1.0.3.1\lib\net452\XPath2.dll</HintPath>
</Reference>
</ItemGroup>
<ItemGroup>
<Compile Include="Program.cs" />
Expand All @@ -75,6 +52,16 @@
<ItemGroup>
<None Include="packages.config" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\src\WireMock.Net.StandAlone\WireMock.Net.StandAlone.csproj">
<Project>{b6269aac-170a-43d5-8b9a-579ded3d9a95}</Project>
<Name>WireMock.Net.StandAlone</Name>
</ProjectReference>
<ProjectReference Include="..\..\src\WireMock.Net\WireMock.Net.csproj">
<Project>{d3804228-91f4-4502-9595-39584e5a01ad}</Project>
<Name>WireMock.Net</Name>
</ProjectReference>
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
Expand Down
6 changes: 0 additions & 6 deletions examples/WireMock.Net.StandAlone.Net452/packages.config
Original file line number Diff line number Diff line change
@@ -1,9 +1,3 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="Handlebars.Net" version="1.9.0" targetFramework="net452" />
<package id="Microsoft.Owin.Host.HttpListener" version="3.1.0" targetFramework="net452" />
<package id="Newtonsoft.Json" version="9.0.1" targetFramework="net452" />
<package id="RestEase" version="1.4.1" targetFramework="net452" />
<package id="SimMetrics.Net" version="1.0.3" targetFramework="net452" />
<package id="XPath2" version="1.0.3.1" targetFramework="net452" />
</packages>
34 changes: 17 additions & 17 deletions src/WireMock.Net/Owin/AspNetCoreSelfHost.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
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;

Expand All @@ -18,6 +16,8 @@ internal class AspNetCoreSelfHost : IOwinSelfHost
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>();
Expand All @@ -33,9 +33,7 @@ public AspNetCoreSelfHost([NotNull] WireMockMiddlewareOptions options, [NotNull]
{
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);
}

Expand All @@ -45,27 +43,26 @@ public AspNetCoreSelfHost([NotNull] WireMockMiddlewareOptions options, [NotNull]

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
}

Expand All @@ -74,8 +71,11 @@ public Task StopAsync()
_cts.Cancel();

IsStarted = false;

#if NETSTANDARD1_3
return Task.FromResult(true);
#else
return _host.WaitForShutdownAsync();
#endif
}
}
}
Expand Down
25 changes: 21 additions & 4 deletions src/WireMock.Net/Server/FluentMockServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using JetBrains.Annotations;
using WireMock.Http;
using WireMock.Matchers;
Expand All @@ -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();
Expand Down Expand Up @@ -173,6 +175,9 @@ private FluentMockServer(FluentMockServerSettings settings)

_httpServer.StartAsync();

// Fix for 'Bug: Server not listening after Start() returns (on macOS)'
Task.Delay(ServerStartDelay).Wait();

if (settings.AllowPartialMapping == true)
{
AllowPartialMapping();
Expand Down Expand Up @@ -318,7 +323,10 @@ public void SetBasicAuthentication([NotNull] string username, [NotNull] string p
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>
Expand All @@ -327,7 +335,10 @@ public void SetBasicAuthentication([NotNull] string username, [NotNull] string p
[PublicAPI]
public void RemoveBasicAuthentication()
{
_options.AuthorizationMatcher = null;
lock (_syncRoot)
{
_options.AuthorizationMatcher = null;
}
}

/// <summary>
Expand All @@ -337,7 +348,10 @@ public void RemoveBasicAuthentication()
[PublicAPI]
public void SetMaxRequestLogCount([CanBeNull] int? maxRequestLogCount)
{
_options.MaxRequestLogCount = maxRequestLogCount;
lock (_syncRoot)
{
_options.MaxRequestLogCount = maxRequestLogCount;
}
}

/// <summary>
Expand All @@ -347,7 +361,10 @@ public void SetMaxRequestLogCount([CanBeNull] int? maxRequestLogCount)
[PublicAPI]
public void SetRequestLogExpirationDuration([CanBeNull] int? requestLogExpirationDuration)
{
_options.RequestLogExpirationDuration = requestLogExpirationDuration;
lock (_syncRoot)
{
_options.RequestLogExpirationDuration = requestLogExpirationDuration;
}
}

/// <summary>
Expand Down

0 comments on commit 8827531

Please sign in to comment.