Task[] tasks;
const int max = 6;
+ object cleanup_mutex = new object ();
+ List<Task> cleanup_list;
[SetUp]
public void Setup()
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)
for (int i = 0; i < max; i++) {
tasks[i] = Task.Factory.StartNew(action);
}
+ AddToCleanup (tasks);
}
[Test]
{
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");
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));
}
CountdownEvent cde = new CountdownEvent (2);
var mre = new ManualResetEvent (false);
var tasks = new[] {
- Task.Factory.StartNew (delegate { mre.WaitOne (); }),
+ Task.Factory.StartNew (delegate { Assert.IsTrue (mre.WaitOne (1500), "#0"); }),
Task.Factory.StartNew (delegate { try { throw new ApplicationException (); } finally { cde.Signal (); } }),
Task.Factory.StartNew (delegate { try { throw new ApplicationException (); } finally { cde.Signal (); } })
};
mre.Set ();
try {
- Assert.IsTrue (Task.WaitAll (tasks, 1000), "#3");
+ Task.WaitAll (tasks, 1000);
Assert.Fail ("#4");
} catch (AggregateException e) {
Assert.AreEqual (2, e.InnerExceptions.Count, "#5");
});
var onErrorTask = testTask.ContinueWith (x => continuationRan = true, TaskContinuationOptions.OnlyOnFaulted);
testTask.RunSynchronously ();
- onErrorTask.Wait (100);
+ onErrorTask.Wait (1000);
Assert.IsNotNull (e);
Assert.IsTrue (continuationRan);
}
bool? is_bg = null;
var t = new Task (() => { is_tp = Thread.CurrentThread.IsThreadPoolThread; is_bg = Thread.CurrentThread.IsBackground; });
t.Start ();
- t.Wait ();
+ Assert.IsTrue (t.Wait (5000), "#0");
Assert.IsTrue ((bool)is_tp, "#1");
Assert.IsTrue ((bool)is_bg, "#2");
is_bg = null;
t = new Task (() => { is_tp = Thread.CurrentThread.IsThreadPoolThread; is_bg = Thread.CurrentThread.IsBackground; }, TaskCreationOptions.LongRunning);
t.Start ();
- t.Wait ();
+ Assert.IsTrue (t.Wait (5000), "#10");
Assert.IsFalse ((bool) is_tp, "#11");
Assert.IsTrue ((bool) is_bg, "#12");
}