Skip to content

Commit

Permalink
Fixes WithSyncCircuitBreaker
Browse files Browse the repository at this point in the history
  • Loading branch information
ismaelhamed committed Dec 27, 2022
1 parent 692ff7f commit 052c442
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 32 deletions.
33 changes: 32 additions & 1 deletion src/core/Akka.TestKit/TestKitBase_Within.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,37 @@ namespace Akka.TestKit
/// </summary>
public abstract partial class TestKitBase
{
public void Within(TimeSpan max, Action action) => Within(TimeSpan.Zero, max, action);

public void Within(TimeSpan min, TimeSpan max, Action action)
{
max = Dilated(max);
var start = Now;
var rem = _testState.End.HasValue ? _testState.End.Value - start : Timeout.InfiniteTimeSpan;
_assertions.AssertTrue(rem.IsInfiniteTimeout() || rem >= min, "Required min time {0} not possible, only {1} left.", min, rem);

_testState.LastWasNoMsg = false;

var maxDiff = max.Min(rem);
var prevEnd = _testState.End;
_testState.End = start + maxDiff;

try
{
action();
}
finally
{
_testState.End = prevEnd;
}

var diff = Now - start;
_assertions.AssertTrue(min <= diff, "Block took {0}, should at least have been {1}", min, rem);

if (!_testState.LastWasNoMsg)
_assertions.AssertTrue(diff <= maxDiff, "Block took {0}, exceeding {1}", diff, maxDiff);
}

/// <summary>
/// Execute code block while bounding its execution time between 0 seconds and <paramref name="max"/>.
/// <para>`within` blocks may be nested. All methods in this class which take maximum wait times
Expand Down Expand Up @@ -49,7 +80,7 @@ public void Within(
cancellationToken: cancellationToken)
.ConfigureAwait(false).GetAwaiter().GetResult();
}

/// <summary>
/// Async version of <see cref="Within(TimeSpan, Action, TimeSpan?, CancellationToken)"/>
/// that takes a <see cref="Func{Task}"/> instead of an <see cref="Action"/>
Expand Down
40 changes: 9 additions & 31 deletions src/core/Akka/Pattern/CircuitBreaker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -240,41 +240,19 @@ public Task WithCircuitBreaker<TState>(TState state, Func<TState, Task> body) =>
CurrentState.InvokeState(state, body);

/// <summary>
/// The failure will be recorded farther down.
/// Wraps invocations of asynchronous calls that need to be protected.
/// </summary>
/// <param name="body">TBD</param>
public void WithSyncCircuitBreaker(Action body)
{
var cbTask = WithCircuitBreaker(body,(b) => Task.Factory.StartNew(b));
if (!cbTask.Wait(CallTimeout))
{
//throw new TimeoutException( string.Format( "Execution did not complete within the time allotted {0} ms", CallTimeout.TotalMilliseconds ) );
}
if (cbTask.Exception != null)
{
ExceptionDispatchInfo.Capture(cbTask.Exception).Throw();
}
}
/// <param name="body">Call needing protected</param>
public void WithSyncCircuitBreaker(Action body) =>
WithCircuitBreaker(body, b => Task.Run(b)).GetAwaiter().GetResult();

/// <summary>
/// Wraps invocations of asynchronous calls that need to be protected
/// If this does not complete within the time allotted, it should return default(<typeparamref name="T"/>)
///
/// <code>
/// Await.result(
/// withCircuitBreaker(try Future.successful(body) catch { case NonFatal(t) ⇒ Future.failed(t) }),
/// callTimeout)
/// </code>
///
/// Wraps invocations of asynchronous calls that need to be protected.
/// </summary>
/// <typeparam name="T">TBD</typeparam>
/// <param name="body">TBD</param>
/// <returns><typeparamref name="T"/> or default(<typeparamref name="T"/>)</returns>
public T WithSyncCircuitBreaker<T>(Func<T> body)
{
var cbTask = WithCircuitBreaker(body,(b) => Task.Factory.StartNew(b));
return cbTask.Wait(CallTimeout) ? cbTask.Result : default(T);
}
/// <param name="body">Call needing protected</param>
/// <returns>The result of the call</returns>
public T WithSyncCircuitBreaker<T>(Func<T> body) =>
WithCircuitBreaker(body, b => Task.Run(b)).Result;

/// <summary>
/// Mark a successful call through CircuitBreaker. Sometimes the callee of CircuitBreaker sends back a message to the
Expand Down

0 comments on commit 052c442

Please sign in to comment.