Skip to content
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

Stabilize AddHedging_IntegrationTest test #1644

Merged
merged 2 commits into from
Sep 27, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -36,45 +36,64 @@ public async Task AddHedging_IntegrationTest()
{
int hedgingCount = 0;

var strategy = _builder
.AddHedging(new()
var strategy = _builder.AddHedging(new()
{
MaxHedgedAttempts = 4,
Delay = System.Threading.Timeout.InfiniteTimeSpan,
ShouldHandle = args => args.Outcome.Result switch
{
MaxHedgedAttempts = 4,
Delay = TimeSpan.FromMilliseconds(20),
ShouldHandle = args => args.Outcome.Result switch
{
"error" => PredicateResult.True(),
_ => PredicateResult.False()
},
ActionGenerator = args =>
"error" => PredicateResult.True(),
_ => PredicateResult.False()
},
ActionGenerator = args =>
{
return () => args.AttemptNumber switch
{
return async () =>
{
await Task.Delay(25, args.ActionContext.CancellationToken);
3 => Outcome.FromResultAsValueTask("success"),
_ => Outcome.FromResultAsValueTask("error")
};
},
OnHedging = args =>
{
Interlocked.Increment(ref hedgingCount);
return default;
}
})
.Build();

if (args.AttemptNumber == 3)
{
return Outcome.FromResult("success");
}
var result = await strategy.ExecuteAsync(token => new ValueTask<string>("error"));
result.Should().Be("success");
hedgingCount.Should().Be(3);
}

return Outcome.FromResult("error");
};
},
OnHedging = args =>
[Fact]
public async Task AddHedging_IntegrationTestWithRealDelay()
{
var strategy = _builder.AddHedging(new()
{
MaxHedgedAttempts = 4,
ShouldHandle = args => args.Outcome.Result switch
{
"error" => PredicateResult.True(),
_ => PredicateResult.False()
},
ActionGenerator = args =>
{
return async () =>
{
hedgingCount++;
return default;
}
})
.Build();
await Task.Delay(20);

var result = await strategy.ExecuteAsync(async token =>
{
await Task.Delay(25, token);
return "error";
});
return args.AttemptNumber switch
{
3 => Outcome.FromResult("success"),
_ => Outcome.FromResult("error")
};
};
}
})
.Build();

var result = await strategy.ExecuteAsync(token => new ValueTask<string>("error"));
result.Should().Be("success");
hedgingCount.Should().Be(4);
}
}