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 void RegisterSystemTarget(ISystemTarget target) { var systemTarget = target as SystemTarget; if (systemTarget == null) { throw new ArgumentException($"Parameter must be of type {typeof(SystemTarget)}", nameof(target)); } scheduler.RegisterWorkContext(systemTarget.SchedulingContext); activationDirectory.RecordNewSystemTarget(systemTarget); }
internal static OrleansTaskScheduler InitializeSchedulerForTesting(ISchedulingContext context) { StatisticsCollector.StatisticsCollectionLevel = StatisticsLevel.Info; SchedulerStatisticsGroup.Init(); var scheduler = new OrleansTaskScheduler(4); scheduler.Start(); WorkItemGroup ignore = scheduler.RegisterWorkContext(context); return(scheduler); }
internal void RegisterSystemTarget(SystemTarget target) { scheduler.RegisterWorkContext(target.SchedulingContext); activationDirectory.RecordNewSystemTarget(target); }