Skip to content

Commit 99be70b

Browse files
authored
Consolidate intercept (#5033)
* Removed a bunch of obsolete and unused methods * Consolidate Intercept methods, and add the new AssertThrows * Fixed a few tests that where dealing with the AggregateExceptions themselves
1 parent 61d08b7 commit 99be70b

File tree

4 files changed

+73
-50
lines changed

4 files changed

+73
-50
lines changed

src/core/Akka.Remote.Tests/Transport/AkkaProtocolSpec.cs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -423,11 +423,7 @@ public void ProtocolStateActor_must_give_up_outbound_after_connection_timeout()
423423

424424
Watch(stateActor);
425425

426-
// inner exception will be a TimeoutException
427-
Intercept<AggregateException>(() =>
428-
{
429-
statusPromise.Task.Wait(TimeSpan.FromSeconds(5)).Should().BeTrue();
430-
});
426+
Intercept<TimeoutException>(() => statusPromise.Task.Wait(TimeSpan.FromSeconds(5)).Should().BeTrue());
431427
ExpectTerminated(stateActor);
432428
}
433429

src/core/Akka.Streams.Tests/Dsl/FlowAskSpec.cs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -266,13 +266,11 @@ public void Flow_with_ask_signal_failure_when_target_actor_is_terminated() => th
266266
.Ask<Reply>(r, _timeout, 4)
267267
.RunWith(Sink.Ignore<Reply>(), _materializer);
268268

269-
Intercept<AggregateException>(() =>
269+
Intercept<WatchedActorTerminatedException>(() =>
270270
{
271271
r.Tell(PoisonPill.Instance);
272272
done.Wait(RemainingOrDefault);
273-
})
274-
.Flatten()
275-
.InnerException.Should().BeOfType<WatchedActorTerminatedException>();
273+
});
276274

277275
}, _materializer);
278276

src/core/Akka.Tests.Shared.Internals/AkkaSpec.cs

Lines changed: 69 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ private static string GetCallerName()
113113
}
114114

115115
public static Config AkkaSpecConfig { get { return _akkaSpecConfig; } }
116-
116+
117117
protected T ExpectMsgPf<T>(TimeSpan? timeout, string hint, Func<object, T> function)
118118
{
119119
MessageEnvelope envelope;
@@ -136,30 +136,88 @@ protected T ExpectMsgPf<T>(string hint, Func<object, T> pf)
136136
return pf.Invoke(t);
137137
}
138138

139-
139+
/// <summary>
140+
/// Intercept and return an exception that's expected to be thrown by the passed function value. The thrown
141+
/// exception must be an instance of the type specified by the type parameter of this method. This method
142+
/// invokes the passed function. If the function throws an exception that's an instance of the specified type,
143+
/// this method returns that exception. Else, whether the passed function returns normally or completes abruptly
144+
/// with a different exception, this method throws <see cref="ThrowsException"/>.
145+
/// <para>
146+
/// Also note that the difference between this method and <seealso cref="AssertThrows{T}"/> is that this method
147+
/// returns the expected exception, so it lets you perform further assertions on that exception. By contrast,
148+
/// the <seealso cref="AssertThrows{T}"/> indicates to the reader of the code that nothing further is expected
149+
/// about the thrown exception other than its type. The recommended usage is to use <seealso cref="AssertThrows{T}"/>
150+
/// by default, intercept only when you need to inspect the caught exception further.
151+
/// </para>
152+
/// </summary>
153+
/// <param name="actionThatThrows">The action that should throw the expected exception</param>
154+
/// <returns>The intercepted exception, if it is of the expected type</returns>
155+
/// <exception cref="ThrowsException">If the passed action does not complete abruptly with an exception that's an instance of the specified type.</exception>
140156
protected T Intercept<T>(Action actionThatThrows) where T : Exception
141157
{
142-
return Assert.Throws<T>(() => actionThatThrows());
143-
}
158+
try
159+
{
160+
actionThatThrows();
161+
}
162+
catch (Exception ex)
163+
{
164+
var exception = ex is AggregateException aggregateException
165+
? aggregateException.Flatten().InnerExceptions[0]
166+
: ex;
167+
168+
var exceptionType = typeof(T);
169+
return exceptionType == exception.GetType()
170+
? (T)exception
171+
: throw new ThrowsException(exceptionType, exception);
172+
}
144173

145-
protected void Intercept(Action actionThatThrows)
174+
throw new ThrowsException(typeof(T));
175+
}
176+
177+
/// <summary>
178+
/// Ensure that an expected exception is thrown by the passed function value. The thrown exception must be an
179+
/// instance of the type specified by the type parameter of this method. This method invokes the passed
180+
/// function. If the function throws an exception that's an instance of the specified type, this method returns
181+
/// void. Else, whether the passed function returns normally or completes abruptly with a different
182+
/// exception, this method throws <see cref="ThrowsException"/>.
183+
/// <para>
184+
/// Also note that the difference between this method and <seealso cref="Intercept{T}"/> is that this method
185+
/// does not return the expected exception, so it does not let you perform further assertions on that exception.
186+
/// It also indicates to the reader of the code that nothing further is expected about the thrown exception
187+
/// other than its type. The recommended usage is to use <see cref="AssertThrows{T}"/> by default,
188+
/// <seealso cref="Intercept{T}"/> only when you need to inspect the caught exception further.
189+
/// </para>
190+
/// </summary>
191+
/// <param name="actionThatThrows">The action that should throw the expected exception</param>
192+
/// <exception cref="ThrowsException">If the passed action does not complete abruptly with an exception that's an instance of the specified type.</exception>
193+
protected void AssertThrows<T>(Action actionThatThrows) where T : Exception
146194
{
147195
try
148196
{
149197
actionThatThrows();
150198
}
151-
catch(Exception)
199+
catch (Exception ex)
152200
{
153-
return;
201+
var exception = ex is AggregateException aggregateException
202+
? aggregateException.Flatten().InnerExceptions[0]
203+
: ex;
204+
205+
var exceptionType = typeof(T);
206+
if (exceptionType == exception.GetType())
207+
return;
208+
209+
throw new ThrowsException(exceptionType, exception);
154210
}
155-
throw new ThrowsException(typeof(Exception));
211+
212+
throw new ThrowsException(typeof(T));
156213
}
157-
158-
protected async Task InterceptAsync(Func<Task> asyncActionThatThrows)
214+
215+
[Obsolete("Use AssertThrows instead.")]
216+
protected void Intercept(Action actionThatThrows)
159217
{
160218
try
161219
{
162-
await asyncActionThatThrows();
220+
actionThatThrows();
163221
}
164222
catch(Exception)
165223
{
@@ -168,24 +226,6 @@ protected async Task InterceptAsync(Func<Task> asyncActionThatThrows)
168226
throw new ThrowsException(typeof(Exception));
169227
}
170228

171-
[Obsolete("Use Intercept instead. This member will be removed.")]
172-
protected void intercept<T>(Action actionThatThrows) where T : Exception
173-
{
174-
Assert.Throws<T>(() => actionThatThrows());
175-
}
176-
177-
[Obsolete("Use ExpectMsgPf instead. This member will be removed.")]
178-
protected T expectMsgPF<T>(string hint, Func<object, T> pf)
179-
{
180-
return ExpectMsgPf<T>(hint, pf);
181-
}
182-
183-
[Obsolete("Use ExpectMsgPf instead. This member will be removed.")]
184-
protected T expectMsgPF<T>(TimeSpan duration, string hint, Func<object, T> pf)
185-
{
186-
return ExpectMsgPf<T>(duration, hint, pf);
187-
}
188-
189229
protected void MuteDeadLetters(params Type[] messageClasses)
190230
{
191231
if (!Sys.Log.IsDebugEnabled)

src/core/Akka.Tests/Actor/CoordinatedShutdownSpec.cs

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -337,18 +337,7 @@ public void CoordinatedShutdown_must_abort_if_recover_is_off()
337337

338338
var result = co.Run(CoordinatedShutdown.UnknownReason.Instance);
339339
ExpectMsg("B");
340-
Intercept<AggregateException>(() =>
341-
{
342-
if (result.Wait(RemainingOrDefault))
343-
{
344-
result.Exception?.Flatten().InnerException.Should().BeOfType<TimeoutException>();
345-
}
346-
else
347-
{
348-
throw new Exception("CoordinatedShutdown task did not complete");
349-
}
350-
});
351-
340+
Intercept<TimeoutException>(() => result.Wait(RemainingOrDefault));
352341
ExpectNoMsg(TimeSpan.FromMilliseconds(200)); // C not run
353342
}
354343

0 commit comments

Comments
 (0)