-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
async TimeoutPolicy even in pessimistic mode cannot time out on a badly-behaved not-truly-async-but-sync-instead API #318
Comments
@joaoasrosa Thank you for the detailed repro. This test within Polly, is intended to demonstrate that the scenario you are using with I note that the Maybe you could adapt the test as follows, to check that the
EDIT: If this comment is off the mark, come back, and we'll dig deeper. |
@reisenberger thanks for your quick reply. From the mentioned test, the code is similar, e.g., use the Regarding your concerns:
I added a The results are the same, the timeout policy is not triggered. And your comment is not off the mark. :) Thanks for the help! |
@joaoasrosa A bit of lunchtime debugging: If you rewrite the test as follows, it passes: public async Task GetDocumentAsync_WhenCallAboveThreshold_TriggersTimeout()
{
_clientMock.Setup(x =>
x.GetObjectAsync(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<CancellationToken>()))
.Returns(async () =>
{
await Task.Delay(2000);
return new GetObjectResponse();
});
await Record.ExceptionAsync(async () => { await _sut.GetDocumentAsync("dummy", "document.json"); });
_policy.TimeoutTriggered.Should().BeTrue();
} The reason for your original test failing looks to be to do with the fact that Moq's (1) Any (2) Moq's (3) I experimented with writing (4) Hence the final version of the test I came up with. Essentially, to simulate a well-written-for-async but slow-responding async call, the test needs to simulate asynchronously waiting for 2 seconds, not synchronously, before returning the delayed response. Hope that detailed explanation of why the original didn't work, helps. This does indicate that Polly's |
@joaoasrosa I deduced those points (2) and (3) above about Moq by quick observation of what was happening, but a brief google now seems to bear out my hunches: https://stackoverflow.com/questions/36583780/async-callback-on-mocked-object-not-awaiting |
PS Thanks for the explanation:
(I'd missed the last param being optional, meaning the setup did cover the matching signature!) |
@reisenberger Thanks for your lunchtime. Cheers |
Hi,
I'm using Polly framework (namely Circuit Breaker and Retry policies) in an async context. I'm trying to integrate the
Timeout
policy with it, but it isn't triggered.I build a little POC, where we can see an example of the policy within a repository, being called by the WebApi.
The policy looks like:
and the policy is used within the repository:
With unit tests we can try to prove the timeout is triggered:
with the
ResiliencePolicyStub
as:The
IAmazonS3
mock has a callback to enforce the thread sleep, with a value above the timeout threshold. The delegate function is not triggered (theTimeoutTriggered
property doesn't change) neither it receives an exception (TimeoutException
).From the samples there is another mechanism in place, however, from an API perspective the calls are
async
by "default".What is missing with the timeout policy configuration?
Note: the example was built with dotnet core 2.0.
The text was updated successfully, but these errors were encountered: