public void RunInThreadTest() { int numIterations = 1000000 * 20; var buf = new ValueHolder(); var f = Future.RunInThread(new Func <ValueHolder, int, int>(WorkerThread), buf, numIterations); while (!f.Completed) { Scheduler.Step(); } Assert.AreEqual(42, f.Result); Assert.AreEqual(numIterations, buf.Value); }
public void WaitForWorkItemsTest() { ValueHolder vh = new ValueHolder(); vh.Value = 0; long timeStart = Time.Ticks; var a = Scheduler.Start(new Sleep(1.5)); a.RegisterOnComplete((f) => { Scheduler.QueueWorkItem(() => { vh.Value = 1; }); }); Scheduler.Step(); Scheduler.WaitForWorkItems(3.0); long elapsed = Time.Ticks - timeStart; Assert.LessOrEqual(elapsed, TimeSpan.FromMilliseconds(1525).Ticks); Scheduler.Step(); Assert.AreEqual(1, vh.Value); }
public void MultipleThreadTest() { int numIterations = 1000000; int numWorkers = 50; var buf = new ValueHolder(); var fn = new Func <ValueHolder, int, int>(WorkerThread); var futures = new List <IFuture>(); for (int i = 0; i < numWorkers; i++) { futures.Add(Future.RunInThread(fn, buf, numIterations)); } var f = Future.WaitForAll(futures); while (!f.Completed) { Scheduler.Step(); } Assert.AreEqual(numIterations * numWorkers, buf.Value); }