Ejemplo n.º 1
0
        public void Should_not_stall_the_scheduler()
        {
            Fiber fiber = new ThreadPoolFiber();
            FiberScheduler scheduler = new TimerFiberScheduler(new ThreadPoolFiber());

            var called = new Future<bool>();

            scheduler.Schedule(200.Milliseconds(), fiber, () => { throw new InvalidOperationException("Bugger!"); });
            scheduler.Schedule(400.Milliseconds(), fiber, () => called.Complete(true));

            called.WaitUntilCompleted(1.Seconds()).ShouldBeTrue();
        }
Ejemplo n.º 2
0
        public void Timer_Based_Demo()
        {
            FiberScheduler scheduler = new TimerFiberScheduler(new SynchronousFiber());
            scheduler.Schedule(0, 1000, new SynchronousFiber(), () =>
                {
                    Trace.WriteLine("Starting it up");
                    _ping.Start(10, _pong);
                });

            Thread.Sleep(6000);
        }
        public void Schedule()
        {
            var fiber = new SynchronousFiber();

            var count = 0;
            var reset = new AutoResetEvent(false);
            Action one = () => Assert.AreEqual(0, count++);
            Action two = () => Assert.AreEqual(1, count++);
            Action three = delegate
                {
                    Assert.AreEqual(2, count++);
                    reset.Set();
                };

            var thread = new TimerFiberScheduler(fiber);
            thread.Schedule(50, fiber, three);
            thread.Schedule(1, fiber, two);
            thread.Schedule(1, fiber, two);
            Assert.IsTrue(reset.WaitOne(10000, false));
        }
Ejemplo n.º 4
0
        public void Should_wait_until_the_appropriate_time_for_the_action_to_execute()
        {
            Fiber fiber = new ThreadPoolFiber();
            FiberScheduler scheduler = new TimerFiberScheduler(new SynchronousFiber());

            var called = new Future<bool>();

            scheduler.Schedule(100.Milliseconds(), fiber, () => called.Complete(true));

            called.IsCompleted.ShouldBeFalse();

            called.WaitUntilCompleted(1.Seconds()).ShouldBeTrue();
        }
Ejemplo n.º 5
0
        public void Should_not_run_any_pending_actions()
        {
            Fiber fiber = new ThreadPoolFiber();
            FiberScheduler scheduler = new TimerFiberScheduler(new ThreadPoolFiber());

            var called = new Future<bool>();

            scheduler.Schedule(1.Seconds(), fiber, () => called.Complete(true));

            scheduler.Disable();

            called.WaitUntilCompleted(2.Seconds()).ShouldBeFalse();
        }
Ejemplo n.º 6
0
        public void Should_schedule_events()
        {
            var update = new UserUpdate {LastActivity = DateTime.Now - 5.Minutes()};

            Fiber fiber = new SynchronousFiber();

            var future = new Future<UserUpdate>();

            Channel<UserUpdate> channel = new PublishSubscribeChannel<UserUpdate>(fiber, new Channel<UserUpdate>[] {future});

            var scheduler = new TimerFiberScheduler(fiber);

            scheduler.Schedule(1000, fiber, () => channel.Send(update));

            Thread.Sleep(500);

            Assert.IsFalse(future.WaitUntilCompleted(0.Seconds()));

            Assert.IsTrue(future.WaitUntilCompleted(1.Seconds()));
        }
        public void Schedule1000In1ms()
        {
            var fiber = new SynchronousFiber();

            var count = 0;
            var reset = new AutoResetEvent(false);
            Action one = delegate
                {
                    count++;
                    if (count == 1000)
                    {
                        reset.Set();
                    }
                };

            var thread = new TimerFiberScheduler(fiber);
            {
                for (var i = 0; i < 1000; i++)
                {
                    thread.Schedule(i, fiber,one);
                }
                Assert.IsTrue(reset.WaitOne(1200, false));
            }
        }
Ejemplo n.º 8
0
        public void Should_run_the_action_until_disabled()
        {
            Fiber fiber = new ThreadPoolFiber();
            FiberScheduler scheduler = new TimerFiberScheduler(new ThreadPoolFiber());

            Stopwatch elapsed = Stopwatch.StartNew();

            int count = 0;
            var called = new Future<int>();
            var failed = new Future<bool>();

            ScheduledAction scheduledAction = null;
            scheduledAction = scheduler.Schedule(TimeSpan.Zero, 100.Milliseconds(), fiber, () =>
                {
                    count++;
                    if (count == 10)
                    {
                        called.Complete(count);
                        scheduledAction.Cancel();
                    }
                    else if (count > 10)
                    {
                        failed.Complete(true);
                    }
                });

            called.WaitUntilCompleted(5.Seconds()).ShouldBeTrue();

            elapsed.Stop();

            failed.WaitUntilCompleted(200.Milliseconds()).ShouldBeFalse();

            Trace.WriteLine("Time Period: " + elapsed.ElapsedMilliseconds);
        }
Ejemplo n.º 9
0
        public void Should_run_the_new_action_immediately()
        {
            Fiber fiber = new ThreadPoolFiber();
            FiberScheduler scheduler = new TimerFiberScheduler(new ThreadPoolFiber());

            var called = new Future<bool>();

            scheduler.Schedule(2.Seconds(), fiber, () => { });
            scheduler.Schedule(0.Seconds(), fiber, () => called.Complete(true));

            called.WaitUntilCompleted(1.Seconds()).ShouldBeTrue();
        }