Skip to content

Commit

Permalink
Fix flaky timeout tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ocoanet committed Jan 22, 2022
1 parent 26ee403 commit ab9eaf5
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,8 @@ public void ShouldCallExceptionHandlerOnTimeoutException()
var ringBuffer = new RingBuffer<StubEvent>(() => new StubEvent(-1), new SingleProducerSequencer(16, waitStrategy));
var sequenceBarrier = (IAsyncSequenceBarrier)ringBuffer.NewBarrier();

var exceptionSignal = new CountdownEvent(1);
var exceptionHandler = new TestExceptionHandler<StubEvent>(x => exceptionSignal.Signal());
var exception = new TaskCompletionSource<Exception>();
var exceptionHandler = new TestExceptionHandler<StubEvent>(x => exception.TrySetResult(x.ex));
var eventHandler = new TestAsyncBatchEventHandler<StubEvent>(x => { }, () => throw new NullReferenceException());
var eventProcessor = CreateEventProcessor(ringBuffer, sequenceBarrier, eventHandler);
ringBuffer.AddGatingSequences(eventProcessor.Sequence);
Expand All @@ -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);
Expand Down Expand Up @@ -256,4 +256,4 @@ public bool WaitShutdown(TimeSpan timeSpan)
return _shutdownSignal.WaitOne(timeSpan);
}
}
}
}
9 changes: 5 additions & 4 deletions src/Disruptor.Tests/Processing/BatchEventProcessorTests.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Threading;
using System.Threading.Tasks;
using Disruptor.Processing;
using Disruptor.Tests.Support;
using NUnit.Framework;
Expand Down Expand Up @@ -83,8 +84,8 @@ public void ShouldCallExceptionHandlerOnTimeoutException()
var ringBuffer = new RingBuffer<StubEvent>(() => new StubEvent(-1), new SingleProducerSequencer(16, waitStrategy));
var sequenceBarrier = ringBuffer.NewBarrier();

var exceptionSignal = new CountdownEvent(1);
var exceptionHandler = new TestExceptionHandler<StubEvent>(x => exceptionSignal.Signal());
var exception = new TaskCompletionSource<Exception>();
var exceptionHandler = new TestExceptionHandler<StubEvent>(x => exception.TrySetResult(x.ex));
var eventHandler = new TestBatchEventHandler<StubEvent>(x => { }, () => throw new NullReferenceException());
var eventProcessor = CreateEventProcessor(ringBuffer, sequenceBarrier, eventHandler);
ringBuffer.AddGatingSequences(eventProcessor.Sequence);
Expand All @@ -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);
Expand Down Expand Up @@ -254,4 +255,4 @@ public bool WaitShutdown(TimeSpan timeSpan)
return _shutdownSignal.WaitOne(timeSpan);
}
}
}
}
9 changes: 5 additions & 4 deletions src/Disruptor.Tests/Processing/EventProcessorTests.cs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -84,8 +85,8 @@ public void ShouldCallExceptionHandlerOnTimeoutException()
var ringBuffer = new RingBuffer<StubEvent>(() => new StubEvent(-1), new SingleProducerSequencer(16, waitStrategy));
var sequenceBarrier = ringBuffer.NewBarrier();

var exceptionSignal = new CountdownEvent(1);
var exceptionHandler = new TestExceptionHandler<StubEvent>(x => exceptionSignal.Signal());
var exception = new TaskCompletionSource<Exception>();
var exceptionHandler = new TestExceptionHandler<StubEvent>(x => exception.TrySetResult(x.ex));
var eventHandler = new TestEventHandler<StubEvent>(x => { }, () => throw new NullReferenceException());
var eventProcessor = CreateEventProcessor(ringBuffer, sequenceBarrier, eventHandler);
ringBuffer.AddGatingSequences(eventProcessor.Sequence);
Expand All @@ -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);
Expand Down Expand Up @@ -362,4 +363,4 @@ public void OnBatchStart(long batchSize)
internal class BatchAwareEventHandlerInternal : BatchAwareEventHandler
{
}
}
}
12 changes: 6 additions & 6 deletions src/Disruptor.Tests/Support/TestExceptionHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ namespace Disruptor.Tests.Support;
public class TestExceptionHandler<T> : IExceptionHandler<T>
where T : class
{
private readonly Action<T?> _action;
private readonly Action<(T? data, Exception ex)> _action;

public TestExceptionHandler(Action<T?> action)
public TestExceptionHandler(Action<(T? data, Exception ex)> action)
{
_action = action;
}
Expand All @@ -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; }
Expand All @@ -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; }
Expand All @@ -38,7 +38,7 @@ public void HandleEventException(Exception ex, long sequence, EventBatch<T> batc

foreach (var evt in batch)
{
_action.Invoke(evt);
_action.Invoke((evt, ex));
}
}

Expand All @@ -49,4 +49,4 @@ public void HandleOnStartException(Exception ex)
public void HandleOnShutdownException(Exception ex)
{
}
}
}

0 comments on commit ab9eaf5

Please sign in to comment.