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

Merge pull request #34180 from sharwell/ignored-cancel

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