Skip to content

Commit

Permalink
Add binary serialization support for circuit breaker exception RetryA…
Browse files Browse the repository at this point in the history
…fter property.
  • Loading branch information
DL444 committed Oct 5, 2024
1 parent cd57a4e commit 9a93845
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 4 deletions.
26 changes: 26 additions & 0 deletions src/Polly.Core/CircuitBreaker/BrokenCircuitException.cs
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,32 @@ public BrokenCircuitException(string message, TimeSpan retryAfter, Exception inn
protected BrokenCircuitException(SerializationInfo info, StreamingContext context)
: base(info, context)
{
Guard.NotNull(info);

// https://github.com/dotnet/runtime/issues/42460
SerializationInfoEnumerator enumerator = info.GetEnumerator();
while (enumerator.MoveNext())
{
SerializationEntry entry = enumerator.Current;
if (string.Equals(entry.Name, "RetryAfter", StringComparison.Ordinal))
{
var ticks = (long)entry.Value;
RetryAfter = new TimeSpan(ticks);
break;
}
}
}

/// <inheritdoc/>
public override void GetObjectData(SerializationInfo info, StreamingContext context)
{
Guard.NotNull(info);
if (RetryAfter.HasValue)
{
info.AddValue("RetryAfter", RetryAfter.Value.Ticks);
}

base.GetObjectData(info, context);
}
#endif
#pragma warning restore RS0016 // Add public types and members to the declared API
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,26 @@ public void Ctor_Message_RetryAfter_InnerException_Ok()

#if !NETCOREAPP
[Fact]
public void BinarySerialization_Ok() =>
BinarySerializationUtil.SerializeAndDeserializeException(new BrokenCircuitException()).Should().NotBeNull();
public void BinarySerialization_NonNullRetryAfter_Ok()
{
var exception = new BrokenCircuitException(TestMessage, TestRetryAfter, new InvalidOperationException());
BrokenCircuitException roundtripResult = BinarySerializationUtil.SerializeAndDeserializeException(exception);
roundtripResult.Should().NotBeNull();
roundtripResult.Message.Should().Be(TestMessage);
roundtripResult.InnerException.Should().BeOfType<InvalidOperationException>();
roundtripResult.RetryAfter.Should().Be(TestRetryAfter);
}

[Fact]
public void BinarySerialization_NullRetryAfter_Ok()
{
var exception = new BrokenCircuitException(TestMessage, new InvalidOperationException());
BrokenCircuitException roundtripResult = BinarySerializationUtil.SerializeAndDeserializeException(exception);
roundtripResult.Should().NotBeNull();
roundtripResult.Message.Should().Be(TestMessage);
roundtripResult.InnerException.Should().BeOfType<InvalidOperationException>();
roundtripResult.RetryAfter.Should().BeNull();
}
#endif

private const string TestMessage = "Dummy.";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,15 @@ public void Ctor_Message_InnerException_Ok()

#if !NETCOREAPP
[Fact]
public void BinarySerialization_Ok() =>
BinarySerializationUtil.SerializeAndDeserializeException(new IsolatedCircuitException("dummy")).Should().NotBeNull();
public void BinarySerialization_Ok()
{
var exception = new IsolatedCircuitException(TestMessage, new InvalidOperationException());
IsolatedCircuitException roundtripResult = BinarySerializationUtil.SerializeAndDeserializeException(exception);
roundtripResult.Should().NotBeNull();
roundtripResult.Message.Should().Be(TestMessage);
roundtripResult.InnerException.Should().BeOfType<InvalidOperationException>();
roundtripResult.RetryAfter.Should().BeNull();
}
#endif

private const string TestMessage = "Dummy.";
Expand Down

0 comments on commit 9a93845

Please sign in to comment.