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 Factory_should_create_worker_unit() { var factory = new UnitOfExecutionsFactory(); var threadExec = factory.GetDedicatedThread(); Check.That(threadExec).IsNotNull(); }
public void Factory_should_decrease_core_count() { var factory = new UnitOfExecutionsFactory(); var initialCount = factory.AvailableCore; var threadExec = factory.GetDedicatedThread(); Check.That(threadExec).IsNotNull(); Check.That(factory.AvailableCore).IsEqualTo(initialCount - 1); }
public void ShouldHaveANiceAPI() { var factory = new UnitOfExecutionsFactory(); var executor = factory.GetDedicatedThread(); var processed = new List <int>(); var processor = executor.BuildProcessor <int>(processed.Add, false); processor.Post(4); processor.Post(5); Check.That(processed).HasSize(2); }
public void ShouldHaveANiceApi() { // conflating is more about data than about calls // the API should reflect that var factory = new UnitOfExecutionsFactory(); var dedicated = factory.GetDedicatedThread(); var act = 0; Action <int> processing = _ => act++; var conflatedProcessor = dedicated.BuildConflator(processing); dedicated.Dispatch(() => Thread.Sleep(10)); conflatedProcessor(1); conflatedProcessor(2); conflatedProcessor(3); conflatedProcessor(4); Thread.Sleep(30); Check.That(act).IsEqualTo(1); }
public void ShouldConflateActions() { var factory = new UnitOfExecutionsFactory(); var dedicated = factory.GetDedicatedThread(); var synchroRoot = new object(); var go = true; var ranTasks = 0; var conflator = new ActionConflator(dedicated); dedicated.Dispatch( () => { lock (synchroRoot) { while (go) { Monitor.Wait(synchroRoot, 1000); } } }); conflator.Conflate(() => Interlocked.Increment(ref ranTasks)); conflator.Conflate(() => Interlocked.Increment(ref ranTasks)); conflator.Conflate(() => Interlocked.Increment(ref ranTasks)); lock (synchroRoot) { go = false; Monitor.Pulse(synchroRoot); } Thread.Sleep(100); // tasks should be conflated Check.That(ranTasks).IsEqualTo(1); }