GetAndIncrement() public method

Returns the current value while simultaneously incrementing the counter
public GetAndIncrement ( ) : int
return int
Exemplo n.º 1
0
        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);
        }
Exemplo n.º 2
0
        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_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 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);
 }