Exemplo n.º 1
0
        private static void ClockChanged_Backward2_Callback()
        {
            var provider = new MyPlatformEnlightenmentProvider();

            PlatformEnlightenmentProvider.Current = provider;

            var scm = (ClockChanged)provider.GetService <INotifySystemClockChanged>();

            var cal = provider._cal;
            var now = new DateTimeOffset(2012, 4, 25, 12, 0, 0, TimeSpan.Zero);
            var rel = TimeSpan.FromSeconds(1);
            var due = now + rel;
            var err = TimeSpan.FromMinutes(-1);

            var s = new MyScheduler();

            s.SetTime(now);

            var n = 0;

            s.Schedule(due, () => { n++; });

            Assert.Equal(0, cal._queue.Count);
            Assert.Equal(1, s._queue.Count);
            var wrk = s._queue[0];

            Assert.True(wrk.DueTime == rel);

            s.SetTime(due + err);
            scm.OnSystemClockChanged();

            Assert.Equal(1, cal._queue.Count);

            var tmr = cal._queue.Deq();

            Assert.True(tmr.Interval > rel);
            Assert.True(tmr.Interval < -err);

            s.SetTime(s.Now + tmr.Interval);
            tmr.Value._action(tmr.Value._state);

            Assert.Equal(0, n);

            Assert.Equal(0, cal._queue.Count);
            Assert.Equal(1, s._queue.Count);

            s.SetTime(due);
            s._queue.Deq().Invoke();

            Assert.Equal(1, n);

            wrk.Invoke();       // Bad schedulers may not grant cancellation immediately.
            Assert.Equal(1, n); // Invoke shouldn't cause double execution of the work.
        }
Exemplo n.º 2
0
        private static void ClockChanged_Backward1_Callback()
        {
            var provider = new MyPlatformEnlightenmentProvider();

            PlatformEnlightenmentProvider.Current = provider;

            var scm = (ClockChanged)provider.GetService <INotifySystemClockChanged>();

            var cal = provider._cal;
            var now = new DateTimeOffset(2012, 4, 25, 12, 0, 0, TimeSpan.Zero);
            var rel = TimeSpan.FromMinutes(1);
            var due = now + rel;
            var err = TimeSpan.FromMinutes(-2);

            var s = new MyScheduler();

            s.SetTime(now);

            var done = false;

            s.Schedule(due, () => { done = true; });

            Assert.Equal(1, cal._queue.Count);
            Assert.True(cal._queue[0].Interval < rel);

            Assert.Equal(0, s._queue.Count);

            s.SetTime(due + err);
            scm.OnSystemClockChanged();

            Assert.Equal(1, cal._queue.Count);

            var tmr = cal._queue.Deq();

            Assert.True(tmr.Interval > rel);
            Assert.True(tmr.Interval < -err);

            s.SetTime(s.Now + tmr.Interval);
            tmr.Value._action(tmr.Value._state);

            Assert.False(done);

            Assert.Equal(0, cal._queue.Count);
            Assert.Equal(1, s._queue.Count);

            s.SetTime(due);
            s._queue.Deq().Invoke();

            Assert.True(done);
        }
Exemplo n.º 3
0
        private static void ClockChanged_FalsePositive_Callback()
        {
            var provider = new MyPlatformEnlightenmentProvider();

            PlatformEnlightenmentProvider.Current = provider;

            var scm = (ClockChanged)provider.GetService <INotifySystemClockChanged>();

            var cal = provider._cal;
            var now = new DateTimeOffset(2012, 4, 25, 12, 0, 0, TimeSpan.Zero);
            var rel = TimeSpan.FromMinutes(1);
            var due = now + rel;

            var s = new MyScheduler();

            s.SetTime(now);

            var done = false;

            s.Schedule(due, () => { done = true; });

            Assert.Equal(1, cal._queue.Count);

            s.SetTime(now);
            scm.OnSystemClockChanged();

            var work = cal._queue.Deq();

            Assert.True(work.Interval < rel);

            s.SetTime(s.Now + work.Interval);
            work.Value._action(work.Value._state);

            Assert.Equal(1, s._queue.Count);

            var next = s._queue.Deq();

            Assert.True(s.Now + next.DueTime == due);

            s.SetTime(due);
            next.Invoke();

            Assert.True(done);
        }
Exemplo n.º 4
0
        public void ClockChanged_Forward1()
        {
            Run(() =>
            {
                var provider = new MyPlatformEnlightenmentProvider();
                PlatformEnlightenmentProvider.Current = provider;

                var scm = (ClockChanged)provider.GetService <INotifySystemClockChanged>();

                var cal = provider._cal;
                var now = new DateTimeOffset(2012, 4, 25, 12, 0, 0, TimeSpan.Zero);
                var rel = TimeSpan.FromMinutes(1);
                var due = now + rel;
                var err = TimeSpan.FromMinutes(1);

                var s = new MyScheduler();
                s.SetTime(now);

                var done = false;
                s.Schedule(due, () => { done = true; });

                Assert.AreEqual(1, cal._queue.Count);
                Assert.AreEqual(0, s._queue.Count);

                s.SetTime(due + err);
                scm.OnSystemClockChanged();

                Assert.AreEqual(1, s._queue.Count);

                var next = s._queue.Deq();
                Assert.IsTrue(next.DueTime == TimeSpan.Zero);
                next.Invoke();
                Assert.IsTrue(done);

                var tmr = cal._queue.Deq();
                tmr.Value._action(tmr.Value._state);

                Assert.AreEqual(0, cal._queue.Count);
                Assert.AreEqual(0, s._queue.Count);
            });
        }
Exemplo n.º 5
0
        public void ClockChanged_Forward2()
        {
            Run(() =>
            {
                var provider = new MyPlatformEnlightenmentProvider();
                PlatformEnlightenmentProvider.Current = provider;

                var scm = (ClockChanged)provider.GetService <INotifySystemClockChanged>();

                var cal = provider._cal;
                var now = new DateTimeOffset(2012, 4, 25, 12, 0, 0, TimeSpan.Zero);
                var rel = TimeSpan.FromSeconds(1);
                var due = now + rel;
                var err = TimeSpan.FromMinutes(1);

                var s = new MyScheduler();
                s.SetTime(now);

                var n = 0;
                s.Schedule(due, () => { n++; });

                Assert.AreEqual(1, s._queue.Count);

                var wrk = s._queue.Deq();
                Assert.IsTrue(wrk.DueTime == rel);

                s.SetTime(due + err);
                scm.OnSystemClockChanged();

                Assert.AreEqual(1, s._queue.Count);

                var next = s._queue.Deq();
                Assert.IsTrue(next.DueTime == TimeSpan.Zero);
                next.Invoke();
                Assert.AreEqual(1, n);

                wrk.Invoke();          // Bad schedulers may not grant cancellation immediately.
                Assert.AreEqual(1, n); // Invoke shouldn't cause double execution of the work.
            });
        }
Exemplo n.º 6
0
        private static void ClockChanged_RefCounting_Callback()
        {
            var provider = new MyPlatformEnlightenmentProvider();

            PlatformEnlightenmentProvider.Current = provider;

            var scm = (ClockChanged)provider.GetService <INotifySystemClockChanged>();

            var cal = provider._cal;
            var now = new DateTimeOffset(2012, 4, 25, 12, 0, 0, TimeSpan.Zero);

            var s = new MyScheduler();

            s.SetTime(now);

            var due1 = now + TimeSpan.FromSeconds(5);
            var due2 = now + TimeSpan.FromSeconds(8);
            var due3 = now + TimeSpan.FromMinutes(1);
            var due4 = now + TimeSpan.FromMinutes(2);
            var due5 = now + TimeSpan.FromMinutes(3);
            var due6 = now + TimeSpan.FromMinutes(3) + TimeSpan.FromSeconds(2);

            var done1 = false;
            var done2 = false;
            var done3 = false;
            var done4 = false;
            var done5 = false;
            var done6 = false;

            var d1 = s.Schedule(due1, () => { done1 = true; });
            var d5 = s.Schedule(due5, () => { done5 = true; });
            var d3 = s.Schedule(due3, () => { done3 = true; throw new Exception(); });
            var d2 = s.Schedule(due2, () => { done2 = true; });
            var d4 = s.Schedule(due4, () => { done4 = true; });

            d2.Dispose();
            d4.Dispose();

            Assert.Equal(1, scm.N);

            s.SetTime(due1);
            var i1 = s._queue.Deq();

            i1.Invoke();
            Assert.True(done1);

            Assert.Equal(1, scm.N);

            s.SetTime(due2);
            var i2 = s._queue.Deq();

            i2.Invoke();
            Assert.False(done2);

            Assert.Equal(1, scm.N);

            var l1  = cal._queue.Deq();
            var l1d = now + l1.Interval;

            s.SetTime(l1d);
            l1.Value._action(l1.Value._state);

            s.SetTime(due3);
            var i3 = s._queue.Deq();

            try
            {
                i3.Invoke();
                Assert.True(false);
            }
            catch { }
            Assert.True(done3);

            Assert.Equal(1, scm.N);

            var l2  = cal._queue.Deq();
            var l2d = l1d + l2.Interval;

            s.SetTime(l2d);
            l2.Value._action(l2.Value._state);

            s.SetTime(due4);
            var i4 = s._queue.Deq();

            i4.Invoke();
            Assert.False(done4);

            Assert.Equal(1, scm.N);

            var l3  = cal._queue.Deq();
            var l3d = l2d + l3.Interval;

            s.SetTime(l3d);
            l3.Value._action(l3.Value._state);

            s.SetTime(due5);
            var i5 = s._queue.Deq();

            i5.Invoke();
            Assert.True(done5);

            Assert.Equal(0, scm.N);

            var d6 = s.Schedule(due6, () => { done6 = true; });

            Assert.Equal(1, scm.N);

            s.SetTime(due6);
            var i6 = s._queue.Deq();

            i6.Invoke();
            Assert.True(done6);

            Assert.Equal(0, scm.N);
        }