Skip to content

Commit

Permalink
Fixed flaky tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
fiseni committed Oct 18, 2024
1 parent ab98e50 commit edfaac7
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public class Pong1(TestQueue<int> queue) : INotificationHandler<Ping>
public async Task Handle(Ping notification, CancellationToken cancellationToken)
{
// Adding a delay at the start compared to Pong3. We still expect this to be the first message.
await Task.Delay(20, cancellationToken);
await Task.Delay(50, cancellationToken);
queue.Write(Id);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public class Pong1(TestQueue<int> queue) : INotificationHandler<Ping>
public async Task Handle(Ping notification, CancellationToken cancellationToken)
{
// Adding a delay at the start compared to Pong3. We still expect this to be the first message.
await Task.Delay(30, cancellationToken);
await Task.Delay(50, cancellationToken);
queue.Write(Id);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public class Pong1(TestQueue<int> queue) : INotificationHandler<Ping>
public async Task Handle(Ping notification, CancellationToken cancellationToken)
{
// Adding a delay at the start compared to Pong3. We still expect this to be the first message.
await Task.Delay(30, cancellationToken);
await Task.Delay(50, cancellationToken);
queue.Write(Id);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,9 @@ public async Task ExecutesAllHandlersConcurrently()
var mediator = scope.ServiceProvider.GetRequiredService<IMediator>();

await mediator.Publish(new Ping(), PublishStrategy.WhenAllBackground);
_queue.Write(0);

var result = await _queue.WaitForCompletion(expectedMessages: 3, timeoutInMilliseconds: 300);
result.Should().Equal(0, Pong5.Id, Pong1.Id);
var result = await _queue.WaitForCompletion(expectedMessages: 2, timeoutInMilliseconds: 300);
result.Should().BeEquivalentTo([Pong1.Id, Pong5.Id]);
}

[Fact]
Expand All @@ -36,10 +35,9 @@ public async Task FlattensAllExceptions()
var mediator = scope.ServiceProvider.GetRequiredService<IMediator>();

await mediator.Publish(new Ping(), PublishStrategy.WhenAllBackground);
_queue.Write(0);

var result = await _queue.WaitForCompletion(expectedMessages: 8, timeoutInMilliseconds: 300);
result.Should().Equal(0, Pong5.Id, Pong1.Id, 21, 31, 32, 33, 41);
var result = await _queue.WaitForCompletion(expectedMessages: 7, timeoutInMilliseconds: 300);
result.Should().BeEquivalentTo([Pong1.Id, Pong5.Id, 21, 31, 32, 33, 41]);
logger.AggregateException.Should().NotBeNull();
logger.AggregateException!.InnerExceptions.Should().HaveCount(5);
}
Expand All @@ -51,7 +49,6 @@ public class Pong1(TestQueue<int> queue) : INotificationHandler<Ping>
public const int Id = 1;
public async Task Handle(Ping notification, CancellationToken cancellationToken)
{
await Task.Delay(30, cancellationToken);
queue.Write(Id);
await Task.Delay(200, cancellationToken);
}
Expand All @@ -65,7 +62,7 @@ public class Pong3() : INotificationHandler<Ping>
{
public async Task Handle(Ping notification, CancellationToken cancellationToken)
{
await Task.Delay(10, cancellationToken);
await Task.Yield();
throw new AggregateException(
new Exception("31"),
new AggregateException(new Exception("32"), new Exception("33")));
Expand All @@ -75,7 +72,7 @@ public class Pong4() : INotificationHandler<Ping>
{
public async Task Handle(Ping notification, CancellationToken cancellationToken)
{
await Task.Delay(30, cancellationToken);
await Task.Yield();
throw new TaskCanceledException("41");
}
}
Expand All @@ -84,7 +81,6 @@ public class Pong5(TestQueue<int> queue) : INotificationHandler<Ping>
public const int Id = 5;
public async Task Handle(Ping notification, CancellationToken cancellationToken)
{
await Task.Delay(10, cancellationToken);
queue.Write(Id);
await Task.Delay(200, cancellationToken);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,9 @@ public async Task ExecutesAllHandlersConcurrently()
await mediator.Publish(new Ping(), PublishStrategy.WhenAll);
_queue.Write(0);

var result = _queue.GetValues();
result.Should().Equal(Pong7.Id, Pong1.Id, 0);
var result = await _queue.WaitForCompletion(expectedMessages: 3, timeoutInMilliseconds: 300);
result.Should().BeEquivalentTo([Pong1.Id, Pong7.Id, 0]);
result[2].Should().Be(0);
}

[Fact]
Expand All @@ -37,8 +38,9 @@ public async Task FlattensAllExceptions()
var ex = await sut.Should().ThrowExactlyAsync<AggregateException>();
_queue.Write(0);

var result = _queue.GetValues();
result.Should().Equal(Pong7.Id, Pong1.Id, 0);
var result = await _queue.WaitForCompletion(expectedMessages: 3, timeoutInMilliseconds: 300);
result.Should().BeEquivalentTo([Pong1.Id, Pong7.Id, 0]);
result[2].Should().Be(0);
ex.Which.InnerExceptions.Should().HaveCount(8);
}

Expand Down Expand Up @@ -144,8 +146,8 @@ public class Pong1(TestQueue<int> queue) : INotificationHandler<Ping>
public const int Id = 1;
public async Task Handle(Ping notification, CancellationToken cancellationToken)
{
await Task.Delay(30, cancellationToken);
queue.Write(Id);
await Task.Delay(200, cancellationToken);
}
}
public class Pong2() : INotificationHandler<Ping>
Expand Down Expand Up @@ -190,8 +192,8 @@ public class Pong7(TestQueue<int> queue) : INotificationHandler<Ping>
public const int Id = 7;
public async Task Handle(Ping notification, CancellationToken cancellationToken)
{
await Task.Yield();
queue.Write(Id);
await Task.Delay(200, cancellationToken);
}
}
}

0 comments on commit edfaac7

Please sign in to comment.