public void Adding_operations_to_a_fiber() { _count = 10000; _values = new int[_count]; Fiber fiber = new PoolFiber(); int index = 0; var completed = new Future <int>(); var go = new Future <bool>(); fiber.Add(() => { go.WaitUntilCompleted(10.Seconds()); }); for (int i = 0; i < _count; i++) { int offset = i; fiber.Add(() => { _values[offset] = index++; if (offset == _count - 1) { completed.Complete(offset); } }); } go.Complete(true); completed.WaitUntilCompleted(10.Seconds()).ShouldBeTrue(); }
public void Adding_operations_to_a_fiber() { _count = 10000; _values = new int[_count]; Fiber fiber = new PoolFiber(); int index = 0; var completed = new Future<int>(); var go = new Future<bool>(); fiber.Add(() => { go.WaitUntilCompleted(10.Seconds()); }); for (int i = 0; i < _count; i++) { int offset = i; fiber.Add(() => { _values[offset] = index++; if (offset == _count - 1) completed.Complete(offset); }); } go.Complete(true); completed.WaitUntilCompleted(10.Seconds()).ShouldBeTrue(); }
public void Should_properly_wrap_the_channel_as_synchronized() { Assert.IsNull(SynchronizationContext.Current); var fiber = new PoolFiber(); var input = new ChannelAdapter(); var context = new TestSynchronizationContext(); var future = new Future <TestMessage>(); SynchronizationContext.SetSynchronizationContext(context); Assert.IsNotNull(SynchronizationContext.Current); using (input.Connect(x => { x.AddConsumerOf <TestMessage>() .OnCurrentSynchronizationContext() .UsingConsumer(message => { Trace.WriteLine("Received on Thread: " + Thread.CurrentThread.ManagedThreadId); Assert.IsNotNull(SynchronizationContext.Current); Assert.AreEqual(context, SynchronizationContext.Current); future.Complete(message); }); })) { Trace.WriteLine("Subscribed on Thread: " + Thread.CurrentThread.ManagedThreadId); SynchronizationContext.SetSynchronizationContext(null); input.Flatten().Select(c => c.GetType()).ShouldEqual(new[] { typeof(ChannelAdapter), typeof(BroadcastChannel), typeof(TypedChannelAdapter <TestMessage>), typeof(SynchronizedChannel <TestMessage>), typeof(ConsumerChannel <TestMessage>), }); fiber.Add(() => { Trace.WriteLine("Thread: " + Thread.CurrentThread.ManagedThreadId); Assert.IsNull(SynchronizationContext.Current); input.Send(new TestMessage()); }); Assert.IsNull(SynchronizationContext.Current); future.WaitUntilCompleted(2.Seconds()).ShouldBeTrue(); } }
public void Should_result_in_no_waiting_actions_in_the_queue() { Fiber fiber = new PoolFiber(); var called = new Future<bool>(); 10.Times(() => fiber.Add(() => Thread.Sleep(100))); fiber.Add(() => called.Complete(true)); Stopwatch timer = Stopwatch.StartNew(); fiber.Shutdown(8.Seconds()); timer.Stop(); called.IsCompleted.ShouldBeTrue(); timer.ElapsedMilliseconds.ShouldBeLessThan(2000); }
public void Should_result_in_no_waiting_actions_in_the_queue() { Fiber fiber = new PoolFiber(); var called = new Future <bool>(); 10.Times(() => fiber.Add(() => Thread.Sleep(100))); fiber.Add(() => called.Complete(true)); Stopwatch timer = Stopwatch.StartNew(); fiber.Shutdown(8.Seconds()); timer.Stop(); called.IsCompleted.ShouldBeTrue(); timer.ElapsedMilliseconds.ShouldBeLessThan(2000); }
public void Should_properly_release_one_waiting_writer() { const int writerCount = 10; const int messageCount = 1000; var complete = new Future <bool>(); int total = 0; Fiber reader = new ThreadFiber(); try { Thread.Sleep(100); Stopwatch timer = Stopwatch.StartNew(); var writers = new List <Fiber>(); for (int i = 0; i < writerCount; i++) { Fiber fiber = new PoolFiber(); for (int j = 0; j < messageCount; j++) { fiber.Add(() => { SuperSleeper.Wait(1); reader.Add(() => { total++; if (total == writerCount * messageCount) { complete.Complete(true); } }); }); } writers.Add(fiber); } complete.WaitUntilCompleted(20.Seconds()).ShouldBeTrue(); timer.Stop(); Trace.WriteLine("Elapsed time: " + timer.ElapsedMilliseconds + "ms (expected " + writerCount * messageCount + ")"); } finally { reader.Stop(); } }
public void Should_prevent_new_actions_from_being_queued() { Fiber fiber = new PoolFiber(); var called = new Future<bool>(); fiber.Stop(); fiber.Add(() => called.Complete(true)); fiber.Shutdown(10.Seconds()); called.IsCompleted.ShouldBeFalse(); }
public void Should_prevent_new_actions_from_being_queued() { Fiber fiber = new PoolFiber(); var called = new Future <bool>(); fiber.Stop(); fiber.Add(() => called.Complete(true)); fiber.Shutdown(10.Seconds()); called.IsCompleted.ShouldBeFalse(); }
public void Should_properly_release_one_waiting_writer() { const int writerCount = 10; const int messageCount = 1000; var complete = new Future<bool>(); int total = 0; Fiber reader = new ThreadFiber(); try { Thread.Sleep(100); Stopwatch timer = Stopwatch.StartNew(); var writers = new List<Fiber>(); for (int i = 0; i < writerCount; i++) { Fiber fiber = new PoolFiber(); for (int j = 0; j < messageCount; j++) { fiber.Add(() => { SuperSleeper.Wait(1); reader.Add(() => { total++; if (total == writerCount*messageCount) complete.Complete(true); }); }); } writers.Add(fiber); } complete.WaitUntilCompleted(20.Seconds()).ShouldBeTrue(); timer.Stop(); Trace.WriteLine("Elapsed time: " + timer.ElapsedMilliseconds + "ms (expected " + writerCount*messageCount + ")"); } finally { reader.Stop(); } }