Skip to content

Commit

Permalink
fix: Robustly start messaging provider on MacOS
Browse files Browse the repository at this point in the history
  • Loading branch information
adamrodger committed Feb 15, 2024
1 parent 6950544 commit b45f0ea
Showing 1 changed file with 21 additions and 11 deletions.
32 changes: 21 additions & 11 deletions src/PactNet/Verifier/Messaging/MessagingProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,23 +60,33 @@ public Uri Start(JsonSerializerOptions settings)
Guard.NotNull(settings, nameof(settings));
this.defaultSettings = settings;

try
while (true)
{
int port = FindUnusedPort();
var uri = new Uri($"http://localhost:{port}/pact-messages/");
Uri uri;

this.config.WriteLine($"Starting messaging provider at {uri}");
this.server.Prefixes.Add(uri.AbsoluteUri);
try
{
int port = FindUnusedPort();
uri = new Uri($"http://localhost:{port}/pact-messages/");

this.server.Start();
this.thread.Start();
this.config.WriteLine($"Starting messaging provider at {uri}");
this.server.Prefixes.Add(uri.AbsoluteUri);
this.server.Start();
}
catch (HttpListenerException e) when (e.Message == "Address already in use")
{
// handle intermittent race condition, mostly on MacOS, where a port says it's unused but still throws when you try to use it
this.config.WriteLine("Failed to start messaging provider as the port is already in use, retrying...");
continue;
}
catch (Exception e)
{
throw new PactFailureException("Unable to start the internal messaging server", e);
}

this.thread.Start();
return uri;
}
catch (Exception e)
{
throw new PactFailureException("Unable to start the internal messaging server", e);
}
}

/// <summary>
Expand Down

0 comments on commit b45f0ea

Please sign in to comment.