public void WithComparer()
        {
            var now = DateTimeOffset.Now;

            var s = new HistoricalScheduler(now, new ReverseComparer <DateTimeOffset>(Comparer <DateTimeOffset> .Default));

            var res = new List <int>();

            s.Schedule(now - TimeSpan.FromSeconds(1), () => res.Add(1));
            s.Schedule(now - TimeSpan.FromSeconds(2), () => res.Add(2));

            s.Start();

            Assert.True(new[] { 1, 2 }.SequenceEqual(res));
        }
        public void No_Nested_AdvanceBy()
        {
            var s = new HistoricalScheduler();

            s.Schedule(() => s.AdvanceBy(TimeSpan.FromSeconds(1)));

            ReactiveAssert.Throws <InvalidOperationException>(() => s.Start());
        }
        public void No_Nested_AdvanceTo()
        {
            var s = new HistoricalScheduler();

            s.Schedule(() => s.AdvanceTo(DateTimeOffset.Now.AddDays(1)));

            ReactiveAssert.Throws <InvalidOperationException>(() => s.Start());
        }
예제 #4
0
 public void RecursiveActionTimeSpan2()
 {
     int i = 0;
     var scheduler = new HistoricalScheduler ();
     var span = TimeSpan.FromMilliseconds (50);
     scheduler.Schedule<object> (null, span, (object obj,Action<object,TimeSpan> a) => { i++; a (obj, span); });
     scheduler.AdvanceBy (TimeSpan.FromSeconds (1));
     Assert.AreEqual (20, i, "#1");
 }
예제 #5
0
 public void Reschedule(HistoricalScheduler historical)
 {
     if (_dueTime.HasValue)
     {
         _disposable = historical.Schedule(_state, _dueTime.Value, _action);
     }
     else
     {
         if (this._relativeTime > TimeSpan.Zero)
         {
             this._disposable = historical.Schedule(this._state, this._relativeTime, this._action);
         }
         else
         {
             this._disposable = this._action(historical, this._state);
         }
     }
 }
예제 #6
0
        public void RecursiveActionTimeSpan2()
        {
            int i         = 0;
            var scheduler = new HistoricalScheduler();
            var span      = TimeSpan.FromMilliseconds(50);

            scheduler.Schedule <object> (null, span, (object obj, Action <object, TimeSpan> a) => { i++; a(obj, span); });
            scheduler.AdvanceBy(TimeSpan.FromSeconds(1));
            Assert.AreEqual(20, i, "#1");
        }
        public void Cancellation()
        {
            var s = new HistoricalScheduler();

            var list = new List <Timestamped <int> >();

            var d = s.Schedule(Time(2), () => list.Add(new Timestamped <int>(2, s.Now)));

            s.Schedule(Time(1), () =>
            {
                list.Add(new Timestamped <int>(0, s.Now));
                d.Dispose();
            });

            s.Start();

            list.AssertEqual(
                new Timestamped <int>(0, Time(1))
                );
        }
        public void Order()
        {
            var s = new HistoricalScheduler();

            var list = new List <Timestamped <int> >();

            s.Schedule(Time(2), () => list.Add(new Timestamped <int>(2, s.Now)));

            s.Schedule(Time(3), () => list.Add(new Timestamped <int>(3, s.Now)));

            s.Schedule(Time(1), () => list.Add(new Timestamped <int>(0, s.Now)));
            s.Schedule(Time(1), () => list.Add(new Timestamped <int>(1, s.Now)));

            s.Start();

            list.AssertEqual(
                new Timestamped <int>(0, Time(1)),
                new Timestamped <int>(1, Time(1)),
                new Timestamped <int>(2, Time(2)),
                new Timestamped <int>(3, Time(3))
                );
        }
        public void Sleep2()
        {
            var s = new HistoricalScheduler();

            var n = 0;

            s.Schedule(s.Now.AddMinutes(1), rec =>
            {
                s.Sleep(TimeSpan.FromMinutes(3));
                n++;

                rec(s.Now.AddMinutes(1));
            });

            s.AdvanceTo(s.Now + TimeSpan.FromMinutes(5));

            Assert.Equal(2, n);
        }
        public void IsEnabled()
        {
            var s = new HistoricalScheduler();

            Assert.Equal(false, s.IsEnabled);

            s.Schedule(() =>
            {
                Assert.Equal(true, s.IsEnabled);
                s.Stop();
                Assert.Equal(false, s.IsEnabled);
            });

            Assert.Equal(false, s.IsEnabled);

            s.Start();

            Assert.Equal(false, s.IsEnabled);
        }
        public void Start_Stop()
        {
            var s = new HistoricalScheduler();

            var list = new List <Timestamped <int> >();

            var ts = TimeSpan.FromHours(1);

            s.Schedule(Time(0), () => list.Add(new Timestamped <int>(1, s.Now)));
            s.Schedule(Time(1), () => list.Add(new Timestamped <int>(2, s.Now)));
            s.Schedule(Time(2), () => s.Stop());
            s.Schedule(Time(3), () => list.Add(new Timestamped <int>(3, s.Now)));
            s.Schedule(Time(4), () => s.Stop());
            s.Schedule(Time(5), () => s.Start());
            s.Schedule(Time(6), () => list.Add(new Timestamped <int>(4, s.Now)));

            s.Start();

            Assert.Equal(Time(2), s.Now);
            Assert.Equal(Time(2), s.Clock);

            s.Start();

            Assert.Equal(Time(4), s.Now);
            Assert.Equal(Time(4), s.Clock);

            s.Start();

            Assert.Equal(Time(6), s.Now);
            Assert.Equal(Time(6), s.Clock);

            s.Start();

            Assert.Equal(Time(6), s.Now);
            Assert.Equal(Time(6), s.Clock);

            list.AssertEqual(
                new Timestamped <int>(1, Time(0)),
                new Timestamped <int>(2, Time(1)),
                new Timestamped <int>(3, Time(3)),
                new Timestamped <int>(4, Time(6))
                );
        }
        public void AdvanceBy()
        {
            var s = new HistoricalScheduler();

            var list = new List <Timestamped <int> >();

            s.Schedule(Time(0), () => list.Add(new Timestamped <int>(0, s.Now)));
            s.Schedule(Time(1), () => list.Add(new Timestamped <int>(1, s.Now)));
            s.Schedule(Time(2), () => list.Add(new Timestamped <int>(2, s.Now)));
            s.Schedule(Time(10), () => list.Add(new Timestamped <int>(10, s.Now)));
            s.Schedule(Time(11), () => list.Add(new Timestamped <int>(11, s.Now)));

            s.AdvanceBy(Time(8) - s.Now);

            Assert.Equal(Time(8), s.Now);
            Assert.Equal(Time(8), s.Clock);

            list.AssertEqual(
                new Timestamped <int>(0, Time(0)),
                new Timestamped <int>(1, Time(1)),
                new Timestamped <int>(2, Time(2))
                );

            s.Schedule(Time(7), () => list.Add(new Timestamped <int>(7, s.Now)));
            s.Schedule(Time(8), () => list.Add(new Timestamped <int>(8, s.Now)));

            ReactiveAssert.Throws <ArgumentOutOfRangeException>(() => s.AdvanceBy(TimeSpan.FromDays(-4)));

            Assert.Equal(Time(8), s.Now);
            Assert.Equal(Time(8), s.Clock);

            list.AssertEqual(
                new Timestamped <int>(0, Time(0)),
                new Timestamped <int>(1, Time(1)),
                new Timestamped <int>(2, Time(2))
                );

            s.AdvanceBy(TimeSpan.Zero);

            Assert.Equal(Time(8), s.Now);
            Assert.Equal(Time(8), s.Clock);

            list.AssertEqual(
                new Timestamped <int>(0, Time(0)),
                new Timestamped <int>(1, Time(1)),
                new Timestamped <int>(2, Time(2))
                );

            s.AdvanceBy(TimeSpan.FromDays(2));

            Assert.Equal(Time(10), s.Now);
            Assert.Equal(Time(10), s.Clock);

            list.AssertEqual(
                new Timestamped <int>(0, Time(0)),
                new Timestamped <int>(1, Time(1)),
                new Timestamped <int>(2, Time(2)),
                new Timestamped <int>(7, Time(8)),
                new Timestamped <int>(8, Time(8)),
                new Timestamped <int>(10, Time(10))
                );

            s.AdvanceBy(TimeSpan.FromDays(90));

            Assert.Equal(Time(100), s.Now);
            Assert.Equal(Time(100), s.Clock);

            list.AssertEqual(
                new Timestamped <int>(0, Time(0)),
                new Timestamped <int>(1, Time(1)),
                new Timestamped <int>(2, Time(2)),
                new Timestamped <int>(7, Time(8)),
                new Timestamped <int>(8, Time(8)),
                new Timestamped <int>(10, Time(10)),
                new Timestamped <int>(11, Time(11))
                );
        }
예제 #13
0
 public void Reschedule(HistoricalScheduler historical)
 {
     _disposable = _dueTime.HasValue ?
                   historical.Schedule(_state, _dueTime.Value, _action) :
                   historical.Schedule(_state, _relativeTime, _action);
 }