Skip to content

Commit 052c442

Browse files
committed
Fixes WithSyncCircuitBreaker
1 parent 692ff7f commit 052c442

File tree

2 files changed

+41
-32
lines changed

2 files changed

+41
-32
lines changed

src/core/Akka.TestKit/TestKitBase_Within.cs

+32-1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,37 @@ namespace Akka.TestKit
1919
/// </summary>
2020
public abstract partial class TestKitBase
2121
{
22+
public void Within(TimeSpan max, Action action) => Within(TimeSpan.Zero, max, action);
23+
24+
public void Within(TimeSpan min, TimeSpan max, Action action)
25+
{
26+
max = Dilated(max);
27+
var start = Now;
28+
var rem = _testState.End.HasValue ? _testState.End.Value - start : Timeout.InfiniteTimeSpan;
29+
_assertions.AssertTrue(rem.IsInfiniteTimeout() || rem >= min, "Required min time {0} not possible, only {1} left.", min, rem);
30+
31+
_testState.LastWasNoMsg = false;
32+
33+
var maxDiff = max.Min(rem);
34+
var prevEnd = _testState.End;
35+
_testState.End = start + maxDiff;
36+
37+
try
38+
{
39+
action();
40+
}
41+
finally
42+
{
43+
_testState.End = prevEnd;
44+
}
45+
46+
var diff = Now - start;
47+
_assertions.AssertTrue(min <= diff, "Block took {0}, should at least have been {1}", min, rem);
48+
49+
if (!_testState.LastWasNoMsg)
50+
_assertions.AssertTrue(diff <= maxDiff, "Block took {0}, exceeding {1}", diff, maxDiff);
51+
}
52+
2253
/// <summary>
2354
/// Execute code block while bounding its execution time between 0 seconds and <paramref name="max"/>.
2455
/// <para>`within` blocks may be nested. All methods in this class which take maximum wait times
@@ -49,7 +80,7 @@ public void Within(
4980
cancellationToken: cancellationToken)
5081
.ConfigureAwait(false).GetAwaiter().GetResult();
5182
}
52-
83+
5384
/// <summary>
5485
/// Async version of <see cref="Within(TimeSpan, Action, TimeSpan?, CancellationToken)"/>
5586
/// that takes a <see cref="Func{Task}"/> instead of an <see cref="Action"/>

src/core/Akka/Pattern/CircuitBreaker.cs

+9-31
Original file line numberDiff line numberDiff line change
@@ -240,41 +240,19 @@ public Task WithCircuitBreaker<TState>(TState state, Func<TState, Task> body) =>
240240
CurrentState.InvokeState(state, body);
241241

242242
/// <summary>
243-
/// The failure will be recorded farther down.
243+
/// Wraps invocations of asynchronous calls that need to be protected.
244244
/// </summary>
245-
/// <param name="body">TBD</param>
246-
public void WithSyncCircuitBreaker(Action body)
247-
{
248-
var cbTask = WithCircuitBreaker(body,(b) => Task.Factory.StartNew(b));
249-
if (!cbTask.Wait(CallTimeout))
250-
{
251-
//throw new TimeoutException( string.Format( "Execution did not complete within the time allotted {0} ms", CallTimeout.TotalMilliseconds ) );
252-
}
253-
if (cbTask.Exception != null)
254-
{
255-
ExceptionDispatchInfo.Capture(cbTask.Exception).Throw();
256-
}
257-
}
245+
/// <param name="body">Call needing protected</param>
246+
public void WithSyncCircuitBreaker(Action body) =>
247+
WithCircuitBreaker(body, b => Task.Run(b)).GetAwaiter().GetResult();
258248

259249
/// <summary>
260-
/// Wraps invocations of asynchronous calls that need to be protected
261-
/// If this does not complete within the time allotted, it should return default(<typeparamref name="T"/>)
262-
///
263-
/// <code>
264-
/// Await.result(
265-
/// withCircuitBreaker(try Future.successful(body) catch { case NonFatal(t) ⇒ Future.failed(t) }),
266-
/// callTimeout)
267-
/// </code>
268-
///
250+
/// Wraps invocations of asynchronous calls that need to be protected.
269251
/// </summary>
270-
/// <typeparam name="T">TBD</typeparam>
271-
/// <param name="body">TBD</param>
272-
/// <returns><typeparamref name="T"/> or default(<typeparamref name="T"/>)</returns>
273-
public T WithSyncCircuitBreaker<T>(Func<T> body)
274-
{
275-
var cbTask = WithCircuitBreaker(body,(b) => Task.Factory.StartNew(b));
276-
return cbTask.Wait(CallTimeout) ? cbTask.Result : default(T);
277-
}
252+
/// <param name="body">Call needing protected</param>
253+
/// <returns>The result of the call</returns>
254+
public T WithSyncCircuitBreaker<T>(Func<T> body) =>
255+
WithCircuitBreaker(body, b => Task.Run(b)).Result;
278256

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

0 commit comments

Comments
 (0)