public void SharedFiber_shutdown_should_not_disrupt_original_Fiber() { var atomicCounter = new AtomicCounter(0); var originalFiber = FiberFactory.CreateFiber(2); //going to use a dedicated thread Fiber var sharedFiber1 = new SharedFiber(originalFiber); var sharedFiber2 = sharedFiber1.Clone(); for (var i = 0; i < 1000; i++) { originalFiber.Add(() => atomicCounter.GetAndIncrement()); sharedFiber1.Add(() => atomicCounter.GetAndIncrement()); } sharedFiber1.GracefulShutdown(TimeSpan.FromSeconds(1)).Wait(); //wait for the fiber to finish Assert.AreEqual(2000, atomicCounter.Current); //should have a total count of 2000 for (var i = 0; i < 1000; i++) { originalFiber.Add(() => atomicCounter.GetAndIncrement()); sharedFiber1.Add(() => atomicCounter.GetAndIncrement()); } Thread.Sleep(TimeSpan.FromSeconds(1)); Assert.AreEqual(3000, atomicCounter.Current); //should have a total count of 3000 Assert.IsTrue(sharedFiber2.Running); Assert.IsTrue(originalFiber.Running); Assert.IsFalse(sharedFiber1.Running); }
public void SetUp() { if (!HighPerformance) { ClientSendBuffer = new ConcurrentCircularBuffer<NetworkData>(BufferSize); ClientReceiveBuffer = new ConcurrentCircularBuffer<NetworkData>(BufferSize); ServerReceiveBuffer = new ConcurrentCircularBuffer<NetworkData>(BufferSize); } ClientReceived = new AtomicCounter(0); ServerReceived = new AtomicCounter(0); _clientExecutor = new AssertExecutor(); _serverExecutor = new AssertExecutor(); var serverBootstrap = new ServerBootstrap() .WorkerThreads(2) .Executor(_serverExecutor) .SetTransport(TransportType) .SetEncoder(Encoder) .SetDecoder(Decoder) .SetAllocator(Allocator) .SetConfig(Config) .Build(); _server = serverBootstrap.NewConnection(Node.Loopback()); _clientConnectionFactory = new ClientBootstrap() .Executor(_clientExecutor) .SetTransport(TransportType) .SetEncoder(Encoder) .SetDecoder(Decoder) .SetAllocator(Allocator) .SetConfig(Config) .Build(); }
public void Should_be_able_to_change_NetworkEventLoop_error_handler_at_runtime() { var eventLoop = EventLoopFactory.CreateNetworkEventLoop(); var count = new AtomicCounter(0); var trappedException = false; var backgroundProducer = Task.Run(() => { for (var i = 0; i < 10; i++) { eventLoop.Execute(() => count.GetAndIncrement()); Thread.Sleep(10); } }); eventLoop.SetExceptionHandler((connection, exception) => trappedException = true, null); eventLoop.Execute(() => { throw new Exception("I'm an exception!"); }); backgroundProducer.Wait(); Assert.AreEqual(10, count.Current); Assert.IsTrue(trappedException); }
public void Should_use_multiple_threads_to_process_queue() { var atomicCounter = new AtomicCounter(0); var fiber = FiberFactory.CreateFiber(2); for (var i = 0; i < 1000; i++) { fiber.Add(() => atomicCounter.GetAndIncrement()); } fiber.GracefulShutdown(TimeSpan.FromSeconds(1)).Wait(); //wait for the fiber to finish Assert.AreEqual(1000, atomicCounter.Current); }
public void Should_not_be_able_to_add_jobs_after_shutdown() { var atomicCounter = new AtomicCounter(0); var fiber = FiberFactory.CreateFiber(2); for (var i = 0; i < 1000; i++) { fiber.Add(() => atomicCounter.GetAndIncrement()); } fiber.GracefulShutdown(TimeSpan.FromSeconds(1)).Wait(); //wait for the fiber to finish //try to increment the counter a bunch more times for (var i = 0; i < 1000; i++) { fiber.Add(() => atomicCounter.GetAndIncrement()); } //value should be equal to its pre-shutdown value Assert.AreEqual(1000, atomicCounter.Current); }
public void STE_should_execute_scheduled_tasks_on_time() { var counter = new AtomicCounter(0); var executor = new SingleThreadEventExecutor("Foo" + ThreadNameCounter.GetAndIncrement(), TimeSpan.FromMilliseconds(100)); Action<object> increment = o => ((AtomicCounter) o).GetAndIncrement(); // schedule a delayed operation var checkCounter = executor.ScheduleAsync(o => Assert.True(((AtomicCounter) o).Current == 4), counter, TimeSpan.FromMilliseconds(40)); // schedule 4 immediate operations executor.Execute(increment, counter); executor.Execute(increment, counter); executor.Execute(increment, counter); executor.Execute(increment, counter); // delay should run after the first 4 previous checkCounter.Wait(TimeSpan.FromMilliseconds(100)); }