-
Notifications
You must be signed in to change notification settings - Fork 44
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
Fix a bug in cooperative cancellation for Polling Services causing a request to time out to quickly #558
Merged
Merged
Fix a bug in cooperative cancellation for Polling Services causing a request to time out to quickly #558
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
using System; | ||
using System.Diagnostics; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using FluentAssertions; | ||
using Halibut.Tests.Support; | ||
using Halibut.Tests.Support.TestAttributes; | ||
using Halibut.Tests.Support.TestCases; | ||
using Halibut.Tests.TestServices; | ||
using Halibut.Tests.TestServices.Async; | ||
using Halibut.TestUtils.Contracts; | ||
using NUnit.Framework; | ||
|
||
namespace Halibut.Tests | ||
{ | ||
public class PollingServiceTimeoutsFixture : BaseTest | ||
{ | ||
[Test] | ||
[LatestClientAndLatestServiceTestCases(testNetworkConditions: false, testListening: false)] | ||
public async Task WhenThePollingRequestQueueTimeoutIsReached_TheRequestShouldTimeout(ClientAndServiceTestCase clientAndServiceTestCase) | ||
{ | ||
var halibutTimeoutsAndLimits = new HalibutTimeoutsAndLimitsForTestsBuilder().Build(); | ||
halibutTimeoutsAndLimits.PollingRequestQueueTimeout = TimeSpan.FromSeconds(5); | ||
|
||
await using (var clientAndService = await clientAndServiceTestCase.CreateTestCaseBuilder() | ||
.As<LatestClientAndLatestServiceBuilder>() | ||
.NoService() | ||
.WithStandardServices() | ||
.WithHalibutTimeoutsAndLimits(halibutTimeoutsAndLimits) | ||
.Build(CancellationToken)) | ||
{ | ||
var client = clientAndService.CreateAsyncClient<IEchoService, IAsyncClientEchoServiceWithOptions>(); | ||
|
||
var stopwatch = Stopwatch.StartNew(); | ||
(await AssertAsync.Throws<HalibutClientException>(async () => await client.SayHelloAsync("Hello", new(CancellationToken)))) | ||
.And.Message.Should().Contain("A request was sent to a polling endpoint, but the polling endpoint did not collect the request within the allowed time (00:00:05), so the request timed out."); | ||
stopwatch.Stop(); | ||
|
||
stopwatch.Elapsed.Should().BeLessThan(TimeSpan.FromSeconds(8), "Should have timed out quickly"); | ||
} | ||
} | ||
|
||
[Test] | ||
[LatestClientAndLatestServiceTestCases(testNetworkConditions: false, testListening: false)] | ||
public async Task WhenThePollingRequestQueueTimeoutIsReached_ButTheResponseIsReceivedBeforeThePollingRequestMaximumMessageProcessingTimeoutIsReached_TheRequestShouldSucceed(ClientAndServiceTestCase clientAndServiceTestCase) | ||
{ | ||
var halibutTimeoutsAndLimits = new HalibutTimeoutsAndLimitsForTestsBuilder().Build(); | ||
halibutTimeoutsAndLimits.PollingRequestQueueTimeout = TimeSpan.FromSeconds(5); | ||
halibutTimeoutsAndLimits.PollingRequestMaximumMessageProcessingTimeout = TimeSpan.FromSeconds(100); | ||
|
||
var responseDelay = TimeSpan.FromSeconds(10); | ||
|
||
await using (var clientAndService = await clientAndServiceTestCase.CreateTestCaseBuilder() | ||
.As<LatestClientAndLatestServiceBuilder>() | ||
.WithDoSomeActionService(() => Thread.Sleep(responseDelay)) | ||
.WithHalibutTimeoutsAndLimits(halibutTimeoutsAndLimits) | ||
.WithInstantReconnectPollingRetryPolicy() | ||
.Build(CancellationToken)) | ||
{ | ||
var doSomeActionClient = clientAndService.CreateAsyncClient<IDoSomeActionService, IAsyncClientDoSomeActionServiceWithOptions>(); | ||
|
||
var stopwatch = Stopwatch.StartNew(); | ||
await doSomeActionClient.ActionAsync(new(CancellationToken)); | ||
stopwatch.Stop(); | ||
|
||
stopwatch.Elapsed.Should() | ||
.BeGreaterThan(halibutTimeoutsAndLimits.PollingRequestQueueTimeout, "Should have waited longer than the PollingRequestQueueTimeout").And | ||
.BeLessThan(responseDelay + TimeSpan.FromSeconds(5), "Should have received the response after the 10 second delay + 5 second buffer"); | ||
} | ||
} | ||
|
||
[Test] | ||
[LatestClientAndLatestServiceTestCases(testNetworkConditions: false, testListening: false)] | ||
public async Task WhenThePollingRequestMaximumMessageProcessingTimeoutIsReached_TheRequestShouldTimeout_AndTheTransferringPendingRequestCancelled(ClientAndServiceTestCase clientAndServiceTestCase) | ||
{ | ||
var halibutTimeoutsAndLimits = new HalibutTimeoutsAndLimitsForTestsBuilder().Build(); | ||
halibutTimeoutsAndLimits.PollingRequestQueueTimeout = TimeSpan.FromSeconds(5); | ||
halibutTimeoutsAndLimits.PollingRequestMaximumMessageProcessingTimeout = TimeSpan.FromSeconds(6); | ||
|
||
var waitSemaphore = new SemaphoreSlim(0, 1); | ||
var connectionsObserver = new TestConnectionsObserver(); | ||
|
||
await using (var clientAndService = await clientAndServiceTestCase.CreateTestCaseBuilder() | ||
.As<LatestClientAndLatestServiceBuilder>() | ||
.WithDoSomeActionService(() => waitSemaphore.Wait(CancellationToken)) | ||
.WithHalibutTimeoutsAndLimits(halibutTimeoutsAndLimits) | ||
.WithInstantReconnectPollingRetryPolicy() | ||
.WithConnectionObserverOnTcpServer(connectionsObserver) | ||
.Build(CancellationToken)) | ||
{ | ||
var doSomeActionClient = clientAndService.CreateAsyncClient<IDoSomeActionService, IAsyncClientDoSomeActionServiceWithOptions>(); | ||
|
||
var stopwatch = Stopwatch.StartNew(); | ||
(await AssertAsync.Throws<HalibutClientException>(async () => await doSomeActionClient.ActionAsync(new(CancellationToken)))) | ||
.And.Message.Should().Contain("A request was sent to a polling endpoint, the polling endpoint collected it but did not respond in the allowed time (00:00:06), so the request timed out."); | ||
stopwatch.Stop(); | ||
|
||
stopwatch.Elapsed.Should().BeLessThan(TimeSpan.FromSeconds(15), "Should have timed out quickly"); | ||
|
||
connectionsObserver.ConnectionAcceptedCount.Should().Be(1, "A single TCP connection should have been created"); | ||
|
||
waitSemaphore.Release(); | ||
|
||
Wait.UntilActionSucceeds(() => | ||
{ | ||
connectionsObserver.ConnectionClosedCount.Should().Be(1, "Cancelling the PendingRequest should have caused the TCP Connection to be cancelled to stop the in-flight request"); | ||
connectionsObserver.ConnectionAcceptedCount.Should().Be(2, "The Service should have reconnected after the request was cancelled"); | ||
}, TimeSpan.FromSeconds(30), Logger, CancellationToken); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Removed as everything is Async