Skip to content

Commit 0cd2522

Browse files
committed
test: remove blocking waits from tests
1 parent 458df11 commit 0cd2522

41 files changed

Lines changed: 329 additions & 237 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

tests/Paramore.Brighter.AWS.Tests/MessagingGateway/Sqs/Fifo/Reactor/When_throwing_defer_action_respect_redrive.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ public async Task When_throwing_defer_action_respect_redrive_async()
136136
var quitMessage = MessageFactory.CreateQuitMessage(_subscription.RoutingKey);
137137
_channel.Enqueue(quitMessage);
138138

139-
Task.WaitAll(task);
139+
await task;
140140

141141
await Task.Delay(5000);
142142

tests/Paramore.Brighter.AWS.V4.Tests/MessagingGateway/Sqs/Fifo/Reactor/When_throwing_defer_action_respect_redrive.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ public async Task When_throwing_defer_action_respect_redrive_async()
136136
var quitMessage = MessageFactory.CreateQuitMessage(_subscription.RoutingKey);
137137
_channel.Enqueue(quitMessage);
138138

139-
Task.WaitAll(task);
139+
await task;
140140

141141
await Task.Delay(5000);
142142

tests/Paramore.Brighter.Core.Tests/MessageDispatch/Proactor/When_a_handler_throws_dont_ack_action_should_nack_the_message_async.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ public async Task When_A_Handler_Throws_DontAck_Action_Should_Nack_The_Message_A
4646
// Act: run pump in background
4747
var task = Task.Factory.StartNew(() => _messagePump.Run(), TaskCreationOptions.LongRunning);
4848
// Wait for handler to process the message (DontAckAction thrown)
49-
var handled = _commandProcessor.WaitForHandle(5000);
49+
var handled = await _commandProcessor.WaitForHandleAsync(5000);
5050
await Assert.That(handled).IsTrue();
5151
// Send quit to stop the pump after DontAckAction processing
5252
_channel.Enqueue(MessageFactory.CreateQuitMessage(_routingKey));
@@ -59,4 +59,4 @@ public async Task When_A_Handler_Throws_DontAck_Action_Should_Nack_The_Message_A
5959
await Assert.That(_bus.Stream(_routingKey)).IsNotEmpty();
6060
}
6161
}
62-
}
62+
}

tests/Paramore.Brighter.Core.Tests/MessageDispatch/Proactor/When_a_handler_throws_dont_ack_action_should_not_acknowledge_async.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ public async Task When_A_Handler_Throws_DontAck_Action_Should_Not_Acknowledge_As
4646
// Act: run the pump in the background
4747
var task = Task.Factory.StartNew(() => _messagePump.Run(), TaskCreationOptions.LongRunning);
4848
// Wait for the handler to be invoked (first delivery)
49-
await Assert.That(_commandProcessor.WaitForHandle()).IsTrue();
49+
await Assert.That(await _commandProcessor.WaitForHandleAsync()).IsTrue();
5050
await Assert.That(_commandProcessor.SendCount).IsEqualTo(1);
5151
// Advance time past the ack timeout to trigger requeue of unacknowledged messages.
5252
// The message was received via InMemoryMessageConsumer.Receive which locks it in _lockedMessages.
@@ -56,7 +56,7 @@ public async Task When_A_Handler_Throws_DontAck_Action_Should_Not_Acknowledge_As
5656
// it was removed from _lockedMessages and no requeue occurs.
5757
_timeProvider.Advance(TimeSpan.FromSeconds(2));
5858
// Wait for the handler to be invoked again (re-delivery of unacknowledged message)
59-
await Assert.That(_commandProcessor.WaitForHandle()).IsTrue();
59+
await Assert.That(await _commandProcessor.WaitForHandleAsync()).IsTrue();
6060
// Assert: handler was called at least twice (original delivery + re-delivery)
6161
// This proves the message was NOT acknowledged by the DontAckAction handler
6262
await Assert.That(_commandProcessor.SendCount >= 2).IsTrue();
@@ -66,4 +66,4 @@ public async Task When_A_Handler_Throws_DontAck_Action_Should_Not_Acknowledge_As
6666
await Task.WhenAll(task);
6767
}
6868
}
69-
}
69+
}

tests/Paramore.Brighter.Core.Tests/MessageDispatch/Reactor/When_a_handler_throws_dont_ack_action_should_nack_the_message.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ public async Task When_A_Handler_Throws_DontAck_Action_Should_Nack_The_Message()
4747
// Act: run pump in background
4848
var task = Task.Factory.StartNew(() => _messagePump.Run(), TaskCreationOptions.LongRunning);
4949
// Wait for handler to process the message (DontAckAction thrown)
50-
var handled = _commandProcessor.WaitForHandle(5000);
50+
var handled = await _commandProcessor.WaitForHandleAsync(5000);
5151
await Assert.That(handled).IsTrue();
5252
// Send quit to stop the pump after DontAckAction processing
5353
_channel.Enqueue(MessageFactory.CreateQuitMessage(_routingKey));
@@ -60,4 +60,4 @@ public async Task When_A_Handler_Throws_DontAck_Action_Should_Nack_The_Message()
6060
await Assert.That(_bus.Stream(_routingKey)).IsNotEmpty();
6161
}
6262
}
63-
}
63+
}

tests/Paramore.Brighter.Core.Tests/MessageDispatch/Reactor/When_an_event_handler_throw_a_reject_message_exception.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,12 +41,12 @@ public MessageDispatchRejectMessageExceptionTests()
4141
public async Task When_an_event_handler_throw_a_reject_message_exception()
4242
{
4343
// Wait for the handler to be invoked before stopping
44-
await Assert.That(MyRejectedEventHandler.WaitForHandle()).IsTrue();
44+
await Assert.That(await MyRejectedEventHandler.WaitForHandleAsync()).IsTrue();
4545
await _dispatcher.End();
4646
await Assert.That(_bus.Stream(_routingKey)).IsEmpty();
4747
await Assert.That(_bus.Stream(_deadLetterRoutingKey)).IsNotEmpty();
4848
var message = _bus.Dequeue(_deadLetterRoutingKey);
4949
var rejectionReason = $"Message rejected reason: {RejectionReason.DeliveryError} Description: {MyRejectedEventHandlerAsync.TestOfRejectionFlow}";
5050
await Assert.That(message.Header.Bag[Message.RejectionReasonHeaderName]).IsEqualTo(rejectionReason);
5151
}
52-
}
52+
}
Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,24 @@
1-
using System.Threading;
1+
using System;
2+
using System.Threading.Tasks;
23
using Paramore.Brighter.Actions;
34

45
namespace Paramore.Brighter.Core.Tests.MessageDispatch.TestDoubles;
56

67
public class MyRejectedEventHandler : RequestHandler<MyRejectedEvent>
78
{
8-
private static readonly ManualResetEventSlim s_handled = new(false);
9+
private static TaskCompletionSource<bool> s_handled = NewCompletionSource();
910

10-
public static void Reset() => s_handled.Reset();
11-
public static bool WaitForHandle(int timeoutMs = 5000) => s_handled.Wait(timeoutMs);
11+
public static void Reset() => s_handled = NewCompletionSource();
12+
13+
public static Task<bool> WaitForHandleAsync(int timeoutMs = 5000) =>
14+
s_handled.Task.WaitAsync(TimeSpan.FromMilliseconds(timeoutMs));
15+
16+
private static TaskCompletionSource<bool> NewCompletionSource() =>
17+
new(TaskCreationOptions.RunContinuationsAsynchronously);
1218

1319
public override MyRejectedEvent Handle(MyRejectedEvent myRejectedEvent)
1420
{
15-
s_handled.Set();
21+
s_handled.TrySetResult(true);
1622
throw new RejectMessageAction("Test of rejection flow");
1723
}
1824
}

tests/Paramore.Brighter.Core.Tests/MessageDispatch/TestDoubles/SpyCommandProcessor.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,8 @@ public SpyDontAckCommandProcessor()
103103
PublishCount = 0;
104104
}
105105

106-
public bool WaitForHandle(int timeoutMs = 5000) => _handled.Wait(timeoutMs);
106+
public async Task<bool> WaitForHandleAsync(int timeoutMs = 5000) =>
107+
await _handled.WaitAsync(TimeSpan.FromMilliseconds(timeoutMs));
107108

108109
public override void Send<T>(T command, RequestContext? requestContext = null)
109110
{

tests/Paramore.Brighter.Core.Tests/Timeout/Test Doubles/MyFailsDueToTimeoutHandler.cs

Lines changed: 6 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,7 @@ THE SOFTWARE. */
2323
#endregion
2424

2525
using System;
26-
using System.Linq;
2726
using System.Threading;
28-
using System.Threading.Tasks;
2927
using Paramore.Brighter.Core.Tests.CommandProcessors.TestDoubles;
3028
using Paramore.Brighter.Policies.Attributes;
3129
using Paramore.Brighter.Policies.Handlers;
@@ -44,28 +42,16 @@ public override MyCommand Handle(MyCommand myCommand)
4442
//already died
4543
return base.Handle(myCommand);
4644
}
47-
try
48-
{
49-
var delay = Task.Delay(1000, ct).ContinueWith(
50-
x =>
51-
{
52-
// done something I should not do, because I should of been cancel
53-
myCommand.WasCancelled = false;
54-
},
55-
ct);
5645

57-
Task.WaitAll(new[] { delay });
58-
}
59-
catch (AggregateException e)
46+
if (ct.WaitHandle.WaitOne(1000))
6047
{
61-
foreach (var tce in e.InnerExceptions.OfType<TaskCanceledException>())
62-
{
63-
myCommand.WasCancelled = true;
64-
myCommand.TaskCompleted = false;
65-
return base.Handle(myCommand);
66-
}
48+
myCommand.WasCancelled = true;
49+
myCommand.TaskCompleted = false;
50+
return base.Handle(myCommand);
6751
}
6852

53+
// done something I should not do, because I should of been cancel
54+
myCommand.WasCancelled = false;
6955
myCommand.TaskCompleted = true;
7056
return base.Handle(myCommand);
7157
}

tests/Paramore.Brighter.Core.Tests/Timeout/Test Doubles/MyPassesTimeoutHandler.cs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,15 @@ internal sealed class MyPassesTimeoutHandler : RequestHandler<MyCommand>
3636
public override MyCommand Handle(MyCommand myCommand)
3737
{
3838
var ct = (CancellationToken)Context.Bag[TimeoutPolicyHandler<MyCommand>.CONTEXT_BAG_TIMEOUT_CANCELLATION_TOKEN];
39-
Task.Delay(100, ct).ContinueWith((antecedent) => { myCommand.TaskCompleted = true; }, ct).Wait(ct);
39+
if (!ct.WaitHandle.WaitOne(100))
40+
{
41+
myCommand.TaskCompleted = true;
42+
}
43+
else
44+
{
45+
ct.ThrowIfCancellationRequested();
46+
}
47+
4048
return base.Handle(myCommand);
4149
}
4250
}

0 commit comments

Comments
 (0)