Skip to content

Conversation

@Zguy
Copy link

@Zguy Zguy commented Nov 5, 2025

I know the contributing guidelines says to make an issue first to have a discussion, but we can have the discussion here and I don't mind if the effort was wasted.

The point of this change is to make WaitFor() in Aspire more useful with WireMock, since it currently doesn't wait for anything. This can be a problem and create a race condition if the main project starts doing HTTP calls to the wiremock server immediately on startup, and will on some runs get a 404 back.

To fix that I've added a health check that calls /__admin/health and checks the result, as well as checks if mappings using AdminApiMappingBuilder has been submitted to the server.

That last part created a catch-22 problem where the mappings were not submitted until the health check was healthy, but the health check was not healthy until the mappings were submitted.

To avoid this, the WireMockServerLifecycleHook class has been slightly re-arranged, and is now using the AfterEndpointsAllocatedAsync callback rather than the AfterResourcesCreatedAsync callback. Within which a separate Task is created that waits until the server is ready and submits the mappings.

Submitter checklist

  • Recommended: Join WireMock Slack to get any help in #help-contributing or a project-specific channel like #wiremock-java
  • The PR request is well described and justified, including the body and the references
  • The PR title represents the desired changelog entry
  • The repository's code style is followed (see the contributing guide)
  • Test coverage that demonstrates that the change works as expected
  • For new features, there's necessary documentation in this pull request or in a subsequent PR to wiremock.org

For use with Aspire, to make WaitFor(wiremock) more useful.
Calls /__admin/health and checks the result, as well as checks if mappings using AdminApiMappingBuilder has been submitted to the server.

This created a catch-22 problem where the mappings were not submitted until the health check was healthy, but the health check was not healthy until the mappings were submitted.

To avoid this, the WireMockServerLifecycleHook class has been slightly re-arranged, and is now using the AfterEndpointsAllocatedAsync callback rather than the AfterResourcesCreatedAsync callback. Within which a separate Task is created that waits until the server is ready and submits the mappings.
Copy link
Collaborator

@StefH StefH left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

some comments / questions

public async ValueTask DisposeAsync()
{
await _shutdownCts.CancelAsync();
if (_mappingTask is not null)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you add { and { for if statements?

internal WireMockServerArguments Arguments { get; }
internal Lazy<IWireMockAdminApi> AdminApi => new(CreateWireMockAdminApi);

internal enum MappingState
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you move this enum to its own file?


public async Task AfterEndpointsAllocatedAsync(DistributedApplicationModel appModel, CancellationToken cancellationToken = default)
{
var cts = CancellationTokenSource.CreateLinkedTokenSource(_shutdownCts.Token, cancellationToken);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sonar complains on this.

Maybe dispose this cts in DisposeAsync method?

.ToArray();

foreach (var wireMockServerResource in wireMockServerResources)
_mappingTask = Task.Run(async () =>
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe use SafeFireAndForget (https://github.com/TheCodeTraveler/AsyncAwaitBestPractices) or something similar to indicate that this is a long running task and exceptions do not matter or are only logged?

return HealthCheckResult.Unhealthy("WireMock.Net is not healthy");
}

if (resource.ApiMappingState == WireMockServerResource.MappingState.NotSubmitted)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What if I post some mappings.
And then clean all mappings?

Will the HealthStatus be not-healthy?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants