public OrleansTaskSchedulerAdvancedTests_Set2(ITestOutputHelper output) { this.output = output; OrleansTaskSchedulerBasicTests.InitSchedulerLogging(); context = new UnitTestSchedulingContext(); masterScheduler = TestInternalHelper.InitializeSchedulerForTesting(context); }
public void Sched_Task_TplFifoTest() { // This is not a great test because there's a 50/50 shot that it will work even if the scheduling // is completely and thoroughly broken and both closures are executed "simultaneously" UnitTestSchedulingContext context = new UnitTestSchedulingContext(); OrleansTaskScheduler orleansTaskScheduler = TestInternalHelper.InitializeSchedulerForTesting(context); ActivationTaskScheduler scheduler = orleansTaskScheduler.GetWorkItemGroup(context).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 task1.Start(scheduler); task2.Start(scheduler); // Pause to let things run Thread.Sleep(2000); // 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); output.WriteLine("Test executed OK."); orleansTaskScheduler.Stop(); }
public void Sched_AC_ContinueWith_1_Test() { UnitTestSchedulingContext context = new UnitTestSchedulingContext(); this.orleansTaskScheduler = TestInternalHelper.InitializeSchedulerForTesting(context, this.loggerFactory); var result = new TaskCompletionSource <bool>(); int n = 0; // ReSharper disable AccessToModifiedClosure this.orleansTaskScheduler.QueueAction(() => { Task task1 = Task.Factory.StartNew(() => { this.output.WriteLine("===> 1a"); Thread.Sleep(OneSecond); n = n + 3; this.output.WriteLine("===> 1b"); }); Task task2 = task1.ContinueWith((_) => { n = n * 5; this.output.WriteLine("===> 2"); }); Task task3 = task2.ContinueWith((_) => { n = n / 5; this.output.WriteLine("===> 3"); }); Task task4 = task3.ContinueWith((_) => { n = n - 2; this.output.WriteLine("===> 4"); result.SetResult(true); }); task4.Ignore(); }, context); // ReSharper restore AccessToModifiedClosure Assert.True(result.Task.Wait(TwoSeconds)); Assert.True(n != 0, "Work items did not get executed"); Assert.Equal(1, n); // "Work items executed out of order" }
public void Sched_AC_Test() { int n = 0; bool insideTask = false; UnitTestSchedulingContext context = new UnitTestSchedulingContext(); this.orleansTaskScheduler = TestInternalHelper.InitializeSchedulerForTesting(context, this.performanceMetrics, this.loggerFactory); this.output.WriteLine("Running Main in Context=" + RuntimeContext.Current); this.orleansTaskScheduler.QueueWorkItem(new ClosureWorkItem(() => { for (int i = 0; i < 10; i++) { Task.Factory.StartNew(() => { // ReSharper disable AccessToModifiedClosure this.output.WriteLine("Starting " + i + " in Context=" + RuntimeContext.Current); Assert.False(insideTask, $"Starting new task when I am already inside task of iteration {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.True(n != 0, "Work items did not get executed"); Assert.Equal(10, n); // "Work items executed concurrently" }
public void Sched_Task_JoinAll() { var result = new TaskCompletionSource <bool>(); int n = 0; Task <int>[] tasks = null; Stopwatch stopwatch = new Stopwatch(); stopwatch.Start(); UnitTestSchedulingContext context = new UnitTestSchedulingContext(); this.orleansTaskScheduler = TestInternalHelper.InitializeSchedulerForTesting(context, this.loggerFactory); // ReSharper disable AccessToModifiedClosure this.orleansTaskScheduler.QueueAction(() => { Task <int> task1 = Task <int> .Factory.StartNew(() => { this.output.WriteLine("===> 1a"); Thread.Sleep(OneSecond); n = n + 3; this.output.WriteLine("===> 1b"); return(1); }); Task <int> task2 = Task <int> .Factory.StartNew(() => { this.output.WriteLine("===> 2a"); Thread.Sleep(OneSecond); n = n + 3; this.output.WriteLine("===> 2b"); return(2); }); Task <int> task3 = Task <int> .Factory.StartNew(() => { this.output.WriteLine("===> 3a"); Thread.Sleep(OneSecond); n = n + 3; this.output.WriteLine("===> 3b"); return(3); }); Task <int> task4 = Task <int> .Factory.StartNew(() => { this.output.WriteLine("===> 4a"); Thread.Sleep(OneSecond); n = n + 3; this.output.WriteLine("===> 4b"); return(4); }); tasks = new Task <int>[] { task1, task2, task3, task4 }; result.SetResult(true); }, context); // ReSharper restore AccessToModifiedClosure Assert.True(result.Task.Wait(TwoSeconds)); // Wait for main (one that creates tasks) work item to finish. var promise = Task <int[]> .Factory.ContinueWhenAll(tasks, (res) => { List <int> output = new List <int>(); int taskNum = 1; foreach (var t in tasks) { Assert.True(t.IsCompleted, "Sub-Task completed"); Assert.False(t.IsFaulted, "Sub-Task faulted: " + t.Exception); var val = t.Result; Assert.Equal(taskNum, val); // "Value returned by Task " + taskNum output.Add(val); taskNum++; } int[] results = output.ToArray(); return(results); }); bool ok = promise.Wait(TimeSpan.FromSeconds(8)); if (!ok) { throw new TimeoutException(); } Assert.True(n != 0, "Work items did not get executed"); Assert.Equal(12, n); // "Not all work items executed" long ms = stopwatch.ElapsedMilliseconds; Assert.True(4000 <= ms && ms <= 5000, "Wait time out of range, expected between 4000 and 5000 milliseconds, was " + ms); }
public OrleansTaskSchedulerAdvancedTests_Set2(ITestOutputHelper output) { this.output = output; this.loggerFactory = OrleansTaskSchedulerBasicTests.InitSchedulerLogging(); this.context = new UnitTestSchedulingContext(); this.performanceMetrics = new SiloPerformanceMetrics(new NoOpHostEnvironmentStatistics(this.loggerFactory), new AppEnvironmentStatistics(), this.loggerFactory, Options.Create<SiloStatisticsOptions>(new SiloStatisticsOptions())); this.masterScheduler = TestInternalHelper.InitializeSchedulerForTesting(this.context, this.performanceMetrics, this.loggerFactory); }
public async Task OrleansSched_Test1() { UnitTestSchedulingContext context = new UnitTestSchedulingContext(); OrleansTaskScheduler orleansTaskScheduler = TestInternalHelper.InitializeSchedulerForTesting(context, this.loggerFactory); ActivationTaskScheduler scheduler = orleansTaskScheduler.GetWorkItemGroup(context).TaskScheduler; await Run_ActivationSched_Test1(scheduler, false); }
public async Task OrleansSched_Test1_Bounce() { UnitTestSchedulingContext context = new UnitTestSchedulingContext(); OrleansTaskScheduler orleansTaskScheduler = TestInternalHelper.InitializeSchedulerForTesting(context); ActivationTaskScheduler scheduler = orleansTaskScheduler.GetWorkItemGroup(context).TaskRunner; await Run_ActivationSched_Test1(scheduler, true); }
public OrleansTaskSchedulerAdvancedTests_Set2(ITestOutputHelper output) { this.output = output; this.loggerFactory = OrleansTaskSchedulerBasicTests.InitSchedulerLogging(); this.context = new UnitTestSchedulingContext(); this.performanceMetrics = new TestHooksHostEnvironmentStatistics(); this.masterScheduler = TestInternalHelper.InitializeSchedulerForTesting(this.context, this.loggerFactory); }
public OrleansTaskSchedulerBasicTests(ITestOutputHelper output) { this.output = output; SynchronizationContext.SetSynchronizationContext(null); this.loggerFactory = InitSchedulerLogging(); this.rootContext = new UnitTestSchedulingContext(); this.scheduler = TestInternalHelper.InitializeSchedulerForTesting(this.rootContext, this.loggerFactory); }
public OrleansTaskSchedulerBasicTests(ITestOutputHelper output) { this.output = output; SynchronizationContext.SetSynchronizationContext(null); this.loggerFactory = InitSchedulerLogging(); this.performanceMetrics = new SiloPerformanceMetrics(new NoOpHostEnvironmentStatistics(this.loggerFactory), new AppEnvironmentStatistics(), this.loggerFactory, Options.Create <LoadSheddingOptions>(new LoadSheddingOptions())); this.rootContext = new UnitTestSchedulingContext(); this.scheduler = TestInternalHelper.InitializeSchedulerForTesting(this.rootContext, this.performanceMetrics, this.loggerFactory); }
public OrleansTaskSchedulerAdvancedTests_Set2(ITestOutputHelper output) { this.output = output; loggerFactory = OrleansTaskSchedulerBasicTests.InitSchedulerLogging(); context = new UnitTestSchedulingContext(); this.runtimeStatisticsGroup = new RuntimeStatisticsGroup(loggerFactory); this.performanceMetrics = new SiloPerformanceMetrics(this.runtimeStatisticsGroup, this.loggerFactory); masterScheduler = TestInternalHelper.InitializeSchedulerForTesting(context, this.performanceMetrics, loggerFactory); }
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; Console.WriteLine("Sub-task " + id + " sleeping"); Thread.Sleep(100); Console.WriteLine("Sub-task " + id + " awake"); n = k + 1; // ReSharper restore AccessToModifiedClosure }; Task.Factory.StartNew(action).ContinueWith(tsk => { LogContext("Sub-task " + id + "-ContinueWith"); Console.WriteLine("Sub-task " + id + " Done"); }); } }; IWorkItem workItem = new ClosureWorkItem(closure); scheduler.QueueWorkItem(workItem, context); // Pause to let things run Console.WriteLine("Main-task sleeping"); Thread.Sleep(TimeSpan.FromSeconds(2)); Console.WriteLine("Main-task awake"); // 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"); scheduler.Stop(); }
public OrleansTaskSchedulerBasicTests(ITestOutputHelper output) { this.output = output; SynchronizationContext.SetSynchronizationContext(null); this.loggerFactory = InitSchedulerLogging(); this.performanceMetrics = new TestHooksHostEnvironmentStatistics(); this.rootContext = new UnitTestSchedulingContext(); this.scheduler = TestInternalHelper.InitializeSchedulerForTesting(this.rootContext, this.performanceMetrics, this.loggerFactory); }
public void Sched_AC_MainTurnWait_Test() { this.orleansTaskScheduler = TestInternalHelper.InitializeSchedulerForTesting(new UnitTestSchedulingContext(), this.performanceMetrics, this.loggerFactory); var promise = Task.Factory.StartNew(() => { Thread.Sleep(1000); }); promise.Wait(); }
public OrleansTaskSchedulerBasicTests(ITestOutputHelper output) { this.output = output; SynchronizationContext.SetSynchronizationContext(null); loggerFactory = InitSchedulerLogging(); this.runtimeStatisticsGroup = new RuntimeStatisticsGroup(loggerFactory); this.performanceMetrics = new SiloPerformanceMetrics(this.runtimeStatisticsGroup, this.loggerFactory); this.rootContext = new UnitTestSchedulingContext(); this.scheduler = TestInternalHelper.InitializeSchedulerForTesting(rootContext, this.performanceMetrics, loggerFactory); }
public async Task Sched_Stopped_WorkItemGroup() { var context = new UnitTestSchedulingContext(); var scheduler = this.orleansTaskScheduler = TestInternalHelper.InitializeSchedulerForTesting(context, this.loggerFactory); WorkItemGroup workItemGroup = this.orleansTaskScheduler.GetWorkItemGroup(context); void CheckScheduler(object state) { Assert.IsType <string>(state); Assert.Equal("some state", state as string); Assert.IsType <ActivationTaskScheduler>(TaskScheduler.Current); } Task <Task> ScheduleTask() => Task.Factory.StartNew( state => { CheckScheduler(state); return(Task.Factory.StartNew( async s => { CheckScheduler(s); await Task.Delay(50); CheckScheduler(s); }, state).Unwrap()); }, "some state", CancellationToken.None, TaskCreationOptions.DenyChildAttach, workItemGroup.TaskScheduler); // Check that the WorkItemGroup is functioning. await await ScheduleTask(); workItemGroup.Stop(); var taskAfterStopped = ScheduleTask(); var resultTask = await Task.WhenAny(taskAfterStopped, Task.Delay(TimeSpan.FromSeconds(10))); Assert.Same(taskAfterStopped, resultTask); await await taskAfterStopped; // Wait for the WorkItemGroup to upgrade the warning to an error and try again. // This delay is based upon SchedulingOptions.StoppedActivationWarningInterval. await Task.Delay(TimeSpan.FromMilliseconds(300)); taskAfterStopped = ScheduleTask(); resultTask = await Task.WhenAny(taskAfterStopped, Task.Delay(TimeSpan.FromSeconds(10))); Assert.Same(taskAfterStopped, resultTask); await await taskAfterStopped; }
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.IsFalse(t.IsFaulted, "Warpper.IsFaulted-" + taskNum + " " + t.Exception); Assert.IsTrue(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.IsTrue(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.IsTrue(ok, "Wait completed successfully for Task-" + i); Assert.IsFalse(tasks[i].IsFaulted, "Task.IsFaulted-" + i + " " + tasks[i].Exception); Assert.IsTrue(tasks[i].IsCompleted, "Task.IsCompleted-" + i); } }
public async Task Sched_AC_WaitTest() { int n = 0; bool insideTask = false; UnitTestSchedulingContext context = new UnitTestSchedulingContext(); this.orleansTaskScheduler = TestInternalHelper.InitializeSchedulerForTesting(context, this.loggerFactory); var result = new TaskCompletionSource <bool>(); this.orleansTaskScheduler.QueueAction(() => { var task1 = Task.Factory.StartNew(() => { this.output.WriteLine("Starting 1"); Assert.False(insideTask, $"Starting new task when I am already inside task of iteration {n}"); insideTask = true; this.output.WriteLine("===> 1a"); Thread.Sleep(1000); n = n + 3; this.output.WriteLine("===> 1b"); insideTask = false; }); var task2 = Task.Factory.StartNew(() => { this.output.WriteLine("Starting 2"); Assert.False(insideTask, $"Starting new task when I am already inside task of iteration {n}"); insideTask = true; this.output.WriteLine("===> 2a"); task1.Wait(); this.output.WriteLine("===> 2b"); n = n * 5; this.output.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.True(false, "Result did not arrive before timeout " + timeoutLimit); } Assert.True(n != 0, "Work items did not get executed"); Assert.Equal(15, n); // "Work items executed out of order" }
public DhtGrainLocatorTests(ITestOutputHelper output) { this.output = output; this.loggerFactory = new LoggerFactory(new[] { new XunitLoggerProvider(output) }); this.rootContext = new UnitTestSchedulingContext(); this.taskScheduler = TestInternalHelper.InitializeSchedulerForTesting(this.rootContext, this.loggerFactory); this.localGrainDirectory = new MockLocalGrainDirectory( TimeSpan.FromMilliseconds(100), TimeSpan.FromMilliseconds(200)); this.target = new DhtGrainLocator(this.localGrainDirectory, this.taskScheduler, this.rootContext); }
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(() => { Console.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(() => { Console.WriteLine("Inside ClosureWorkItem-" + taskNum); tasks[taskNum].Start(scheduler); bool ok = tasks[taskNum].Wait(TimeSpan.FromMilliseconds(NumTasks * 100)); Assert.IsTrue(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.IsTrue(ok, "Wait completed successfully for Task-" + i); } for (int i = 0; i < tasks.Length; i++) { Assert.IsFalse(tasks[i].IsFaulted, "Task.IsFaulted-" + i + " Exception=" + tasks[i].Exception); Assert.IsTrue(tasks[i].IsCompleted, "Task.IsCompleted-" + i); } }
public void Sched_Task_RequestContext_NewTask_ContinueWith() { UnitTestSchedulingContext rootContext = new UnitTestSchedulingContext(); OrleansTaskScheduler scheduler = TestInternalHelper.InitializeSchedulerForTesting(rootContext); const string key = "K"; int val = TestConstants.random.Next(); RequestContext.Set(key, val); Console.WriteLine("Initial - SynchronizationContext.Current={0} TaskScheduler.Current={1} Thread={2}", SynchronizationContext.Current, TaskScheduler.Current, Thread.CurrentThread.ManagedThreadId); Assert.AreEqual(val, RequestContext.Get(key), "RequestContext.Get Initial"); Task t0 = new Task(() => { Console.WriteLine("#0 - new Task - SynchronizationContext.Current={0} TaskScheduler.Current={1} Thread={2}", SynchronizationContext.Current, TaskScheduler.Current, Thread.CurrentThread.ManagedThreadId); Assert.AreEqual(val, RequestContext.Get(key), "RequestContext.Get #0"); Task t1 = new Task(() => { Console.WriteLine("#1 - new Task - SynchronizationContext.Current={0} TaskScheduler.Current={1} Thread={2}", SynchronizationContext.Current, TaskScheduler.Current, Thread.CurrentThread.ManagedThreadId); Assert.AreEqual(val, RequestContext.Get(key), "RequestContext.Get #1"); }); Task t2 = t1.ContinueWith(task => { Assert.IsFalse(task.IsFaulted, "Task #1 FAULTED=" + task.Exception); Console.WriteLine("#2 - new Task - SynchronizationContext.Current={0} TaskScheduler.Current={1} Thread={2}", SynchronizationContext.Current, TaskScheduler.Current, Thread.CurrentThread.ManagedThreadId); Assert.AreEqual(val, RequestContext.Get(key), "RequestContext.Get #2"); }); t1.Start(scheduler); bool ok = t2.Wait(TimeSpan.FromSeconds(5)); if (!ok) { throw new TimeoutException(); } }); t0.Start(scheduler); bool finished = t0.Wait(TimeSpan.FromSeconds(10)); if (!finished) { throw new TimeoutException(); } Assert.IsFalse(t0.IsFaulted, "Task #0 FAULTED=" + t0.Exception); }
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(); Console.WriteLine("Task-1"); }); Task task2 = new Task(() => { pause2.WaitOne(); Console.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.IsTrue(task1.IsCompleted, "Task.IsCompleted-1"); Assert.IsFalse(task1.IsFaulted, "Task.IsFaulted-1"); wrapper2.Start(scheduler); pause2.Set(); bool finished = wrapper2.Wait(TimeSpan.FromMilliseconds(100)); if (!finished) { throw new TimeoutException(); } Assert.IsTrue(task2.IsCompleted, "Task.IsCompleted-2"); Assert.IsFalse(task2.IsFaulted, "Task.IsFaulted-2"); }
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.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_ClosureWorkItem_SpecificScheduler() { UnitTestSchedulingContext context = new UnitTestSchedulingContext(); OrleansTaskScheduler scheduler = TestInternalHelper.InitializeSchedulerForTesting(context); ActivationTaskScheduler activationScheduler = scheduler.GetWorkItemGroup(context).TaskRunner; var result0 = new TaskCompletionSource <bool>(); var result1 = new TaskCompletionSource <bool>(); Task t1 = null; scheduler.QueueWorkItem(new ClosureWorkItem(() => { try { Console.WriteLine("#0 - TaskWorkItem - SynchronizationContext.Current={0} TaskScheduler.Current={1}", SynchronizationContext.Current, TaskScheduler.Current); Assert.AreEqual(activationScheduler, TaskScheduler.Current, "TaskScheduler.Current #0"); t1 = 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"); result1.SetResult(true); }); t1.Start(scheduler); result0.SetResult(true); } catch (Exception exc) { result0.SetException(exc); } }), context); result0.Task.Wait(TimeSpan.FromSeconds(1)); Assert.IsTrue(result0.Task.Exception == null, "Task-0 should not throw exception: " + result0.Task.Exception); Assert.IsTrue(result0.Task.Result, "Task-0 completed"); Assert.IsNotNull(t1, "Task-1 started"); result1.Task.Wait(TimeSpan.FromSeconds(1)); Assert.IsTrue(t1.IsCompleted, "Task-1 completed"); Assert.IsFalse(t1.IsFaulted, "Task-1 faulted: " + t1.Exception); Assert.IsTrue(result1.Task.Result, "Task-1 completed"); }
public void Sched_AC_ContinueWith_2_OrleansSched() { this.orleansTaskScheduler = TestInternalHelper.InitializeSchedulerForTesting(new UnitTestSchedulingContext(), this.performanceMetrics, this.loggerFactory); var result1 = new TaskCompletionSource <bool>(); var result2 = new TaskCompletionSource <bool>(); bool failed1 = false; bool failed2 = false; Task task1 = Task.Factory.StartNew(() => { this.output.WriteLine("===> 1a"); Thread.Sleep(OneSecond); throw new ArgumentException(); }); Task task2 = task1.ContinueWith((Task t) => { if (!t.IsFaulted) { this.output.WriteLine("===> 2"); } else { this.output.WriteLine("===> 3"); failed1 = true; result1.SetResult(true); } }); Task task3 = task1.ContinueWith((Task t) => { if (!t.IsFaulted) { this.output.WriteLine("===> 4"); } else { this.output.WriteLine("===> 5"); failed2 = true; result2.SetResult(true); } }); task1.Ignore(); task2.Ignore(); task3.Ignore(); Assert.True(result1.Task.Wait(TwoSeconds), "First ContinueWith did not fire."); Assert.True(result2.Task.Wait(TwoSeconds), "Second ContinueWith did not fire."); Assert.True(failed1); // "First ContinueWith did not fire error handler." Assert.True(failed2); // "Second ContinueWith did not fire error handler." }
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(); this.orleansTaskScheduler = TestInternalHelper.InitializeSchedulerForTesting(context, this.loggerFactory); var result1 = new TaskCompletionSource <bool>(); var result2 = new TaskCompletionSource <bool>(); this.orleansTaskScheduler.QueueAction(() => { this.mainDone = false; this.stageNum1 = this.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 = Task.CompletedTask.ContinueWith((_) => SubProcess2(21)); Task task22 = task21.ContinueWith((_) => { SubProcess2(22); result2.SetResult(true); }); task22.Ignore(); Thread.Sleep(TimeSpan.FromSeconds(1)); this.mainDone = true; }, context); try { await result1.Task.WithTimeout(TimeSpan.FromSeconds(3)); } catch (TimeoutException) { Assert.True(false, "Timeout-1"); } try { await result2.Task.WithTimeout(TimeSpan.FromSeconds(3)); } catch (TimeoutException) { Assert.True(false, "Timeout-2"); } Assert.NotEqual(0, this.stageNum1); // "Work items did not get executed-1" Assert.NotEqual(0, this.stageNum2); // "Work items did not get executed-2" Assert.Equal(14, this.stageNum1); // "Work items executed out of order-1" Assert.Equal(22, this.stageNum2); // "Work items executed out of order-2" }
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.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 Sched_AC_RequestContext_StartNew_ContinueWith() { UnitTestSchedulingContext rootContext = new UnitTestSchedulingContext(); OrleansTaskScheduler scheduler = TestInternalHelper.InitializeSchedulerForTesting(rootContext); const string key = "A"; int val = TestConstants.random.Next(); RequestContext.Set(key, val); Console.WriteLine("Initial - SynchronizationContext.Current={0} TaskScheduler.Current={1}", SynchronizationContext.Current, TaskScheduler.Current); Assert.AreEqual(val, RequestContext.Get(key), "RequestContext.Get Initial"); Task t0 = Task.Factory.StartNew(() => { Console.WriteLine("#0 - new Task - SynchronizationContext.Current={0} TaskScheduler.Current={1}", SynchronizationContext.Current, TaskScheduler.Current); Assert.AreEqual(val, RequestContext.Get(key), "RequestContext.Get #0"); Task t1 = Task.Factory.StartNew(() => { Console.WriteLine("#1 - new Task - SynchronizationContext.Current={0} TaskScheduler.Current={1}", SynchronizationContext.Current, TaskScheduler.Current); Assert.AreEqual(val, RequestContext.Get(key), "RequestContext.Get #1"); }); Task t2 = t1.ContinueWith((_) => { Console.WriteLine("#2 - new Task - SynchronizationContext.Current={0} TaskScheduler.Current={1}", SynchronizationContext.Current, TaskScheduler.Current); Assert.AreEqual(val, RequestContext.Get(key), "RequestContext.Get #2"); }); t2.Wait(TimeSpan.FromSeconds(5)); }); t0.Wait(TimeSpan.FromSeconds(10)); Assert.IsTrue(t0.IsCompleted, "Task #0 FAULTED=" + t0.Exception); }
public void Sched_Task_NewTask_ContinueWith_Wrapped_OrleansTaskScheduler() { UnitTestSchedulingContext rootContext = new UnitTestSchedulingContext(); OrleansTaskScheduler scheduler = TestInternalHelper.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(); } }