elsa-core/test/unit/Elsa.Resilience.Core.UnitTests/ResilienceStrategySerializerTests.cs
Sipke Schoorstra cf23279bf1
test(resilience): cover Elsa.Resilience.Core and lift its coverage gate off the Debug/Release seam (#7971)
`dotnet test test/unit/Elsa.Resilience.Core.UnitTests` exited 1 on a clean
checkout with all 56 tests passing. The failure was the coverlet gate, not a
test: the project pinned `<Threshold>49</Threshold>` against 48.17% measured
line coverage in Debug. Release measures slightly differently and cleared it,
so CI (which builds `--configuration Release`) stayed green while every local
run — Debug is the default — went red. A red exit for a suite that passes
trains people to ignore exit codes.

Rather than move the goalposts, cover the code. The gap was concentrated in
`ResilientActivityInvoker`, which had no tests at all, plus the serializer,
the activity-execution extensions and the retry telemetry listener.
`Elsa.Testing.Shared`'s `ActivityTestFixture` was already referenced here and
builds a real `ActivityExecutionContext`, which is what all of them needed.

Adds 40 tests. The invoker ones drive a real zero-delay Polly retry pipeline,
so the telemetry listener is exercised through the actual Polly path rather
than being called directly: pass-through when no strategy is configured, the
applied strategy recorded on the context, retry-then-succeed, one record per
retry carrying identifiers and details, null details dropped, the retries flag
and attempt count, exhausted retries rethrowing, and an unhandled exception
type not being retried. The extensions tests build a three-level context chain
to pin down that the retries flag propagates up the ancestor chain and not
down.

Line coverage goes 48.17% -> 98.17% in Debug and 97.8% in Release; the five
lines still uncovered are defensive early-returns. The threshold moves to 90,
below the lower of the two configurations with enough headroom that the
Debug/Release delta cannot straddle it again. Verified by deleting the invoker
tests once: coverage falls to 68.97% and the gate fails as it should.

Co-authored-by: Claude Opus 5 <noreply@anthropic.com>
2026-08-21 03:01:51 +02:00

118 lines
4.4 KiB
C#

using System.Text.Json;
using Elsa.Resilience.Core.UnitTests.TestHelpers;
using Elsa.Resilience.Options;
using Elsa.Resilience.Serialization;
namespace Elsa.Resilience.Core.UnitTests;
public class ResilienceStrategySerializerTests
{
private readonly ResilienceStrategySerializer _serializer = CreateSerializer(typeof(TestRetryStrategy), typeof(TestNoopStrategy));
private static ResilienceStrategySerializer CreateSerializer(params Type[] strategyTypes)
{
var options = Microsoft.Extensions.Options.Options.Create(new ResilienceOptions
{
StrategyTypes = strategyTypes.ToList()
});
return new(options);
}
[Fact(DisplayName = "Serializer should write a type discriminator named after the strategy type")]
public void Serialize_RegisteredStrategy_WritesTypeDiscriminator()
{
var json = _serializer.Serialize(new TestRetryStrategy());
using var document = JsonDocument.Parse(json);
Assert.Equal(nameof(TestRetryStrategy), document.RootElement.GetProperty("$type").GetString());
}
[Fact(DisplayName = "Serializer should write property names in camel case")]
public void Serialize_RegisteredStrategy_UsesCamelCasePropertyNames()
{
var json = _serializer.Serialize(new TestRetryStrategy
{
Id = "my-strategy",
MaxRetryAttempts = 7
});
using var document = JsonDocument.Parse(json);
Assert.Equal("my-strategy", document.RootElement.GetProperty("id").GetString());
Assert.Equal(7, document.RootElement.GetProperty("maxRetryAttempts").GetInt32());
}
[Fact(DisplayName = "Serializer should write enums as strings")]
public void Serialize_StrategyWithEnum_WritesEnumAsString()
{
var json = _serializer.Serialize(new TestNoopStrategy
{
Flavor = TestStrategyFlavor.Fancy
});
using var document = JsonDocument.Parse(json);
Assert.Equal(nameof(TestStrategyFlavor.Fancy), document.RootElement.GetProperty("flavor").GetString());
}
[Fact(DisplayName = "Serializer should round-trip a strategy back into its concrete type")]
public void Deserialize_SerializedStrategy_ReturnsConcreteType()
{
var json = _serializer.Serialize(new TestRetryStrategy
{
Id = "round-trip",
DisplayName = "Round Trip",
MaxRetryAttempts = 4
});
var strategy = Assert.IsType<TestRetryStrategy>(_serializer.Deserialize(json));
Assert.Equal("round-trip", strategy.Id);
Assert.Equal("Round Trip", strategy.DisplayName);
Assert.Equal(4, strategy.MaxRetryAttempts);
}
[Fact(DisplayName = "Serializer should read property names case-insensitively")]
public void Deserialize_PascalCasePropertyNames_ReadsValues()
{
var json = $$"""{"$type":"{{nameof(TestRetryStrategy)}}","Id":"pascal","MaxRetryAttempts":3}""";
var strategy = Assert.IsType<TestRetryStrategy>(_serializer.Deserialize(json));
Assert.Equal("pascal", strategy.Id);
Assert.Equal(3, strategy.MaxRetryAttempts);
}
[Fact(DisplayName = "Serializer should read numbers written as strings")]
public void Deserialize_NumberAsString_ReadsNumber()
{
var json = $$"""{"$type":"{{nameof(TestRetryStrategy)}}","maxRetryAttempts":"5"}""";
var strategy = Assert.IsType<TestRetryStrategy>(_serializer.Deserialize(json));
Assert.Equal(5, strategy.MaxRetryAttempts);
}
[Fact(DisplayName = "Serializer should round-trip a heterogeneous list of strategies")]
public void SerializeMany_MixedStrategies_RoundTripsEachConcreteType()
{
var json = _serializer.SerializeMany([
new TestRetryStrategy { Id = "first" },
new TestNoopStrategy { Id = "second" }
]);
var strategies = _serializer.DeserializeMany(json).ToList();
Assert.Collection(strategies,
s => Assert.Equal("first", Assert.IsType<TestRetryStrategy>(s).Id),
s => Assert.Equal("second", Assert.IsType<TestNoopStrategy>(s).Id));
}
[Fact(DisplayName = "Serializer should reject strategy types that were not registered")]
public void Serialize_UnregisteredStrategy_Throws()
{
var serializer = CreateSerializer(typeof(TestNoopStrategy));
Assert.Throws<NotSupportedException>(() => serializer.Serialize(new TestRetryStrategy()));
}
}