using System.Collections.Generic;
using NUnit.Framework;
+#if !MOBILE
+using NUnit.Framework.SyntaxHelpers;
+#endif
+
namespace MonoTests.System.Threading.Tasks
{
[TestFixture]
}
}
+ class NonInlineableScheduler : TaskScheduler
+ {
+ protected override IEnumerable<Task> GetScheduledTasks ()
+ {
+ throw new NotImplementedException ();
+ }
+
+ protected override void QueueTask (Task task)
+ {
+ if (!base.TryExecuteTask (task))
+ throw new ApplicationException ();
+ }
+
+ protected override bool TryExecuteTaskInline (Task task, bool taskWasPreviouslyQueued)
+ {
+ return false;
+ }
+ }
+
Task[] tasks;
const int max = 6;
Task.WaitAll (tasks);
Assert.Fail ("#1");
} catch (AggregateException e) {
- Assert.IsInstanceOfType (typeof (ApplicationException), e.InnerException, "#2");
+ Assert.That (e.InnerException, Is.TypeOf (typeof (ApplicationException)), "#2");
var inner = (TaskCanceledException) e.InnerExceptions[1];
Assert.AreEqual (tasks[1], inner.Task, "#3");
}
public void WaitingForChildrenToComplete ()
{
Task nested = null;
+ var mre = new ManualResetEvent (false);
parent_wfc = Task.Factory.StartNew (() => {
nested = Task.Factory.StartNew (() => {
+ Assert.IsTrue (mre.WaitOne (4000), "parent_wfc needs to be set first");
Assert.IsFalse (parent_wfc.Wait (10), "#1a");
Assert.AreEqual (TaskStatus.WaitingForChildrenToComplete, parent_wfc.Status, "#1b");
}, TaskCreationOptions.AttachedToParent).ContinueWith (l => {
}, TaskContinuationOptions.ExecuteSynchronously);
});
+ mre.Set ();
Assert.IsTrue (parent_wfc.Wait (2000), "#3");
Assert.IsTrue (nested.Wait (2000), "#4");
}
Assert.IsTrue (r2);
}
+ [Test]
+ public void AsyncWaitHandleSet ()
+ {
+ var task = new TaskFactory ().StartNew (() => { });
+ var ar = (IAsyncResult)task;
+ ar.AsyncWaitHandle.WaitOne ();
+ }
+
#if NET_4_5
[Test]
public void Delay_Invalid ()
}
}
+ [Test]
+ public void Delay_TimeManagement ()
+ {
+ var delay1 = Task.Delay(50);
+ var delay2 = Task.Delay(25);
+ Assert.IsTrue (Task.WhenAny(new[] { delay1, delay2 }).Wait (1000));
+ Assert.AreEqual (TaskStatus.RanToCompletion, delay2.Status);
+ }
+
[Test]
public void WaitAny_WithNull ()
{
}
}
+ [Test]
+ public void WhenAll_Empty ()
+ {
+ var tasks = new Task[0];
+
+ Task t = Task.WhenAll(tasks);
+
+ Assert.IsTrue(t.Wait(1000), "#1");
+ }
+
[Test]
public void WhenAll_WithNull ()
{
Assert.IsTrue (t.Wait (1000), "#2");
Assert.Fail ("#2a");
} catch (AggregateException e) {
- Assert.IsInstanceOfType (typeof (TaskCanceledException), e.InnerException, "#3");
+ Assert.That (e.InnerException, Is.TypeOf (typeof (TaskCanceledException)), "#3");
}
}
Assert.IsTrue (t.Wait (1000), "#2");
Assert.Fail ("#2a");
} catch (AggregateException e) {
- Assert.IsInstanceOfType (typeof (ApplicationException), e.InnerException, "#3");
- Assert.IsInstanceOfType (typeof (InvalidTimeZoneException), e.InnerExceptions[1], "#4");
+ Assert.That (e.InnerException, Is.TypeOf (typeof (ApplicationException)), "#3");
+ Assert.That (e.InnerExceptions[1], Is.TypeOf (typeof (InvalidTimeZoneException)), "#4");
}
}
Assert.IsTrue (t.Wait (1000), "#2");
}
+ [Test]
+ public void WhenAllResult_Empty ()
+ {
+ var tasks = new Task<int>[0];
+
+ Task<int[]> t = Task.WhenAll(tasks);
+
+ Assert.IsTrue(t.Wait(1000), "#1");
+ Assert.IsNotNull(t.Result, "#2");
+ Assert.AreEqual(t.Result.Length, 0, "#3");
+ }
+
[Test]
public void WhenAllResult_WithNull ()
{
Assert.IsTrue (t.Wait (1000), "#2");
Assert.Fail ("#2a");
} catch (AggregateException e) {
- Assert.IsInstanceOfType (typeof (TaskCanceledException), e.InnerException, "#3");
+ Assert.That (e.InnerException, Is.TypeOf (typeof (TaskCanceledException)), "#3");
}
try {
Assert.IsTrue (t.Wait (1000), "#2");
Assert.IsNull (t.Exception, "#3");
- Assert.IsInstanceOfType (typeof (ApplicationException), t.Result.Exception.InnerException, "#4");
+ Assert.That (t.Result.Exception.InnerException, Is.TypeOf (typeof (ApplicationException)), "#4");
}
[Test]
Assert.IsTrue (t.Wait (1000), "#2");
Assert.IsNull (t.Exception, "#3");
- Assert.IsInstanceOfType (typeof (ApplicationException), t.Result.Exception.InnerException, "#4");
+ Assert.That (t.Result.Exception.InnerException, Is.TypeOf (typeof (ApplicationException)), "#4");
}
[Test]
Assert.AreEqual ('d', d.Result, "#3r");
}
+ [Test]
+ public void ContinueWith_CustomScheduleRejected ()
+ {
+ var scheduler = new NonInlineableScheduler ();
+ var t = Task.Factory.StartNew (delegate { }).
+ ContinueWith (r => {}, CancellationToken.None, TaskContinuationOptions.ExecuteSynchronously, scheduler);
+
+ Assert.IsTrue (t.Wait (5000));
+ }
+
[Test]
public void FromResult ()
{
Assert.IsTrue (cont.Wait (1000), "#2");
Assert.Fail ();
} catch (AggregateException ex) {
- Assert.IsInstanceOfType (typeof (TaskCanceledException), ex.InnerException);
+ Assert.That (ex.InnerException, Is.TypeOf (typeof (TaskCanceledException)), "#3");
}
}
#endif