Skip to content

Commit

Permalink
Improve DataFlow test reliability
Browse files Browse the repository at this point in the history
  • Loading branch information
karolz-ms committed Jan 25, 2018
1 parent b03c28d commit 92158e4
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 6 deletions.
12 changes: 7 additions & 5 deletions test/Microsoft.Diagnostics.EventFlow.Core.Tests/DataflowTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -442,6 +442,10 @@ private async Task BlockKeepsDeclinedMessages(Func<ISourceBlock<int>> BlockFacto

private async Task BlockKeepsPostponedMessages(Func<ISourceBlock<int>> BlockFactory)
{
// This test requires longer timeout than most of the other tests in this suite, otherwise it occasionally fails on slower machines
// If it fails nevertheless, switch to custom TaskScheduler.
TimeSpan longArrivalTimeout = TimeSpan.FromMilliseconds(MessageArrivalTimeout.TotalMilliseconds * 10.0);

ISourceBlock<int> block = BlockFactory();
ITargetBlock<int> blockT = (ITargetBlock<int>)block;

Expand All @@ -458,21 +462,19 @@ private async Task BlockKeepsPostponedMessages(Func<ISourceBlock<int>> BlockFact
// Out of capacity
Assert.False(blockT.Post(4));

// However SendAsync() will allow postponing the message, sot the message will be eventually delivered
// However SendAsync() will allow postponing the message, so the message will be eventually delivered
blockT.SendAsync(5).Forget();

// Wait till the block offers a message
// Assumption: only one message will be offered, the block will not offer more messages if the target postpones
bool messageOffered = await TaskUtils.PollWaitAsync(() => testTarget.MessagesPostponed.Count == 1, MessageArrivalTimeout);
bool messageOffered = await TaskUtils.PollWaitAsync(() => testTarget.MessagesPostponed.Count == 1, longArrivalTimeout);
Assert.True(messageOffered);

// Assumption: once the block target stops postponing, the block will keep pushing data to target
// until it runs out of buffered messages.
testTarget.ConsumptionMode = DataflowMessageStatus.Accepted;
testTarget.ConsumePostponedMessages();
// Use 10 times the normal message arrival timeout for extra padding--the test tended to be a bit flakey at this point.
// If this happens again, switch to custom TaskScheduler.
bool gotAllMessages = await TaskUtils.PollWaitAsync(() => testTarget.MessagesConsumed.Count == 4, TimeSpan.FromMilliseconds(MessageArrivalTimeout.TotalMilliseconds * 10));
bool gotAllMessages = await TaskUtils.PollWaitAsync(() => testTarget.MessagesConsumed.Count == 4, longArrivalTimeout);
Assert.True(gotAllMessages, "We should have gotten 4 messages");
Assert.Equal(testTarget.MessagesConsumed.OrderBy((i) => i), new int[] { 1, 2, 3, 5 });
}
Expand Down
2 changes: 1 addition & 1 deletion test/TestHelpers/TaskUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ namespace Microsoft.Diagnostics.EventFlow.TestHelpers
{
public static class TaskUtils
{
private static readonly TimeSpan JustABit = TimeSpan.FromMilliseconds(20);
private static readonly TimeSpan JustABit = TimeSpan.FromMilliseconds(50);

public static async Task<bool> PollWaitAsync(Func<bool> condition, TimeSpan timeout)
{
Expand Down

0 comments on commit 92158e4

Please sign in to comment.