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); }
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"); } }
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); }
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)); }
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(); } }
public ThreadedEventLoop(int workerThreads) : base(FiberFactory.CreateFiber(workerThreads)) { }
public ThreadedEventLoop(IExecutor internalExecutor, int workerThreads) : base(FiberFactory.CreateFiber(internalExecutor, workerThreads)) { }
protected override IFiber CreateFiber() { return(FiberFactory.CreateFiber(2)); }
protected override IFiber CreateFiber() { return(FiberFactory.CreateFiber(FiberMode.MaximumConcurrency)); }
protected override IFiber CreateFiber() { return(FiberFactory.CreateFiber(FiberMode.Synchronous)); }
protected override IFiber CreateFiber() { return(FiberFactory.CreateFiber(FiberMode.SingleThreaded)); }
public SimpleEventBroker() : this(FiberFactory.CreateFiber(FiberMode.Synchronous)) { }