Esempio 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);
        }
Esempio n. 2
0
        private static void Main(string[] args)
        {
            using (var system = ActorSystem.Create("TimeClient"))
            {
                var tmp = system.ActorSelection("akka.tcp://TimeServer@localhost:9391/user/time");
                Console.Title = string.Format("TimeClient {0}", Process.GetCurrentProcess().Id);
                var timeClient = system.ActorOf(Props.Create(() => new TimeClientActor(tmp)), "timeChecker");


                var fiber = FiberFactory.CreateFiber(3);

                while (!Program.IsShutdown)
                {
                    fiber.Add(() =>
                    {
                        Console.WriteLine("before sleep");
                        Thread.Sleep(3000);
                        timeClient.Tell(Time);
                    });
                }

                Console.WriteLine("Connection closed.");
                fiber.GracefulShutdown(TimeSpan.FromSeconds(1));

                Console.ReadLine();
                IsShutdown = true;
                Console.WriteLine("Shutting down...");
                Console.WriteLine("Terminated");
            }
        }
Esempio n. 3
0
        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);
        }
Esempio n. 4
0
        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);
        }
Esempio n. 5
0
        private static void LoopWrite()
        {
            var command = Encoding.UTF8.GetBytes("gettime");
            var fiber   = FiberFactory.CreateFiber(3);

            Action dedicatedMethod = () =>
            {
                Thread.Sleep(1);
                TimeClient.Send(new NetworkData {
                    Buffer = command, Length = command.Length
                });
            };

            while (TimeClient.IsOpen())
            {
                fiber.Add(dedicatedMethod);
            }
            Console.WriteLine("Connection closed.");
            fiber.GracefulShutdown(TimeSpan.FromSeconds(1));
        }
Esempio n. 6
0
 private static void CreateAndWaitForWorkItems(int numWorkItems, int numThreads)
 {
     using (var mre = new ManualResetEvent(false))
         using (var fiber = FiberFactory.CreateFiber(numThreads))
         {
             var itemsRemaining = numWorkItems;
             for (var i = 0; i < numWorkItems; i++)
             {
                 fiber.Add(delegate
                 {
                     if (Interlocked.Decrement(
                             ref itemsRemaining) == 0)
                     {
                         mre.Set();
                     }
                 });
             }
             mre.WaitOne();
         }
 }
Esempio n. 7
0
 public ThreadedEventLoop(int workerThreads) : base(FiberFactory.CreateFiber(workerThreads))
 {
 }
Esempio n. 8
0
 public ThreadedEventLoop(IExecutor internalExecutor, int workerThreads)
     : base(FiberFactory.CreateFiber(internalExecutor, workerThreads))
 {
 }
Esempio n. 9
0
 protected override IFiber CreateFiber()
 {
     return(FiberFactory.CreateFiber(2));
 }
Esempio n. 10
0
 protected override IFiber CreateFiber()
 {
     return(FiberFactory.CreateFiber(FiberMode.MaximumConcurrency));
 }
Esempio n. 11
0
 protected override IFiber CreateFiber()
 {
     return(FiberFactory.CreateFiber(FiberMode.Synchronous));
 }
Esempio n. 12
0
 protected override IFiber CreateFiber()
 {
     return(FiberFactory.CreateFiber(FiberMode.SingleThreaded));
 }
Esempio n. 13
0
 public SimpleEventBroker() : this(FiberFactory.CreateFiber(FiberMode.Synchronous))
 {
 }