Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
benjamin-bader opened this issue Aug 19, 2017 · 12 comments
Closed

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

benjamin-bader opened this issue Aug 19, 2017 · 12 comments
Assignees
Labels

Comments

@benjamin-bader
Copy link

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?

@StefH StefH changed the title Server not listening after Start() returns Bug: Server not listening after Start() returns (on macOS) Aug 19, 2017
@StefH StefH self-assigned this Aug 19, 2017
@StefH StefH added the bug label Aug 19, 2017
@StefH
Copy link
Collaborator

StefH commented Aug 19, 2017

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

@benjamin-bader
Copy link
Author

benjamin-bader commented 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);
        }
    }
}

@skunkworks
Copy link

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

@StefH
Copy link
Collaborator

StefH commented Sep 26, 2017

Are you running on netcore 1.x or 2.0 ?

@skunkworks
Copy link

skunkworks commented Sep 26, 2017

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

@StefH
Copy link
Collaborator

StefH commented 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.

@benjamin-bader
Copy link
Author

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?

@StefH
Copy link
Collaborator

StefH commented Sep 30, 2017

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

@StefH
Copy link
Collaborator

StefH commented Oct 3, 2017

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

@skunkworks
Copy link

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

@sdcoffey
Copy link

sdcoffey commented Oct 5, 2017

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

@StefH
Copy link
Collaborator

StefH commented Oct 11, 2017

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants