未验证 提交 5badb211 编写于 作者: S Sam Harwell 提交者: GitHub

Merge pull request #34180 from sharwell/ignored-cancel

Fix flaky test AsynchronousOperationListenerTests.IgnoredCancel
......@@ -5,8 +5,6 @@
using System.Threading;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis.Shared.TestHooks;
using Microsoft.CodeAnalysis.Text;
using Roslyn.Test.Utilities;
using Roslyn.Utilities;
using Xunit;
......@@ -26,20 +24,18 @@ public SleepHelper()
_tokenSource = new CancellationTokenSource();
}
~SleepHelper()
public void Dispose()
{
if (!Environment.HasShutdownStarted)
Task[] tasks;
lock (_tasks)
{
Contract.Fail("Should have been disposed");
}
_tokenSource.Cancel();
tasks = _tasks.ToArray();
}
public void Dispose()
{
_tokenSource.Cancel();
try
{
Task.WaitAll(_tasks.ToArray());
Task.WaitAll(tasks);
}
catch (AggregateException e)
{
......@@ -54,7 +50,10 @@ public void Dispose()
public void Sleep(TimeSpan timeToSleep)
{
var task = Task.Factory.SafeStartNew(() =>
Task task;
lock (_tasks)
{
task = Task.Factory.SafeStartNew(() =>
{
while (true)
{
......@@ -64,6 +63,7 @@ public void Sleep(TimeSpan timeToSleep)
}, _tokenSource.Token, TaskScheduler.Default);
_tasks.Add(task);
}
task.Wait((int)timeToSleep.TotalMilliseconds);
}
......@@ -252,9 +252,9 @@ public void IgnoredCancel()
var signal = new ManualResetEventSlim();
var listener = new AsynchronousOperationListener();
var done = false;
var queuedFinished = false;
var cancelledFinished = false;
var done = new TaskCompletionSource<VoidResult>();
var queuedFinished = new TaskCompletionSource<VoidResult>();
var cancelledFinished = new TaskCompletionSource<VoidResult>();
var asyncToken1 = listener.BeginAsyncOperation("Test");
var task = new Task(() =>
{
......@@ -263,34 +263,31 @@ public void IgnoredCancel()
var cancelledTask = new Task(() =>
{
sleepHelper.Sleep(TimeSpan.FromSeconds(10));
cancelledFinished = true;
cancelledFinished.SetResult(default);
});
signal.Set();
cancelledTask.Start(TaskScheduler.Default);
}
sleepHelper.Sleep(TimeSpan.FromMilliseconds(500));
// Now that we've cancelled the first request, queue another one to make sure we wait for it.
var asyncToken2 = listener.BeginAsyncOperation("Test");
var queuedTask = new Task(() =>
{
sleepHelper.Sleep(TimeSpan.FromSeconds(1));
queuedFinished = true;
queuedFinished.SetResult(default);
});
queuedTask.CompletesAsyncOperation(asyncToken2);
queuedTask.Start(TaskScheduler.Default);
done = true;
done.SetResult(default);
});
task.CompletesAsyncOperation(asyncToken1);
task.Start(TaskScheduler.Default);
Wait(listener, signal);
Assert.True(done, "Cancelling should have completed the current task.");
Assert.True(queuedFinished, "Continued didn't run, but it was supposed to ignore the cancel.");
Assert.False(cancelledFinished, "We waited for the cancelled task to finish.");
Assert.True(done.Task.IsCompleted, "Cancelling should have completed the current task.");
Assert.True(queuedFinished.Task.IsCompleted, "Continued didn't run, but it was supposed to ignore the cancel.");
Assert.False(cancelledFinished.Task.IsCompleted, "We waited for the cancelled task to finish.");
}
}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册