Skip to content
Merged
Show file tree
Hide file tree
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
30 changes: 30 additions & 0 deletions IntervalAction.Test/IntervalActionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@
{
Interlocked.Increment(ref executions);
// Simulate a long running task.
Thread.Sleep(500);

Check warning on line 55 in IntervalAction.Test/IntervalActionTests.cs

View workflow job for this annotation

GitHub Actions / ci / .NET / Analyze & Release

Do not use 'Thread.Sleep()' in a test.

Check warning on line 55 in IntervalAction.Test/IntervalActionTests.cs

View workflow job for this annotation

GitHub Actions / ci / .NET / Analyze & Release

Do not use 'Thread.Sleep()' in a test.
},
IntervalType = IntervalType.FromLastStart
};
Expand Down Expand Up @@ -500,6 +500,36 @@
StringAssert.Contains(exception.StackTrace, nameof(ThrowFromNamedMethod));
}

[TestMethod]
public async Task RethrowExceptionsKeepsTheSameStackTraceOnEveryCall()
{
// Arrange
IntervalActionOptions options = new()
{
PollingInterval = TimeSpan.FromMilliseconds(10),
ActionInterval = TimeSpan.Zero,
Action = ThrowFromNamedMethod,
IntervalType = IntervalType.FromLastStart
};
IntervalAction intervalAction = IntervalAction.Start(options);
await WaitForPollingToFaultAsync(intervalAction).ConfigureAwait(false);

// Act
List<string?> traces = [];
for (int i = 0; i < 100; i++)
{
traces.Add(Assert.ThrowsExactly<InvalidOperationException>(intervalAction.RethrowExceptions).StackTrace);
}

intervalAction.Stop();

// Assert: each rethrow used to capture the trace the previous one had left on the same
// exception, adding a section per call
int firstLength = traces[0]?.Length ?? 0;
Assert.IsTrue(traces.TrueForAll(trace => trace?.Length == firstLength), $"Trace lengths ranged from {firstLength} to {traces.Max(trace => trace?.Length)}.");
StringAssert.Contains(traces[^1], nameof(ThrowFromNamedMethod));

Check warning on line 530 in IntervalAction.Test/IntervalActionTests.cs

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Use 'Assert.Contains' instead of 'StringAssert.Contains'

See more on https://sonarcloud.io/project/issues?id=ktsu-dev_IntervalAction&issues=AaHAok6epQTXMdC1zyx0&open=AaHAok6epQTXMdC1zyx0&pullRequest=106
}

[TestMethod]
public async Task AsyncActionRunsNeverOverlap()
{
Expand Down Expand Up @@ -1215,7 +1245,7 @@
Action = () =>
{
runTimes.Enqueue(stopwatch.ElapsedMilliseconds);
Thread.Sleep(workMilliseconds);

Check warning on line 1248 in IntervalAction.Test/IntervalActionTests.cs

View workflow job for this annotation

GitHub Actions / ci / .NET / Analyze & Release

Do not use 'Thread.Sleep()' in a test.

Check warning on line 1248 in IntervalAction.Test/IntervalActionTests.cs

View workflow job for this annotation

GitHub Actions / ci / .NET / Analyze & Release

Do not use 'Thread.Sleep()' in a test.
},
});

Expand Down
36 changes: 34 additions & 2 deletions IntervalAction/IntervalAction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,17 @@ public sealed class IntervalAction : IDisposable

private Lock Lock { get; } = new();

/// <summary>
/// Gets or sets the capture of the failure most recently rethrown.
/// </summary>
/// <remarks>
/// <see cref="ExceptionDispatchInfo.Capture"/> snapshots the exception's trace as it stands, and
/// a stored failure is the same object on every rethrow, so capturing it afresh each time would
/// fold every earlier rethrow into the snapshot and grow the trace by a section per call. Reusing
/// one capture per failure keeps the trace the same on every call.
/// </remarks>
private ExceptionDispatchInfo? RethrownFailure { get; set; }

/// <summary>
/// Gets or sets the most recent restart, which the next restart waits on before it begins.
/// </summary>
Expand Down Expand Up @@ -582,7 +593,7 @@ private bool TryRun(long stopGeneration)
if (GetActionFailure(ActionTask) is { } failure)
{
// Rethrow through ExceptionDispatchInfo so the trace still shows where the action failed
ExceptionDispatchInfo.Capture(failure).Throw();
Rethrow(failure);
}

ActionTask = null;
Expand Down Expand Up @@ -822,7 +833,28 @@ public void RethrowExceptions()

if (exception is not null)
{
ExceptionDispatchInfo.Capture(exception).Throw();
Rethrow(exception);
}
}

/// <summary>
/// Rethrows a failure with the trace from where the action threw, through one
/// <see cref="ExceptionDispatchInfo"/> per failure, so repeated rethrows don't grow the trace.
/// </summary>
/// <param name="failure">The exception to rethrow.</param>
private void Rethrow(Exception failure)
{
ExceptionDispatchInfo captured;
lock (Lock)
{
if (RethrownFailure is not { } previous || !ReferenceEquals(previous.SourceException, failure))
{
RethrownFailure = ExceptionDispatchInfo.Capture(failure);
}

captured = RethrownFailure;
}

captured.Throw();
}
}
Loading