Bug: Server not listening after Start() returns (on macOS) #37

Closed
opened 2025-12-29 14:21:25 +01:00 by adam · 12 comments
Owner

Originally created by @benjamin-bader on GitHub (Aug 19, 2017).

Originally assigned to: @StefH on GitHub.

Hi, I just ported a project from .NET 4.5 to .NET Core 2.0 on macOS. I noticed that a unit test using FluentMockServer started failing with System.Net.Http.CurlException : Couldn't connect to server.

When I add Thread.Sleep(100) after starting the server, the error goes away. I'm assuming that, on macOS at least, the underlying server implementation is a bit more asynchronous than that on .NET proper (or on Mono). I notice that FluentMockServer.Start(...) calls StartAsync on its inner HTTP server. Is there a way to wait for that async start to finish that's more reliable than putting the test thread to sleep?

Originally created by @benjamin-bader on GitHub (Aug 19, 2017). Originally assigned to: @StefH on GitHub. Hi, I just ported a project from .NET 4.5 to .NET Core 2.0 on macOS. I noticed that a unit test using FluentMockServer started failing with `System.Net.Http.CurlException : Couldn't connect to server`. When I add `Thread.Sleep(100)` after starting the server, the error goes away. I'm assuming that, on macOS at least, the underlying server implementation is a bit more asynchronous than that on .NET proper (or on Mono). I notice that `FluentMockServer.Start(...)` calls `StartAsync` on its inner HTTP server. Is there a way to wait for that async start to finish that's more reliable than putting the test thread to sleep?
adam added the bug label 2025-12-29 14:21:25 +01:00
adam closed this issue 2025-12-29 14:21:25 +01:00
Author
Owner

@StefH commented on GitHub (Aug 19, 2017):

Can you post the full code where you put the Thread.Sleep()?

@StefH commented on GitHub (Aug 19, 2017): Can you post the full code where you put the Thread.Sleep()?
Author
Owner

@benjamin-bader commented on GitHub (Aug 19, 2017):

I can't post the actual source (proprietary, etc), but here's the essence. It's an xunit test class:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;
using FluentAssertions;
using WireMock.RequestBuilders;
using WireMock.ResponseBuilders;
using WireMock.Server;
using Xunit;

namespace Service
{
    public class ServiceClientTest : IDisposable
    {
        private FluentMockServer server;
        private MyServiceClient client;

        public ServiceClientTest()
        {
            server = FluentMockServer.Start();
            client = new MyServiceClient(server.Urls.Select(u => new Uri(u)).First());
        }

        public void Dispose()
        {
            server.Dispose();
            client.Dispose();
        }
        
        [Fact]
        public async Task ClientRequestIsWellFormed()
        {
            var request = new ServiceRequest() { ... };

            server.Given(Request.Create().WithPath("/api/endpoint").UsingPost())
                .RespondWith(Response.Create()
                    .WithStatusCode(200)
                    .WithBodyAsJson(new Dictionary<string, object>
                    {
                        ["id"]              = "requestId",
                        ["analysisId"]      = Guid.NewGuid()
                    }));

            // Request fails if we don't sleep here, because the server has not yet finished starting.
            Thread.Sleep(100);

            var response = await client.SendRequestAsync(request);

            Assert.NotNull(response);
        }
    }
}
@benjamin-bader commented on GitHub (Aug 19, 2017): I can't post the actual source (proprietary, etc), but here's the essence. It's an xunit test class: ```csharp using System; using System.Collections.Generic; using System.Linq; using System.Net.Http; using System.Threading; using System.Threading.Tasks; using FluentAssertions; using WireMock.RequestBuilders; using WireMock.ResponseBuilders; using WireMock.Server; using Xunit; namespace Service { public class ServiceClientTest : IDisposable { private FluentMockServer server; private MyServiceClient client; public ServiceClientTest() { server = FluentMockServer.Start(); client = new MyServiceClient(server.Urls.Select(u => new Uri(u)).First()); } public void Dispose() { server.Dispose(); client.Dispose(); } [Fact] public async Task ClientRequestIsWellFormed() { var request = new ServiceRequest() { ... }; server.Given(Request.Create().WithPath("/api/endpoint").UsingPost()) .RespondWith(Response.Create() .WithStatusCode(200) .WithBodyAsJson(new Dictionary<string, object> { ["id"] = "requestId", ["analysisId"] = Guid.NewGuid() })); // Request fails if we don't sleep here, because the server has not yet finished starting. Thread.Sleep(100); var response = await client.SendRequestAsync(request); Assert.NotNull(response); } } } ```
Author
Owner

@skunkworks commented on GitHub (Sep 26, 2017):

I can confirm that I've had the same issue with WireMock.NET not starting up the server in time on MacOS.

@skunkworks commented on GitHub (Sep 26, 2017): I can confirm that I've had the same issue with WireMock.NET not starting up the server in time on MacOS.
Author
Owner

@StefH commented on GitHub (Sep 26, 2017):

Are you running on netcore 1.x or 2.0 ?

@StefH commented on GitHub (Sep 26, 2017): Are you running on netcore 1.x or 2.0 ?
Author
Owner

@skunkworks commented on GitHub (Sep 26, 2017):

@StefH This is on .NET Core 2.0. Adding a 100ms thread sleep works around the issue for us.

@skunkworks commented on GitHub (Sep 26, 2017): @StefH This is on .NET Core 2.0. Adding a 100ms thread sleep works around the issue for us.
Author
Owner

@StefH commented on GitHub (Sep 27, 2017):

@skunkworks OK. Clear.

I was wondering if there is an environment setting to see if I'm running on Linux, Windows or MacOS... I'll investigate this.

@StefH commented on GitHub (Sep 27, 2017): @skunkworks OK. Clear. I was wondering if there is an environment setting to see if I'm running on Linux, Windows or MacOS... I'll investigate this.
Author
Owner

@benjamin-bader commented on GitHub (Sep 27, 2017):

What do you think about returning a Task in the public FluentMockServer API? The inner HTTP server already seems to, but the server doesn't do anything with it.

Alternately, why not block on the task returned by the inner HTTP server?

@benjamin-bader commented on GitHub (Sep 27, 2017): What do you think about returning a `Task` in the public `FluentMockServer` API? The inner HTTP server already seems to, but the server doesn't do anything with it. Alternately, why not block on the task returned by the inner HTTP server?
Author
Owner

@StefH commented on GitHub (Sep 30, 2017):

Should be fixed. Download source or use latest NuGet version 1.0.2.4-preview-02.

@StefH commented on GitHub (Sep 30, 2017): Should be fixed. Download source or use latest NuGet version `1.0.2.4-preview-02`.
Author
Owner

@StefH commented on GitHub (Oct 3, 2017):

@benjamin-bader and @skunkworks : were you able to test this new source-code / NuGet ?

@StefH commented on GitHub (Oct 3, 2017): @benjamin-bader and @skunkworks : were you able to test this new source-code / NuGet ?
Author
Owner

@skunkworks commented on GitHub (Oct 4, 2017):

We'll give it a shot and report back. Thanks for the fix!

@skunkworks commented on GitHub (Oct 4, 2017): We'll give it a shot and report back. Thanks for the fix!
Author
Owner

@sdcoffey commented on GitHub (Oct 5, 2017):

@StefH looks like this update worked, thanks for the quick fix!

@sdcoffey commented on GitHub (Oct 5, 2017): @StefH looks like this update worked, thanks for the quick fix!
Author
Owner

@StefH commented on GitHub (Oct 11, 2017):

@skunkworks : I close this issue now, if you have more troubles, create a new issue.

@StefH commented on GitHub (Oct 11, 2017): @skunkworks : I close this issue now, if you have more troubles, create a new issue.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/WireMock.Net-wiremock#37