Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Stabilize unit test #3311

Merged
merged 3 commits into from
Feb 1, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -417,14 +417,16 @@ public async Task ProcessTestRunAttachmentsAsync_ShouldReturnInitialAttachmentsT

var innerTaskCompletionSource = new TaskCompletionSource<object>();

CountdownEvent expectedProgress = new(4);
_mockEventsHandler.Setup(h => h.HandleTestRunAttachmentsProcessingProgress(It.IsAny<TestRunAttachmentsProcessingProgressEventArgs>())).Callback((TestRunAttachmentsProcessingProgressEventArgs _) => expectedProgress.Signal());

_mockAttachmentHandler1.Setup(h => h.ProcessAttachmentSetsAsync(It.IsAny<XmlElement>(), It.IsAny<ICollection<AttachmentSet>>(), It.IsAny<IProgress<int>>(), It.IsAny<IMessageLogger>(), It.IsAny<CancellationToken>()))
.Returns((string configElement, ICollection<AttachmentSet> i1, IProgress<int> progress, IMessageLogger logger, CancellationToken cancellation) =>
{
try
{
for (int i = 0; i < 100; ++i)
{
Task.Delay(200, cancellation).Wait(cancellation);
Console.WriteLine($"Iteration: {i}");
logger.SendMessage(TestMessageLevel.Informational, $"Iteration: {i}");

Expand All @@ -434,33 +436,39 @@ public async Task ProcessTestRunAttachmentsAsync_ShouldReturnInitialAttachmentsT
if (i == 3)
{
_cancellationTokenSource.Cancel();
Task.Delay(1000, cancellation).Wait(cancellation);
}
}
}
catch (OperationCanceledException)
finally
{
innerTaskCompletionSource.TrySetResult(null);
}

return Task.FromResult(outputAttachments);
});

ManualResetEventSlim attachmentProcessingComplete = new(false);
_mockEventsHandler.Setup(h => h.HandleTestRunAttachmentsProcessingComplete(It.IsAny<TestRunAttachmentsProcessingCompleteEventArgs>(), It.IsAny<IEnumerable<AttachmentSet>>()))
.Callback((TestRunAttachmentsProcessingCompleteEventArgs _, IEnumerable<AttachmentSet> _) => attachmentProcessingComplete.Set());

// act
await _manager.ProcessTestRunAttachmentsAsync(Constants.EmptyRunSettings, _mockRequestData.Object, inputAttachments, new InvokedDataCollector[0], _mockEventsHandler.Object, _cancellationTokenSource.Token);
Console.WriteLine("Attachments processing done");
await innerTaskCompletionSource.Task;

// assert
VerifyCompleteEvent(true, false, inputAttachments[0]);
// Wait to drain all progress events
Assert.IsTrue(expectedProgress.Wait(TimeSpan.FromMinutes(1)));

// TODO: Make more stable
// _mockEventsHandler.Verify(h => h.HandleTestRunAttachmentsProcessingProgress(It.IsAny<TestRunAttachmentsProcessingProgressEventArgs>()), Times.Exactly(4));
// _mockEventsHandler.Verify(h => h.HandleTestRunAttachmentsProcessingProgress(It.Is<TestRunAttachmentsProcessingProgressEventArgs>(a => VerifyProgressArgs(a, 1))));
// _mockEventsHandler.Verify(h => h.HandleTestRunAttachmentsProcessingProgress(It.Is<TestRunAttachmentsProcessingProgressEventArgs>(a => VerifyProgressArgs(a, 2))));
// _mockEventsHandler.Verify(h => h.HandleTestRunAttachmentsProcessingProgress(It.Is<TestRunAttachmentsProcessingProgressEventArgs>(a => VerifyProgressArgs(a, 3))));
// _mockEventsHandler.Verify(h => h.HandleTestRunAttachmentsProcessingProgress(It.Is<TestRunAttachmentsProcessingProgressEventArgs>(a => VerifyProgressArgs(a, 4))));
// Wait for the HandleTestRunAttachmentsProcessingComplete
Assert.IsTrue(attachmentProcessingComplete.Wait(TimeSpan.FromMinutes(1)));

// assert
VerifyCompleteEvent(true, false, inputAttachments[0]);
_mockEventsHandler.Verify(h => h.HandleTestRunAttachmentsProcessingProgress(It.IsAny<TestRunAttachmentsProcessingProgressEventArgs>()), Times.Exactly(4));
_mockEventsHandler.Verify(h => h.HandleTestRunAttachmentsProcessingProgress(It.Is<TestRunAttachmentsProcessingProgressEventArgs>(a => VerifyProgressArgs(a, 1))));
_mockEventsHandler.Verify(h => h.HandleTestRunAttachmentsProcessingProgress(It.Is<TestRunAttachmentsProcessingProgressEventArgs>(a => VerifyProgressArgs(a, 2))));
_mockEventsHandler.Verify(h => h.HandleTestRunAttachmentsProcessingProgress(It.Is<TestRunAttachmentsProcessingProgressEventArgs>(a => VerifyProgressArgs(a, 3))));
_mockEventsHandler.Verify(h => h.HandleTestRunAttachmentsProcessingProgress(It.Is<TestRunAttachmentsProcessingProgressEventArgs>(a => VerifyProgressArgs(a, 4))));
_mockEventsHandler.Verify(h => h.HandleLogMessage(TestMessageLevel.Informational, "Attachments processing was cancelled."));
_mockAttachmentHandler1.Verify(h => h.GetExtensionUris());
_mockAttachmentHandler2.Verify(h => h.GetExtensionUris(), Times.Never);
Expand Down Expand Up @@ -506,7 +514,7 @@ public async Task ProcessTestRunAttachmentsAsync_ShouldReturnInitialAttachments_
}
}
}
catch (OperationCanceledException)
finally
{
innerTaskCompletionSource.TrySetResult(null);
}
Expand Down