public void Should_capture_the_interval_channel()
        {
            var channel = new ConsumerChannel<ICollection<int>>(new SynchronousFiber(), x => { });
            var scheduler = new TimerFiberScheduler(new SynchronousFiber());
            var interval = new IntervalChannel<int>(new SynchronousFiber(), scheduler, 5.Minutes(), channel);

            new ChannelVisitor().Visit(interval);
        }
Exemple #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 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();
        }
        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();
        }
        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();
        }
Exemple #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 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));
        }
Exemple #8
0
        public void Should_deliver_the_messages_at_once()
        {
            var queue = new SynchronousFiber();
            var scheduler = new TimerFiberScheduler(new SynchronousFiber());

            var called = new Future<ICollection<MyMessage>>();
            var consumer = new ConsumerChannel<ICollection<MyMessage>>(queue, called.Complete);

            var channel = new IntervalChannel<MyMessage>(queue, scheduler, 2.Seconds(), consumer);

            for (int i = 0; i < 5; i++)
            {
                channel.Send(new MyMessage());
            }

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

            channel.Dispose();

            called.Value.ShouldNotBeNull();
            called.Value.Count.ShouldEqual(5);
        }
        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));
            }
        }
        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);
        }
        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();
        }