public void should_start_after_first_period()
        {
            var period = 500.Milliseconds();

            _periodicInitializer = new XPeriodicActionHostInitializer(_bus, period);

            var      signal        = new ManualResetEvent(false);
            DateTime?firstCallTime = null;

            _periodicInitializer.PeriodicAction = () =>
            {
                if (firstCallTime == null)
                {
                    firstCallTime = DateTime.UtcNow;
                }

                signal.Set();
            };

            var startTime = DateTime.UtcNow;

            _periodicInitializer.AfterStart();

            signal.WaitOne(2.Seconds());

            Console.WriteLine("First call " + firstCallTime.GetValueOrDefault().ToString("h:mm:ss.fffff"));
            Console.WriteLine("Expected " + (startTime + period).ToString("h:mm:ss.fffff"));

            firstCallTime.ShouldNotBeNull();
            firstCallTime.GetValueOrDefault().ShouldApproximateDateTime(startTime + period, 50);
        }
        public void should_start_at_specified_time()
        {
            var offset = 1.Seconds();

            _periodicInitializer = new XPeriodicActionHostInitializer(_bus, 20.Milliseconds(), () => DateTime.UtcNow + offset);

            var      signal        = new ManualResetEvent(false);
            DateTime?firstCallTime = null;

            _periodicInitializer.PeriodicAction = () =>
            {
                if (firstCallTime == null)
                {
                    firstCallTime = DateTime.UtcNow;
                }

                signal.Set();
            };

            var startTime = DateTime.UtcNow;

            _periodicInitializer.AfterStart();

            signal.WaitOne(2.Seconds());

            firstCallTime.ShouldNotBeNull();
            firstCallTime.GetValueOrDefault().ShouldApproximateDateTime(startTime + offset, 50);
        }
        public void should_not_run_action_if_period_is_negative()
        {
            var period = -20.Milliseconds();

            _periodicInitializer = new XPeriodicActionHostInitializer(_bus, period);

            var callCount = 0;

            _periodicInitializer.PeriodicAction = () => callCount++;

            _periodicInitializer.AfterStart();

            Thread.Sleep(100);

            callCount.ShouldEqual(0);
        }