-
-
Notifications
You must be signed in to change notification settings - Fork 233
Add WireMockHealthCheck in WireMock.Net.Aspire #1375
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
base: master
Are you sure you want to change the base?
Conversation
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.
StefH
left a comment
There was a problem hiding this 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) |
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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); |
There was a problem hiding this comment.
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 () => |
There was a problem hiding this comment.
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) |
There was a problem hiding this comment.
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?
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/healthand 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
#help-contributingor a project-specific channel like#wiremock-java