-
Notifications
You must be signed in to change notification settings - Fork 762
/
Copy pathStandardHedgingTests.cs
283 lines (232 loc) · 10.6 KB
/
StandardHedgingTests.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System;
using System.Collections.Generic;
using System.Net;
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;
using FluentAssertions;
using Microsoft.Extensions.Compliance.Testing;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;
using Microsoft.Extensions.Http.Resilience.Internal;
using Microsoft.Extensions.Http.Resilience.Routing.Internal;
using Microsoft.Extensions.Http.Resilience.Test.Helpers;
using Microsoft.Extensions.Options;
using Moq;
using Polly;
using Polly.Hedging;
using Polly.Registry;
using Xunit;
namespace Microsoft.Extensions.Http.Resilience.Test.Hedging;
public sealed class StandardHedgingTests : HedgingTests<IStandardHedgingHandlerBuilder>
{
public StandardHedgingTests()
: base(ConfigureDefaultBuilder)
{
}
private static IStandardHedgingHandlerBuilder ConfigureDefaultBuilder(IHttpClientBuilder builder, Func<RequestRoutingStrategy> factory)
{
return builder
.AddStandardHedgingHandler(routing => routing.ConfigureRoutingStrategy(_ => factory))
.Configure(options =>
{
options.HedgingOptions.MaxHedgedAttempts = DefaultHedgingAttempts;
options.HedgingOptions.HedgingDelay = TimeSpan.FromMilliseconds(5);
});
}
[Fact]
public void EnsureValidated_BasicValidation()
{
Builder.Configure(options => options.HedgingOptions.MaxHedgedAttempts = -1);
Assert.Throws<OptionsValidationException>(() => CreateClientWithHandler());
}
[Fact]
public void EnsureValidated_AdvancedValidation()
{
Builder.Configure(options => options.TotalRequestTimeoutOptions.Timeout = TimeSpan.FromSeconds(1));
Assert.Throws<OptionsValidationException>(() => CreateClientWithHandler());
}
[Fact]
public void Configure_Callback_Ok()
{
Builder.Configure(o => o.HedgingOptions.MaxHedgedAttempts = 8);
var options = Builder.Services.BuildServiceProvider().GetRequiredService<IOptionsMonitor<HttpStandardHedgingResilienceOptions>>().Get(Builder.Name);
Assert.Equal(8, options.HedgingOptions.MaxHedgedAttempts);
}
[Fact]
public void Configure_CallbackWithServiceProvider_Ok()
{
Builder.Configure((o, serviceProvider) =>
{
serviceProvider.GetRequiredService<ResilienceStrategyProvider<HttpKey>>().Should().NotBeNull();
o.HedgingOptions.MaxHedgedAttempts = 8;
});
var options = Builder.Services.BuildServiceProvider().GetRequiredService<IOptionsMonitor<HttpStandardHedgingResilienceOptions>>().Get(Builder.Name);
Assert.Equal(8, options.HedgingOptions.MaxHedgedAttempts);
}
[Fact]
public void RoutingStrategyBuilder_EnsureExpectedName()
{
Assert.Equal(ClientId, Builder.RoutingStrategyBuilder.Name);
}
[Fact]
public void Configure_ValidConfigurationSection_ShouldInitialize()
{
var section = ConfigurationStubFactory.Create(new Dictionary<string, string?>
{
{ "dummy:HedgingOptions:MaxHedgedAttempts", "8" }
}).GetSection("dummy");
Builder.Configure(section);
var options = Builder.Services.BuildServiceProvider().GetRequiredService<IOptionsMonitor<HttpStandardHedgingResilienceOptions>>().Get(Builder.Name);
Assert.Equal(8, options.HedgingOptions.MaxHedgedAttempts);
}
[Fact]
public void ActionGenerator_Ok()
{
var options = Builder.Services.BuildServiceProvider().GetRequiredService<IOptionsMonitor<HttpStandardHedgingResilienceOptions>>().Get(Builder.Name);
var generator = options.HedgingOptions.HedgingActionGenerator;
var primary = ResilienceContext.Get();
var secondary = ResilienceContext.Get();
using var response = new HttpResponseMessage(HttpStatusCode.OK);
var args = new HedgingActionGeneratorArguments<HttpResponseMessage>(primary, secondary, 0, _ => Outcome.FromResultAsTask(response));
generator.Invoking(g => g(args)).Should().Throw<InvalidOperationException>().WithMessage("Request message snapshot is not attached to the resilience context.");
using var request = new HttpRequestMessage();
using var snapshot = RequestMessageSnapshot.Create(request);
primary.Properties.Set(ResilienceKeys.RequestSnapshot, snapshot);
generator.Invoking(g => g(args)).Should().NotThrow();
}
#if NET8_0_OR_GREATER
// Whilst these API are marked as NET6_0_OR_GREATER we don't build .NET 6.0,
// and as such the API is available in .NET 8 onwards.
[Fact]
public void Configure_InvalidConfigurationSection_ShouldThrow()
{
var section = ConfigurationStubFactory.Create(new Dictionary<string, string?>
{
{ "dummy:HedgingOptionsTypo:MaxHedgedAttempts", "8" },
{ "dummy:TotalRequestTimeoutOptions:TimeoutInterval", "00:00:20" },
}).GetSection("dummy");
Builder.Configure(section);
Assert.Throws<InvalidOperationException>(() =>
Builder.Services.BuildServiceProvider()
.GetRequiredService<IOptionsMonitor<HttpStandardHedgingResilienceOptions>>()
.Get(Builder.Name));
}
#endif
[Fact]
public void Configure_EmptyConfigurationSectionContent_ShouldThrow()
{
var section = ConfigurationStubFactory.Create(new Dictionary<string, string?>
{
{ "dummy", "" }
}).GetSection("dummy");
Assert.Throws<ArgumentNullException>(() =>
Builder.Configure(section));
}
[Fact]
public void Configure_EmptyConfigurationSection_ShouldThrow()
{
var section = ConfigurationStubFactory.CreateEmpty().GetSection(string.Empty);
Assert.Throws<ArgumentNullException>(() =>
Builder.Configure(section));
}
[Fact]
public async Task VerifyPipeline()
{
using var request = new HttpRequestMessage(HttpMethod.Get, "https://to-be-replaced:1234/some-path?query");
var strategies = new List<IList<ResilienceStrategy>>();
Builder.Services.RemoveAll<ResilienceStrategyBuilder>();
Builder.Services.AddTransient(_ =>
{
return new ResilienceStrategyBuilder
{
OnCreatingStrategy = list => strategies.Add(list)
};
});
Builder.SelectStrategyByAuthority(SimpleClassifications.PublicData);
SetupRouting();
SetupRoutes(1);
AddResponse(HttpStatusCode.OK);
using var client = CreateClientWithHandler();
await client.SendAsync(request, CancellationToken.None);
// primary handler
strategies.Should().HaveCount(2);
strategies[0].Should().HaveCount(4);
strategies[0][0].GetType().Name.Should().Contain("Routing");
strategies[0][1].GetType().Name.Should().Contain("Snapshot");
strategies[0][2].GetType().Name.Should().Contain("Timeout");
strategies[0][3].GetType().Name.Should().Contain("Hedging");
// inner handler
strategies[1].Should().HaveCount(3);
strategies[1][0].GetType().Name.Should().Contain("RateLimiter");
strategies[1][1].GetType().Name.Should().Contain("CircuitBreaker");
strategies[1][2].GetType().Name.Should().Contain("Timeout");
}
[InlineData(null)]
[InlineData("custom-key")]
[Theory]
public async Task VerifyPipelineSelection(string? customKey)
{
var noPolicy = NullResilienceStrategy<HttpResponseMessage>.Instance;
var provider = new Mock<ResilienceStrategyProvider<HttpKey>>(MockBehavior.Strict);
Builder.Services.AddSingleton(provider.Object);
if (customKey == null)
{
Builder.SelectStrategyByAuthority(SimpleClassifications.PublicData);
}
else
{
Builder.SelectStrategyBy(_ => _ => customKey);
}
customKey ??= "https://key:80";
provider.Setup(v => v.GetStrategy<HttpResponseMessage>(new HttpKey("clientId-standard-hedging", string.Empty))).Returns(noPolicy);
provider.Setup(v => v.GetStrategy<HttpResponseMessage>(new HttpKey("clientId-standard-hedging-endpoint", customKey))).Returns(noPolicy);
using var client = CreateClientWithHandler();
using var request = new HttpRequestMessage(HttpMethod.Get, "https://key:80/discarded");
AddResponse(HttpStatusCode.OK);
var response = await client.SendAsync(request, CancellationToken.None);
provider.VerifyAll();
}
[Fact]
public async Task DynamicReloads_Ok()
{
// arrange
var requests = new List<HttpRequestMessage>();
var config = ConfigurationStubFactory.Create(
new()
{
{ "standard:HedgingOptions:MaxHedgedAttempts", "3" }
},
out var reloadAction).GetSection("standard");
Builder.Configure(config).Configure(options => options.HedgingOptions.HedgingDelay = Timeout.InfiniteTimeSpan);
SetupRouting();
SetupRoutes(10);
var client = CreateClientWithHandler();
// act && assert
AddResponse(HttpStatusCode.InternalServerError, 3);
using var firstRequest = new HttpRequestMessage(HttpMethod.Get, "https://to-be-replaced:1234/some-path?query");
await client.SendAsync(firstRequest);
AssertNoResponse();
reloadAction(new() { { "standard:HedgingOptions:MaxHedgedAttempts", "7" } });
AddResponse(HttpStatusCode.InternalServerError, 7);
using var secondRequest = new HttpRequestMessage(HttpMethod.Get, "https://to-be-replaced:1234/some-path?query");
await client.SendAsync(secondRequest);
AssertNoResponse();
}
[Fact]
public async Task NoRouting_Ok()
{
// arrange
Builder.Services.Configure<RequestRoutingOptions>(Builder.RoutingStrategyBuilder.Name, options => options.RoutingStrategyProvider = null);
var client = CreateClientWithHandler();
// act && assert
AddResponse(HttpStatusCode.InternalServerError, 3);
using var firstRequest = new HttpRequestMessage(HttpMethod.Get, "https://some-endpoint:1234/some-path?query");
await client.SendAsync(firstRequest);
AssertNoResponse();
Requests.Should().AllSatisfy(r => r.Should().Be("https://some-endpoint:1234/some-path?query"));
}
protected override void ConfigureHedgingOptions(Action<HttpHedgingStrategyOptions> configure) => Builder.Configure(options => configure(options.HedgingOptions));
}