Imported Upstream version 4.0.0~alpha1

Former-commit-id: 806294f5ded97629b74c85c09952f2a74fe182d9
This commit is contained in:
Jo Shields
2015-04-07 09:35:12 +01:00
parent 283343f570
commit 3c1f479b9d
22469 changed files with 2931443 additions and 869343 deletions

View File

@@ -35,7 +35,6 @@ using NUnit.Framework;
namespace MonoTests.System.Threading.Tasks
{
[TestFixture]
[Ignore ("Not implemented yet")]
public class ConcurrentExclusiveSchedulerPairTest
{
ConcurrentExclusiveSchedulerPair schedPair;

View File

@@ -115,6 +115,16 @@ namespace MonoTests.System.Threading.Tasks
});
}
[Test]
public void ParallelForEachTestCaseWithIndex ()
{
var list = new List<int> { 0, 1, 2, 3, 4 };
Parallel.ForEach (list, (l, s, i) => {
Assert.AreEqual (l, i, "#1");
});
}
class ValueAndSquare
{
public float Value { get; set; }

View File

@@ -157,9 +157,9 @@ namespace MonoTests.System.Threading.Tasks
var mre = new ManualResetEventSlim (false);
Task[] tasks = new Task[3];
tasks[0] = new Task (() => { Thread.Sleep (0); Assert.IsTrue (mre.Wait (3000)); });
tasks[1] = new Task (() => { Assert.IsTrue (mre.Wait (3000)); });
tasks[2] = new Task (() => { Assert.IsTrue (mre.Wait (3000)); });
tasks[0] = new Task (() => { Thread.Sleep (0); Assert.IsTrue (mre.Wait (5000)); });
tasks[1] = new Task (() => { Assert.IsTrue (mre.Wait (5000)); });
tasks[2] = new Task (() => { Assert.IsTrue (mre.Wait (5000)); });
bool ran = false;
Task cont = factory.ContinueWhenAll (tasks, ts => {
@@ -172,7 +172,7 @@ namespace MonoTests.System.Threading.Tasks
mre.Set ();
Assert.IsTrue (cont.Wait (1000), "#1");
Assert.IsTrue (cont.Wait (3000), "#1");
Assert.IsTrue (ran, "#2");
}
@@ -180,7 +180,7 @@ namespace MonoTests.System.Threading.Tasks
public void ContinueWhenAll_WithMixedCompletionState ()
{
var mre = new ManualResetEventSlim ();
var task = Task.Factory.StartNew (() => mre.Wait (200));
var task = Task.Factory.StartNew (() => mre.Wait (1000));
var contFailed = task.ContinueWith (t => {}, TaskContinuationOptions.OnlyOnFaulted);
var contCanceled = task.ContinueWith (t => {}, TaskContinuationOptions.OnlyOnCanceled);
var contSuccess = task.ContinueWith (t => {}, TaskContinuationOptions.OnlyOnRanToCompletion);
@@ -189,7 +189,7 @@ namespace MonoTests.System.Threading.Tasks
var cont = Task.Factory.ContinueWhenAll (new Task[] { contFailed, contCanceled, contSuccess }, _ => ran = true);
mre.Set ();
cont.Wait (200);
cont.Wait (3000);
Assert.IsTrue (ran);
Assert.AreEqual (TaskStatus.RanToCompletion, cont.Status);
@@ -223,19 +223,20 @@ namespace MonoTests.System.Threading.Tasks
try {
factory.ContinueWhenAll (tasks, null);
Assert.Fail ("#4");
} catch (ArgumentException) {
} catch (ArgumentNullException) {
}
try {
factory.ContinueWhenAll (tasks, delegate { }, CancellationToken.None, TaskContinuationOptions.None, null);
Assert.Fail ("#5");
} catch (ArgumentException) {
} catch (ArgumentNullException) {
}
try {
factory.ContinueWhenAll (tasks, delegate { }, CancellationToken.None, TaskContinuationOptions.OnlyOnCanceled, null);
Assert.Fail ("#6");
} catch (ArgumentException) {
} catch (ArgumentNullException) {
} catch (ArgumentOutOfRangeException) {
}
}
@@ -260,8 +261,8 @@ namespace MonoTests.System.Threading.Tasks
var t2 = new ManualResetEvent (false);
var tasks = new Task[2] {
Task.Factory.StartNew (() => { t1.WaitOne (3000); }),
Task.Factory.StartNew (() => { t2.WaitOne (3000); })
Task.Factory.StartNew (() => { t1.WaitOne (5000); }),
Task.Factory.StartNew (() => { t2.WaitOne (5000); })
};
bool ran = false;
@@ -275,7 +276,7 @@ namespace MonoTests.System.Threading.Tasks
t1.Set ();
Assert.IsTrue (cont.Wait (2000), "#10");
Assert.IsTrue (cont.Wait (3000), "#10");
Assert.IsTrue (ran, "#11");
t2.Set ();
@@ -319,19 +320,20 @@ namespace MonoTests.System.Threading.Tasks
try {
factory.ContinueWhenAny (tasks, null);
Assert.Fail ("#4");
} catch (ArgumentException) {
} catch (ArgumentNullException) {
}
try {
factory.ContinueWhenAny (tasks, delegate { }, CancellationToken.None, TaskContinuationOptions.None, null);
Assert.Fail ("#5");
} catch (ArgumentException) {
} catch (ArgumentNullException) {
}
try {
factory.ContinueWhenAny (tasks, delegate { }, CancellationToken.None, TaskContinuationOptions.OnlyOnCanceled, null);
Assert.Fail ("#6");
} catch (ArgumentException) {
} catch (ArgumentNullException) {
} catch (ArgumentOutOfRangeException) {
}
}
@@ -613,15 +615,14 @@ namespace MonoTests.System.Threading.Tasks
} catch (InvalidOperationException) {
}
Assert.IsTrue (task.IsCanceled, "#2");
task = factory.StartNew (() => { }, ct);
try {
task.Wait ();
Assert.Fail ("#2");
} catch (AggregateException e) {
Assert.IsTrue (task.IsCanceled, "#3");
Assert.That (e.InnerException, Is.TypeOf (typeof (TaskCanceledException)), "#4");
Assert.That (e.InnerException, Is.TypeOf (typeof (TaskCanceledException)), "#3");
}
Assert.IsTrue (task.IsCanceled, "#4");
}
}
}

View File

@@ -265,15 +265,14 @@ namespace MonoTests.System.Threading.Tasks
} catch (InvalidOperationException) {
}
Assert.IsTrue (task.IsCanceled, "#2");
task = factory.StartNew (() => 1, ct);
try {
task.Wait ();
Assert.Fail ("#2");
} catch (AggregateException e) {
Assert.IsTrue (task.IsCanceled, "#3");
Assert.That (e.InnerException, Is.TypeOf (typeof (TaskCanceledException)), "#4");
Assert.That (e.InnerException, Is.TypeOf (typeof (TaskCanceledException)), "#3");
}
Assert.IsTrue (task.IsCanceled, "#4");
}
}
}

View File

@@ -109,6 +109,8 @@ namespace MonoTests.System.Threading.Tasks
Task[] tasks;
const int max = 6;
object cleanup_mutex = new object ();
List<Task> cleanup_list;
[SetUp]
public void Setup()
@@ -116,13 +118,35 @@ namespace MonoTests.System.Threading.Tasks
ThreadPool.GetMinThreads (out workerThreads, out completionPortThreads);
ThreadPool.SetMinThreads (1, 1);
tasks = new Task[max];
tasks = new Task[max];
cleanup_list = new List<Task> ();
}
[TearDown]
public void Teardown()
{
ThreadPool.SetMinThreads (workerThreads, completionPortThreads);
Task[] l = null;
lock (cleanup_mutex) {
l = cleanup_list.ToArray ();
}
try {
Task.WaitAll (l);
} catch (Exception) {
}
}
void AddToCleanup (Task[] tasks) {
lock (cleanup_mutex) {
foreach (var t in tasks)
cleanup_list.Add (t);
}
}
void AddToCleanup (Task task) {
lock (cleanup_mutex) {
cleanup_list.Add (task);
}
}
void InitWithDelegate(Action action)
@@ -130,6 +154,7 @@ namespace MonoTests.System.Threading.Tasks
for (int i = 0; i < max; i++) {
tasks[i] = Task.Factory.StartNew(action);
}
AddToCleanup (tasks);
}
[Test]
@@ -212,11 +237,11 @@ namespace MonoTests.System.Threading.Tasks
{
var mre = new ManualResetEventSlim (false);
var tasks = new Task[] {
Task.Factory.StartNew (delegate { mre.Wait (1000); }),
Task.Factory.StartNew (delegate { mre.Wait (5000); }),
Task.Factory.StartNew (delegate { throw new ApplicationException (); })
};
Assert.AreEqual (1, Task.WaitAny (tasks, 1000), "#1");
Assert.AreEqual (1, Task.WaitAny (tasks, 3000), "#1");
Assert.IsFalse (tasks[0].IsCompleted, "#2");
Assert.IsTrue (tasks[1].IsFaulted, "#3");
@@ -286,6 +311,7 @@ namespace MonoTests.System.Threading.Tasks
for (int i = 0; i < tasks.Length; i++) {
tasks[i] = Task.Factory.StartNew (delegate { Thread.Sleep (0); });
}
AddToCleanup (tasks);
Assert.IsTrue (Task.WaitAll (tasks, 5000));
}
@@ -492,13 +518,26 @@ namespace MonoTests.System.Threading.Tasks
Assert.AreEqual (true, previouslyQueued);
}
[Test, ExpectedException (typeof (InvalidOperationException))]
[Test]
public void CreationWhileInitiallyCanceled ()
{
var token = new CancellationToken (true);
var task = new Task (() => { }, token);
Assert.AreEqual (TaskStatus.Canceled, task.Status);
task.Start ();
try {
task.Start ();
Assert.Fail ("#1");
} catch (InvalidOperationException) {
}
try {
task.Wait ();
Assert.Fail ("#2");
} catch (AggregateException e) {
Assert.That (e.InnerException, Is.TypeOf (typeof (TaskCanceledException)), "#3");
}
Assert.IsTrue (task.IsCanceled, "#4");
}
[Test]
@@ -508,25 +547,25 @@ namespace MonoTests.System.Threading.Tasks
try {
task.ContinueWith (null);
Assert.Fail ("#1");
} catch (ArgumentException) {
} catch (ArgumentNullException e) {
}
try {
task.ContinueWith (delegate { }, null);
Assert.Fail ("#2");
} catch (ArgumentException) {
} catch (ArgumentNullException e) {
}
try {
task.ContinueWith (delegate { }, TaskContinuationOptions.OnlyOnCanceled | TaskContinuationOptions.NotOnCanceled);
Assert.Fail ("#3");
} catch (ArgumentException) {
} catch (ArgumentOutOfRangeException) {
}
try {
task.ContinueWith (delegate { }, TaskContinuationOptions.OnlyOnRanToCompletion | TaskContinuationOptions.NotOnRanToCompletion);
Assert.Fail ("#4");
} catch (ArgumentException) {
} catch (ArgumentOutOfRangeException) {
}
}
@@ -985,7 +1024,7 @@ namespace MonoTests.System.Threading.Tasks
[Test]
public void Start_NullArgument ()
{
var t = Task.Factory.StartNew (delegate () { });
var t = new Task (() => { });
try {
t.Start (null);
Assert.Fail ();
@@ -1850,7 +1889,7 @@ namespace MonoTests.System.Threading.Tasks
bool? is_bg = null;
var t = new Task (() => { is_tp = Thread.CurrentThread.IsThreadPoolThread; is_bg = Thread.CurrentThread.IsBackground; });
t.Start ();
Assert.IsTrue (t.Wait (100));
t.Wait ();
Assert.IsTrue ((bool)is_tp, "#1");
Assert.IsTrue ((bool)is_bg, "#2");
@@ -1858,8 +1897,7 @@ namespace MonoTests.System.Threading.Tasks
is_bg = null;
t = new Task (() => { is_tp = Thread.CurrentThread.IsThreadPoolThread; is_bg = Thread.CurrentThread.IsBackground; }, TaskCreationOptions.LongRunning);
t.Start ();
Assert.IsTrue (t.Wait (100));
t.Wait ();
Assert.IsFalse ((bool) is_tp, "#11");
Assert.IsTrue ((bool) is_bg, "#12");
}