From ab9eaf5c357a92a3b0c256039780a9ca95b647f4 Mon Sep 17 00:00:00 2001 From: Olivier Coanet Date: Sat, 22 Jan 2022 18:28:18 +0100 Subject: [PATCH] Fix flaky timeout tests --- .../Processing/AsyncBatchEventProcessorTests.cs | 8 ++++---- .../Processing/BatchEventProcessorTests.cs | 9 +++++---- .../Processing/EventProcessorTests.cs | 9 +++++---- src/Disruptor.Tests/Support/TestExceptionHandler.cs | 12 ++++++------ 4 files changed, 20 insertions(+), 18 deletions(-) diff --git a/src/Disruptor.Tests/Processing/AsyncBatchEventProcessorTests.cs b/src/Disruptor.Tests/Processing/AsyncBatchEventProcessorTests.cs index db38b16b..15141127 100644 --- a/src/Disruptor.Tests/Processing/AsyncBatchEventProcessorTests.cs +++ b/src/Disruptor.Tests/Processing/AsyncBatchEventProcessorTests.cs @@ -84,8 +84,8 @@ public void ShouldCallExceptionHandlerOnTimeoutException() var ringBuffer = new RingBuffer(() => new StubEvent(-1), new SingleProducerSequencer(16, waitStrategy)); var sequenceBarrier = (IAsyncSequenceBarrier)ringBuffer.NewBarrier(); - var exceptionSignal = new CountdownEvent(1); - var exceptionHandler = new TestExceptionHandler(x => exceptionSignal.Signal()); + var exception = new TaskCompletionSource(); + var exceptionHandler = new TestExceptionHandler(x => exception.TrySetResult(x.ex)); var eventHandler = new TestAsyncBatchEventHandler(x => { }, () => throw new NullReferenceException()); var eventProcessor = CreateEventProcessor(ringBuffer, sequenceBarrier, eventHandler); ringBuffer.AddGatingSequences(eventProcessor.Sequence); @@ -94,7 +94,7 @@ public void ShouldCallExceptionHandlerOnTimeoutException() var task = eventProcessor.Start(); - Assert.IsTrue(exceptionSignal.Wait(TimeSpan.FromSeconds(2))); + Assert.IsTrue(exception.Task.Wait(TimeSpan.FromSeconds(2))); Assert.AreEqual(0, exceptionHandler.EventExceptionCount); Assert.AreEqual(1, exceptionHandler.TimeoutExceptionCount); Assert.AreEqual(0, exceptionHandler.BatchExceptionCount); @@ -256,4 +256,4 @@ public bool WaitShutdown(TimeSpan timeSpan) return _shutdownSignal.WaitOne(timeSpan); } } -} \ No newline at end of file +} diff --git a/src/Disruptor.Tests/Processing/BatchEventProcessorTests.cs b/src/Disruptor.Tests/Processing/BatchEventProcessorTests.cs index b0f0b4be..4d9ad9bf 100644 --- a/src/Disruptor.Tests/Processing/BatchEventProcessorTests.cs +++ b/src/Disruptor.Tests/Processing/BatchEventProcessorTests.cs @@ -1,5 +1,6 @@ using System; using System.Threading; +using System.Threading.Tasks; using Disruptor.Processing; using Disruptor.Tests.Support; using NUnit.Framework; @@ -83,8 +84,8 @@ public void ShouldCallExceptionHandlerOnTimeoutException() var ringBuffer = new RingBuffer(() => new StubEvent(-1), new SingleProducerSequencer(16, waitStrategy)); var sequenceBarrier = ringBuffer.NewBarrier(); - var exceptionSignal = new CountdownEvent(1); - var exceptionHandler = new TestExceptionHandler(x => exceptionSignal.Signal()); + var exception = new TaskCompletionSource(); + var exceptionHandler = new TestExceptionHandler(x => exception.TrySetResult(x.ex)); var eventHandler = new TestBatchEventHandler(x => { }, () => throw new NullReferenceException()); var eventProcessor = CreateEventProcessor(ringBuffer, sequenceBarrier, eventHandler); ringBuffer.AddGatingSequences(eventProcessor.Sequence); @@ -93,7 +94,7 @@ public void ShouldCallExceptionHandlerOnTimeoutException() var task = eventProcessor.Start(); - Assert.IsTrue(exceptionSignal.Wait(TimeSpan.FromSeconds(2))); + Assert.IsTrue(exception.Task.Wait(TimeSpan.FromSeconds(2))); Assert.AreEqual(0, exceptionHandler.EventExceptionCount); Assert.AreEqual(1, exceptionHandler.TimeoutExceptionCount); Assert.AreEqual(0, exceptionHandler.BatchExceptionCount); @@ -254,4 +255,4 @@ public bool WaitShutdown(TimeSpan timeSpan) return _shutdownSignal.WaitOne(timeSpan); } } -} \ No newline at end of file +} diff --git a/src/Disruptor.Tests/Processing/EventProcessorTests.cs b/src/Disruptor.Tests/Processing/EventProcessorTests.cs index ad3f9a41..6203911f 100644 --- a/src/Disruptor.Tests/Processing/EventProcessorTests.cs +++ b/src/Disruptor.Tests/Processing/EventProcessorTests.cs @@ -1,6 +1,7 @@ using System; using System.Collections.Generic; using System.Threading; +using System.Threading.Tasks; using Disruptor.Processing; using Disruptor.Tests.Support; using NUnit.Framework; @@ -84,8 +85,8 @@ public void ShouldCallExceptionHandlerOnTimeoutException() var ringBuffer = new RingBuffer(() => new StubEvent(-1), new SingleProducerSequencer(16, waitStrategy)); var sequenceBarrier = ringBuffer.NewBarrier(); - var exceptionSignal = new CountdownEvent(1); - var exceptionHandler = new TestExceptionHandler(x => exceptionSignal.Signal()); + var exception = new TaskCompletionSource(); + var exceptionHandler = new TestExceptionHandler(x => exception.TrySetResult(x.ex)); var eventHandler = new TestEventHandler(x => { }, () => throw new NullReferenceException()); var eventProcessor = CreateEventProcessor(ringBuffer, sequenceBarrier, eventHandler); ringBuffer.AddGatingSequences(eventProcessor.Sequence); @@ -94,7 +95,7 @@ public void ShouldCallExceptionHandlerOnTimeoutException() var task = eventProcessor.Start(); - Assert.IsTrue(exceptionSignal.Wait(TimeSpan.FromSeconds(2))); + Assert.IsTrue(exception.Task.Wait(TimeSpan.FromSeconds(2))); Assert.AreEqual(0, exceptionHandler.EventExceptionCount); Assert.AreEqual(1, exceptionHandler.TimeoutExceptionCount); Assert.AreEqual(0, exceptionHandler.BatchExceptionCount); @@ -362,4 +363,4 @@ public void OnBatchStart(long batchSize) internal class BatchAwareEventHandlerInternal : BatchAwareEventHandler { } -} \ No newline at end of file +} diff --git a/src/Disruptor.Tests/Support/TestExceptionHandler.cs b/src/Disruptor.Tests/Support/TestExceptionHandler.cs index c5956943..7d1f5dfc 100644 --- a/src/Disruptor.Tests/Support/TestExceptionHandler.cs +++ b/src/Disruptor.Tests/Support/TestExceptionHandler.cs @@ -5,9 +5,9 @@ namespace Disruptor.Tests.Support; public class TestExceptionHandler : IExceptionHandler where T : class { - private readonly Action _action; + private readonly Action<(T? data, Exception ex)> _action; - public TestExceptionHandler(Action action) + public TestExceptionHandler(Action<(T? data, Exception ex)> action) { _action = action; } @@ -18,7 +18,7 @@ public void HandleEventException(Exception ex, long sequence, T evt) { EventExceptionCount++; - _action.Invoke(evt); + _action.Invoke((evt, ex)); } public int TimeoutExceptionCount { get; private set; } @@ -27,7 +27,7 @@ public void HandleOnTimeoutException(Exception ex, long sequence) { TimeoutExceptionCount++; - _action.Invoke(null); + _action.Invoke((null, ex)); } public int BatchExceptionCount { get; private set; } @@ -38,7 +38,7 @@ public void HandleEventException(Exception ex, long sequence, EventBatch batc foreach (var evt in batch) { - _action.Invoke(evt); + _action.Invoke((evt, ex)); } } @@ -49,4 +49,4 @@ public void HandleOnStartException(Exception ex) public void HandleOnShutdownException(Exception ex) { } -} \ No newline at end of file +}