public void Should_Execute_Tasks_Sequentially() { var poolExec = new DotNetThreadPoolUnitOfExecution(); ISequencer sequencer = this.BuildSequencer(poolExec); var context = new RaceConditionDetector(); sequencer.Dispatch(() => context.Delay(20)); int current = 0; bool failed = false; for (int i = 0; i < 1000; i++) { int targetCount = i; sequencer.Dispatch( () => { // we check if the task is executed at the proper rank if (targetCount != current) { failed = true; } current++; }); } Check.That(context.WaitForTasks(1)).IsFalse(); Check.That(failed).IsFalse(); }
public void Sequencer_should_process_fairly() { var factory = new UnitOfExecutionsFactory(); var thread = factory.GetDedicatedThread(); var sequencer = this.BuildSequencer(thread); var context = new RaceConditionDetector(); // we inject delay deliberately, to ensure queueing happens sequencer.Dispatch(() => context.Delay(20)); var current = 0; var failed = false; for (var i = 0; i < 1000; i++) { var targetCount = i; var executor = ((i % 2) == 0) ? thread : (IUnitOfExecution)sequencer; executor.Dispatch( () => { // we check if the task is executed at the proper rank if (targetCount != current) { failed = true; } current++; }); } //thread.Dispose(); Check.That(context.WaitForTasks(1)).IsFalse(); Check.That(failed).IsFalse(); }
public void Should_Execute_Tasks_Non_Concurrently() { var poolExec = new DotNetThreadPoolUnitOfExecution(); ISequencer sequencer = this.BuildSequencer(poolExec); var context = new RaceConditionDetector(); // first task inject delay sequencer.Dispatch(() => context.Delay(20)); // second task check non concurrence sequencer.Dispatch(() => context.Delay(20)); // wait for the two tasks to be executed context.WaitForTasks(2); Check.That(context.RaceConditionDetected).IsFalse(); }