本文整理汇总了C#中UnitTests.SchedulerTests.UnitTestSchedulingContext类的典型用法代码示例。如果您正苦于以下问题:C# UnitTestSchedulingContext类的具体用法?C# UnitTestSchedulingContext怎么用?C# UnitTestSchedulingContext使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
UnitTestSchedulingContext类属于UnitTests.SchedulerTests命名空间,在下文中一共展示了UnitTestSchedulingContext类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: OrleansTaskSchedulerAdvancedTests_Set2
public OrleansTaskSchedulerAdvancedTests_Set2(ITestOutputHelper output)
{
this.output = output;
OrleansTaskSchedulerBasicTests.InitSchedulerLogging();
context = new UnitTestSchedulingContext();
masterScheduler = TestInternalHelper.InitializeSchedulerForTesting(context);
}
示例2: Sched_AC_Test
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");
}
示例3: Sched_AC_WaitTest
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");
}
示例4: Async_Task_Start_OrleansTaskScheduler
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);
}
示例5: Async_Task_Start_ActivationTaskScheduler
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);
}
示例6: Sched_Task_ClosureWorkItem_Wait
public void Sched_Task_ClosureWorkItem_Wait()
{
UnitTestSchedulingContext cntx = new UnitTestSchedulingContext();
OrleansTaskScheduler scheduler = TestInternalHelper.InitializeSchedulerForTesting(cntx);
const int NumTasks = 10;
ManualResetEvent[] flags = new ManualResetEvent[NumTasks];
for (int i = 0; i < NumTasks; i++)
{
flags[i] = new ManualResetEvent(false);
}
Task[] tasks = new Task[NumTasks];
for (int i = 0; i < NumTasks; i++)
{
int taskNum = i; // Capture
tasks[i] = new Task(() => { output.WriteLine("Inside Task-" + taskNum); flags[taskNum].WaitOne(); });
}
ClosureWorkItem[] workItems = new ClosureWorkItem[NumTasks];
for (int i = 0; i < NumTasks; i++)
{
int taskNum = i; // Capture
workItems[i] = new ClosureWorkItem(() =>
{
output.WriteLine("Inside ClosureWorkItem-" + taskNum);
tasks[taskNum].Start(scheduler);
bool ok = tasks[taskNum].Wait(TimeSpan.FromMilliseconds(NumTasks * 100));
Assert.True(ok, "Wait completed successfully inside ClosureWorkItem-" + taskNum);
});
}
foreach (var workItem in workItems) scheduler.QueueWorkItem(workItem, cntx);
foreach (var flag in flags) flag.Set();
for (int i = 0; i < tasks.Length; i++)
{
bool ok = tasks[i].Wait(TimeSpan.FromMilliseconds(NumTasks * 150));
Assert.True(ok, "Wait completed successfully for Task-" + i);
}
for (int i = 0; i < tasks.Length; i++)
{
Assert.False(tasks[i].IsFaulted, "Task.IsFaulted-" + i + " Exception=" + tasks[i].Exception);
Assert.True(tasks[i].IsCompleted, "Task.IsCompleted-" + i);
}
}
示例7: Sched_Task_StartTask_Wait_Wrapped
public void Sched_Task_StartTask_Wait_Wrapped()
{
UnitTestSchedulingContext cntx = new UnitTestSchedulingContext();
OrleansTaskScheduler scheduler = TestInternalHelper.InitializeSchedulerForTesting(cntx);
const int NumTasks = 100;
ManualResetEvent[] flags = new ManualResetEvent[NumTasks];
for (int i = 0; i < NumTasks; i++)
{
flags[i] = new ManualResetEvent(false);
}
Task[] tasks = new Task[NumTasks];
for (int i = 0; i < NumTasks; i++)
{
int taskNum = i; // Capture
tasks[i] = new Task(() => { output.WriteLine("Inside Task-" + taskNum); flags[taskNum].WaitOne(); });
output.WriteLine("Created Task-" + taskNum + " Id=" + tasks[taskNum].Id);
}
Task[] wrappers = new Task[NumTasks];
for (int i = 0; i < NumTasks; i++)
{
int taskNum = i; // Capture
wrappers[i] = new Task(() =>
{
output.WriteLine("Inside Wrapper-" + taskNum);
tasks[taskNum].Start(scheduler);
});
wrappers[i].ContinueWith(t =>
{
Assert.False(t.IsFaulted, "Warpper.IsFaulted-" + taskNum + " " + t.Exception);
Assert.True(t.IsCompleted, "Wrapper.IsCompleted-" + taskNum);
});
output.WriteLine("Created Wrapper-" + taskNum + " Task.Id=" + wrappers[taskNum].Id);
}
foreach (var wrapper in wrappers) wrapper.Start(scheduler);
foreach (var flag in flags) flag.Set();
for (int i = 0; i < wrappers.Length; i++)
{
bool ok = wrappers[i].Wait(TimeSpan.FromMilliseconds(NumTasks * 150 * 2));
Assert.True(ok, "Wait completed successfully for Wrapper-" + i);
}
for (int i = 0; i < tasks.Length; i++)
{
bool ok = tasks[i].Wait(TimeSpan.FromMilliseconds(NumTasks * 150 * 2));
Assert.True(ok, "Wait completed successfully for Task-" + i);
Assert.False(tasks[i].IsFaulted, "Task.IsFaulted-" + i + " " + tasks[i].Exception);
Assert.True(tasks[i].IsCompleted, "Task.IsCompleted-" + i);
}
}
示例8: Sched_Task_StartTask_Wrapped
public void Sched_Task_StartTask_Wrapped()
{
UnitTestSchedulingContext cntx = new UnitTestSchedulingContext();
OrleansTaskScheduler scheduler = TestInternalHelper.InitializeSchedulerForTesting(cntx);
ManualResetEvent pause1 = new ManualResetEvent(false);
ManualResetEvent pause2 = new ManualResetEvent(false);
Task task1 = new Task(() => { pause1.WaitOne(); output.WriteLine("Task-1"); });
Task task2 = new Task(() => { pause2.WaitOne(); output.WriteLine("Task-2"); });
Task wrapper1 = new Task(() =>
{
task1.Start(scheduler);
bool ok = task1.Wait(TimeSpan.FromMilliseconds(100));
if (!ok) throw new TimeoutException();
});
Task wrapper2 = new Task(() =>
{
task2.Start(scheduler);
bool ok = task2.Wait(TimeSpan.FromMilliseconds(100));
if (!ok) throw new TimeoutException();
});
pause1.Set();
wrapper1.Start(scheduler);
bool ok1 = wrapper1.Wait(TimeSpan.FromMilliseconds(1000));
if (!ok1) throw new TimeoutException();
Assert.True(task1.IsCompleted, "Task.IsCompleted-1");
Assert.False(task1.IsFaulted, "Task.IsFaulted-1");
wrapper2.Start(scheduler);
pause2.Set();
bool finished = wrapper2.Wait(TimeSpan.FromMilliseconds(100));
if (!finished) throw new TimeoutException();
Assert.True(task2.IsCompleted, "Task.IsCompleted-2");
Assert.False(task2.IsFaulted, "Task.IsFaulted-2");
}
示例9: Sched_Task_TplFifoTest_TaskScheduler
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);
}
示例10: Task_OrleansTaskScheduler
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");
}
}
示例11: TestInit
public void TestInit()
{
context = new UnitTestSchedulingContext();
masterScheduler = TestInternalHelper.InitializeSchedulerForTesting(context);
}
示例12: Sched_Task_StartNew_ContinueWith_TaskScheduler
public void Sched_Task_StartNew_ContinueWith_TaskScheduler()
{
UnitTestSchedulingContext rootContext = new UnitTestSchedulingContext();
OrleansTaskScheduler scheduler = TestInternalHelper.InitializeSchedulerForTesting(rootContext);
output.WriteLine("#0 - StartNew - SynchronizationContext.Current={0} TaskScheduler.Current={1}",
SynchronizationContext.Current, TaskScheduler.Current);
Task t0 = Task.Factory.StartNew(state =>
{
output.WriteLine("#1 - StartNew - SynchronizationContext.Current={0} TaskScheduler.Current={1}",
SynchronizationContext.Current, TaskScheduler.Current);
Assert.Equal(scheduler, TaskScheduler.Current); // "TaskScheduler.Current #1"
}, null, CancellationToken.None, TaskCreationOptions.None, scheduler);
Task t1 = t0.ContinueWith(task =>
{
Assert.False(task.IsFaulted, "Task #1 Faulted=" + task.Exception);
output.WriteLine("#2 - StartNew - SynchronizationContext.Current={0} TaskScheduler.Current={1}",
SynchronizationContext.Current, TaskScheduler.Current);
Assert.Equal(scheduler, TaskScheduler.Current); // "TaskScheduler.Current #2"
}, scheduler);
bool ok = t1.Wait(TimeSpan.FromSeconds(30));
if (!ok) throw new TimeoutException();
}
示例13: Sched_Task_SubTaskExecutionSequencing
public void Sched_Task_SubTaskExecutionSequencing()
{
UnitTestSchedulingContext rootContext = new UnitTestSchedulingContext();
OrleansTaskScheduler scheduler = TestInternalHelper.InitializeSchedulerForTesting(rootContext);
UnitTestSchedulingContext context = new UnitTestSchedulingContext();
scheduler.RegisterWorkContext(context);
LogContext("Main-task " + Task.CurrentId);
int n = 0;
Action closure = () =>
{
LogContext("ClosureWorkItem-task " + Task.CurrentId);
for (int i = 0; i < 10; i++)
{
int id = -1;
Action action = () =>
{
id = Task.CurrentId.HasValue ? (int)Task.CurrentId : -1;
// ReSharper disable AccessToModifiedClosure
LogContext("Sub-task " + id + " n=" + n);
int k = n;
output.WriteLine("Sub-task " + id + " sleeping");
Thread.Sleep(100);
output.WriteLine("Sub-task " + id + " awake");
n = k + 1;
// ReSharper restore AccessToModifiedClosure
};
Task.Factory.StartNew(action).ContinueWith(tsk =>
{
LogContext("Sub-task " + id + "-ContinueWith");
output.WriteLine("Sub-task " + id + " Done");
});
}
};
IWorkItem workItem = new ClosureWorkItem(closure);
scheduler.QueueWorkItem(workItem, context);
// Pause to let things run
output.WriteLine("Main-task sleeping");
Thread.Sleep(TimeSpan.FromSeconds(2));
output.WriteLine("Main-task awake");
// N should be 10, because all tasks should execute serially
Assert.True(n != 0, "Work items did not get executed");
Assert.Equal(10, n); // "Work items executed concurrently"
scheduler.Stop();
}
示例14: Sched_AC_ContinueWith_1_Test
public void Sched_AC_ContinueWith_1_Test()
{
UnitTestSchedulingContext context = new UnitTestSchedulingContext();
orleansTaskScheduler = TestHelper.InitializeSchedulerForTesting(context);
var result = new TaskCompletionSource<bool>();
int n = 0;
// ReSharper disable AccessToModifiedClosure
orleansTaskScheduler.QueueWorkItem(new ClosureWorkItem(() =>
{
Task task1 = Task.Factory.StartNew(() => { Console.WriteLine("===> 1a"); Thread.Sleep(OneSecond); n = n + 3; Console.WriteLine("===> 1b"); });
Task task2 = task1.ContinueWith((_) => { n = n * 5; Console.WriteLine("===> 2"); });
Task task3 = task2.ContinueWith((_) => { n = n / 5; Console.WriteLine("===> 3"); });
Task task4 = task3.ContinueWith((_) => { n = n - 2; Console.WriteLine("===> 4"); result.SetResult(true); });
task4.Ignore();
}), context);
// ReSharper restore AccessToModifiedClosure
Assert.IsTrue(result.Task.Wait(TwoSeconds));
Assert.IsTrue(n != 0, "Work items did not get executed");
Assert.AreEqual(1, n, "Work items executed out of order");
}
示例15: Sched_Task_NewTask_ContinueWith_Wrapped_OrleansTaskScheduler
public void Sched_Task_NewTask_ContinueWith_Wrapped_OrleansTaskScheduler()
{
UnitTestSchedulingContext rootContext = new UnitTestSchedulingContext();
OrleansTaskScheduler scheduler = TestHelper.InitializeSchedulerForTesting(rootContext);
Task wrapped = new Task(() =>
{
Console.WriteLine("#0 - new Task - SynchronizationContext.Current={0} TaskScheduler.Current={1}",
SynchronizationContext.Current, TaskScheduler.Current);
Task t0 = new Task(() =>
{
Console.WriteLine("#1 - new Task - SynchronizationContext.Current={0} TaskScheduler.Current={1}",
SynchronizationContext.Current, TaskScheduler.Current);
Assert.AreEqual(scheduler, TaskScheduler.Current, "TaskScheduler.Current #1");
});
Task t1 = t0.ContinueWith(task =>
{
Assert.IsFalse(task.IsFaulted, "Task #1 Faulted=" + task.Exception);
Console.WriteLine("#2 - new Task - SynchronizationContext.Current={0} TaskScheduler.Current={1}",
SynchronizationContext.Current, TaskScheduler.Current);
Assert.AreEqual(scheduler, TaskScheduler.Current, "TaskScheduler.Current #2");
});
t0.Start(scheduler);
bool ok = t1.Wait(TimeSpan.FromSeconds(15));
if (!ok) throw new TimeoutException();
});
wrapped.Start(scheduler);
bool finished = wrapped.Wait(TimeSpan.FromSeconds(30));
if (!finished) throw new TimeoutException();
}