public void Sched_AC_Test()
{
int n = 0;
bool insideTask = false;
UnitTestSchedulingContext context = new UnitTestSchedulingContext();
orleansTaskScheduler = TestHelper.InitializeSchedulerForTesting(context);
Console.WriteLine("Running Main in Context=" + RuntimeContext.Current);
orleansTaskScheduler.QueueWorkItem(new ClosureWorkItem(() =>
{
for (int i = 0; i < 10; i++)
{
Task.Factory.StartNew(() =>
{
// ReSharper disable AccessToModifiedClosure
Console.WriteLine("Starting " + i + " in Context=" + RuntimeContext.Current);
Assert.IsFalse(insideTask, "Starting new task when I am already inside task of iteration {0}", n);
insideTask = true;
int k = n;
Thread.Sleep(100);
n = k + 1;
insideTask = false;
// ReSharper restore AccessToModifiedClosure
}).Ignore();
}
}), context);
// Pause to let things run
Thread.Sleep(1500);
// N should be 10, because all tasks should execute serially
Assert.IsTrue(n != 0, "Work items did not get executed");
Assert.AreEqual(10, n, "Work items executed concurrently");
}
public async Task Sched_AC_WaitTest()
{
int n = 0;
bool insideTask = false;
UnitTestSchedulingContext context = new UnitTestSchedulingContext();
orleansTaskScheduler = TestHelper.InitializeSchedulerForTesting(context);
var result = new TaskCompletionSource<bool>();
orleansTaskScheduler.QueueWorkItem(new ClosureWorkItem(() =>
{
var task1 = Task.Factory.StartNew(() =>
{
Console.WriteLine("Starting 1");
Assert.IsFalse(insideTask, "Starting new task when I am already inside task of iteration {0}", n);
insideTask = true;
Console.WriteLine("===> 1a");
Thread.Sleep(1000); n = n + 3;
Console.WriteLine("===> 1b");
insideTask = false;
});
var task2 = Task.Factory.StartNew(() =>
{
Console.WriteLine("Starting 2");
Assert.IsFalse(insideTask, "Starting new task when I am alraedy inside task of iteration {0}", n);
insideTask = true;
Console.WriteLine("===> 2a");
task1.Wait();
Console.WriteLine("===> 2b");
n = n * 5;
Console.WriteLine("===> 2c");
insideTask = false;
result.SetResult(true);
});
task1.Ignore();
task2.Ignore();
}), context);
var timeoutLimit = TimeSpan.FromMilliseconds(1500);
try
{
await result.Task.WithTimeout(timeoutLimit);
}
catch (TimeoutException)
{
Assert.Fail("Result did not arrive before timeout " + timeoutLimit);
}
Assert.IsTrue(n != 0, "Work items did not get executed");
Assert.AreEqual(15, n, "Work items executed out of order");
}
public void Async_Task_Start_OrleansTaskScheduler()
{
InitSchedulerLogging();
UnitTestSchedulingContext cntx = new UnitTestSchedulingContext();
OrleansTaskScheduler scheduler = TestInternalHelper.InitializeSchedulerForTesting(cntx);
int expected = 2;
bool done = false;
Task<int> t = new Task<int>(() => { done = true; return expected; });
t.Start(scheduler);
int received = t.Result;
Assert.True(t.IsCompleted, "Task should have completed");
Assert.False(t.IsFaulted, "Task should not thrown exception: " + t.Exception);
Assert.True(done, "Task should be done");
Assert.Equal(expected, received);
}
public void Async_Task_Start_ActivationTaskScheduler()
{
InitSchedulerLogging();
UnitTestSchedulingContext cntx = new UnitTestSchedulingContext();
OrleansTaskScheduler masterScheduler = TestInternalHelper.InitializeSchedulerForTesting(cntx);
ActivationTaskScheduler activationScheduler = masterScheduler.GetWorkItemGroup(cntx).TaskRunner;
int expected = 2;
bool done = false;
Task<int> t = new Task<int>(() => { done = true; return expected; });
t.Start(activationScheduler);
int received = t.Result;
Assert.IsTrue(t.IsCompleted, "Task should have completed");
Assert.IsFalse(t.IsFaulted, "Task should not thrown exception: " + t.Exception);
Assert.IsTrue(done, "Task should be done");
Assert.AreEqual(expected, received, "Task did not return expected value " + expected);
}
public void Sched_Task_TplFifoTest_TaskScheduler()
{
UnitTestSchedulingContext cntx = new UnitTestSchedulingContext();
OrleansTaskScheduler scheduler = TestInternalHelper.InitializeSchedulerForTesting(cntx);
ActivationTaskScheduler activationScheduler = scheduler.GetWorkItemGroup(cntx).TaskRunner;
int n = 0;
// ReSharper disable AccessToModifiedClosure
Task task1 = new Task(() => { Thread.Sleep(1000); n = n + 5; });
Task task2 = new Task(() => { n = n * 3; });
// ReSharper restore AccessToModifiedClosure
// By queuuing to ActivationTaskScheduler we guarantee single threaded ordered execution.
// If we queued to OrleansTaskScheduler we would not guarantee that.
task1.Start(activationScheduler);
task2.Start(activationScheduler);
// Pause to let things run
Thread.Sleep(TimeSpan.FromSeconds(2));
// N should be 15, because the two tasks should execute in order
Assert.True(n != 0, "Work items did not get executed");
Assert.Equal(15, n);
}
public void Task_OrleansTaskScheduler()
{
string testName = "Task_OrleansTaskScheduler";
var baseline = DoBaseTestRun(testName + "-Baseline", numTasks);
var tasks = new List<Task>(numTasks);
UnitTestSchedulingContext rootContext = new UnitTestSchedulingContext();
TaskScheduler taskScheduler = TestInternalHelper.InitializeSchedulerForTesting(rootContext);
QueuedTaskSchedulerTests_Set1.TimeRun(1, baseline, testName, output, () =>
{
for (int i = 0; i < numTasks; i++)
{
string context = i.ToString();
Task t = CreateTask(i, context);
t.Start(taskScheduler);
tasks.Add(t);
}
Task.WaitAll(tasks.ToArray());
});
foreach (Task t in tasks)
{
Assert.IsTrue(t.IsCompleted, "Task is completed");
Assert.IsFalse(t.IsFaulted, "Task did not fault");
Assert.IsNull(t.Exception, "Task did not return an Exception");
}
}
public async Task Sched_AC_Turn_Execution_Order()
{
// Can we add a unit test that basicaly checks that any turn is indeed run till completion before any other turn?
// For example, you have a long running main turn and in the middle it spawns a lot of short CWs (on Done promise) and StartNew.
// You test that no CW/StartNew runs until the main turn is fully done. And run in stress.
UnitTestSchedulingContext context = new UnitTestSchedulingContext();
orleansTaskScheduler = TestHelper.InitializeSchedulerForTesting(context);
var result1 = new TaskCompletionSource<bool>();
var result2 = new TaskCompletionSource<bool>();
orleansTaskScheduler.QueueWorkItem(new ClosureWorkItem(() =>
{
mainDone = false;
stageNum1 = stageNum2 = 0;
Task task1 = Task.Factory.StartNew(() => SubProcess1(11));
Task task2 = task1.ContinueWith((_) => SubProcess1(12));
Task task3 = task2.ContinueWith((_) => SubProcess1(13));
Task task4 = task3.ContinueWith((_) => { SubProcess1(14); result1.SetResult(true); });
task4.Ignore();
Task task21 = TaskDone.Done.ContinueWith((_) => SubProcess2(21));
Task task22 = task21.ContinueWith((_) => { SubProcess2(22); result2.SetResult(true); });
task22.Ignore();
Thread.Sleep(TimeSpan.FromSeconds(1));
mainDone = true;
}), context);
try { await result1.Task.WithTimeout(TimeSpan.FromSeconds(3)); }
catch (TimeoutException) { Assert.Fail("Timeout-1"); }
try { await result2.Task.WithTimeout(TimeSpan.FromSeconds(3)); }
catch (TimeoutException) { Assert.Fail("Timeout-2"); }
Assert.AreNotEqual(0, stageNum1, "Work items did not get executed-1");
Assert.AreNotEqual(0, stageNum2, "Work items did not get executed-2");
Assert.AreEqual(14, stageNum1, "Work items executed out of order-1");
Assert.AreEqual(22, stageNum2, "Work items executed out of order-2");
}
请发表评论