Skip to content

Commit

Permalink
New tests
Browse files Browse the repository at this point in the history
  • Loading branch information
martintmk committed Jun 26, 2023
1 parent b41ea69 commit 5caab39
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ public static IStandardHedgingHandlerBuilder AddStandardHedgingHandler(this IHtt
context.EnableReloads<HttpStandardHedgingResilienceOptions>(optionsName);

_ = builder
.AddStrategy(new RoutingStrategy(context.ServiceProvider.GetRoutingFactory(routingBuilder.Name)))
.AddStrategy(new RoutingResilienceStrategy(context.ServiceProvider.GetRoutingFactory(routingBuilder.Name)))
.AddStrategy(new RequestMessageSnapshotStrategy(context.ServiceProvider.GetRequiredService<IRequestCloner>()))
.AddTimeout(options.TotalRequestTimeoutOptions)
.AddHedging(options.HedgingOptions);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ namespace Microsoft.Extensions.Http.Resilience.Routing.Internal;
/// <summary>
/// Adds routing support to an inner strategy.
/// </summary>
internal sealed class RoutingStrategy : ResilienceStrategy
internal sealed class RoutingResilienceStrategy : ResilienceStrategy
{
private readonly IRequestRoutingStrategyFactory _factory;

public RoutingStrategy(IRequestRoutingStrategyFactory factory)
public RoutingResilienceStrategy(IRequestRoutingStrategyFactory factory)
{
_factory = factory;
}
Expand All @@ -26,6 +26,11 @@ protected override async ValueTask<Outcome<TResult>> ExecuteCoreAsync<TResult, T
ResilienceContext context,
TState state)
{
if (!context.Properties.TryGetValue(ResilienceKeys.RequestMessage, out var request))
{
Throw.InvalidOperationException("The HTTP request message was not found in the resilience context.");
}

var strategy = _factory.CreateRoutingStrategy();

// if there are not routes we cannot continue
Expand All @@ -34,11 +39,6 @@ protected override async ValueTask<Outcome<TResult>> ExecuteCoreAsync<TResult, T
Throw.InvalidOperationException("The routing strategy did not provide any route URL on the first attempt.");
}

if (!context.Properties.TryGetValue(ResilienceKeys.RequestMessage, out var request))
{
Throw.InvalidOperationException("The HTTP request message was not found in the resilience context.");
}

context.Properties.Set(ResilienceKeys.RoutingStrategy, strategy);

// for primary request, use retrieved route
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public void Validate_ValidOptionsWithoutRouting_ValidationErrors()

var validationResult = CreateValidator("dummy").Validate("other", options);

validationResult.Failed.Should().BeFalse();
validationResult.Failed.Should().BeTrue();
validationResult.FailureMessage.Should().Be("The hedging routing is not configured for 'other' HTTP client.");
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// 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 FluentAssertions;
using Microsoft.Extensions.Http.Resilience.Routing.Internal;
using Moq;
using Xunit;

namespace Microsoft.Extensions.Http.Resilience.Test.Routing;

public class RoutingResilienceStrategyTests
{
[Fact]
public void NoRequestMessage_Throws()
{
RoutingResilienceStrategy strategy = new RoutingResilienceStrategy(Mock.Of<IRequestRoutingStrategyFactory>());

strategy.Invoking(s => s.Execute(() => { })).Should().Throw<InvalidOperationException>().WithMessage("The HTTP request message was not found in the resilience context.");
}
}

0 comments on commit 5caab39

Please sign in to comment.