Exemplo n.º 1
0
        public static void TimeoutCancel()
        {
            RootedTimeout timeout;
            var           value = new[] { 0 };

            while (true)
            {
                value[0] = 0;
                timeout  = RootedTimeout.Launch(() => value[0] = 1, 1000);
                Assert.IsFalse(timeout.IsCanceled);
                timeout.Cancel();
                while (!timeout.IsCompleted && !timeout.IsCanceled)
                {
                    // Empty
                }

                if (!timeout.IsCompleted)
                {
                    break;
                }

                Assert.AreEqual(1, value[0]);
            }

            Assert.IsTrue(timeout.IsCanceled);
            Assert.IsFalse(timeout.IsCompleted);
            Assert.AreEqual(0, value[0]);
        }
Exemplo n.º 2
0
        public static void TimeoutConstructorCancellationToken()
        {
            var value   = new[] { 0 };
            var timeout = RootedTimeout.Launch(() => value[0] = 1, 0, CancellationToken.None);

            Assert.IsFalse(timeout.IsCanceled);
        }
Exemplo n.º 3
0
        public static RootedTimeout Launch(Action callback, long dueTime, CancellationToken token)
        {
            if (callback == null)
            {
                throw new ArgumentNullException(nameof(callback));
            }
            if (dueTime < -1)
            {
                throw new ArgumentOutOfRangeException(nameof(dueTime));
            }
            var timeout = new RootedTimeout();

            if (token.IsCancellationRequested)
            {
                timeout._status = _canceled;
                return(timeout);
            }
            timeout.Callback = () =>
            {
                try
                {
                    callback();
                }
                finally
                {
                    UnRoot(timeout);
                }
            };
            token.Register(timeout.Cancel);
            Root(timeout);
            timeout.Start(dueTime);
            return(timeout);
        }
Exemplo n.º 4
0
        public static RootedTimeout Launch(Action callback, long dueTime)
        {
            if (callback == null)
            {
                throw new ArgumentNullException(nameof(callback));
            }
            if (dueTime < -1)
            {
                throw new ArgumentOutOfRangeException(nameof(dueTime));
            }
            var timeout = new RootedTimeout();

            timeout.Callback = () =>
            {
                try
                {
                    callback();
                }
                finally
                {
                    UnRoot(timeout);
                }
            };
            Root(timeout);
            timeout.Start(dueTime);
            return(timeout);
        }
Exemplo n.º 5
0
        public static void TimeoutConstructorZeroDueTime()
        {
            var value   = new[] { 0 };
            var timeout = RootedTimeout.Launch(() => value[0] = 1, 0);

            ThreadingHelper.SpinWaitUntil(() => timeout.IsCompleted);
            Assert.AreEqual(1, value[0]);
        }
Exemplo n.º 6
0
        protected static void UnRoot(RootedTimeout rootedTimeout)
        {
            var rootIndex = Interlocked.Exchange(ref rootedTimeout._rootIndex, -1);

            if (rootIndex != -1)
            {
                _root.RemoveAt(rootIndex);
            }
        }
Exemplo n.º 7
0
        public static void TimeoutConstructorCanceledToken()
        {
            var value   = new[] { 0 };
            var token   = new CancellationToken(true);
            var timeout = RootedTimeout.Launch(() => value[0] = 1, 0, token);

            Assert.IsTrue(timeout.IsCanceled);
            Assert.IsFalse(timeout.IsCompleted);
            Assert.AreEqual(0, value[0]);
        }
Exemplo n.º 8
0
        public static void TimeoutFinishAndChange()
        {
            var value   = new[] { 0 };
            var timeout = RootedTimeout.Launch(() => value[0] = 1, 100);

            Assert.IsFalse(timeout.IsCanceled);
            ThreadingHelper.SpinWaitUntil(() => timeout.IsCompleted);
            Assert.IsFalse(timeout.IsCanceled);
            Assert.IsTrue(timeout.IsCompleted);
            Assert.AreEqual(1, value[0]);
            Assert.IsFalse(timeout.Change(1000));
            Assert.IsFalse(timeout.IsCanceled);
            Assert.IsTrue(timeout.IsCompleted);
        }
Exemplo n.º 9
0
        public static void TimeoutChange()
        {
again:
            var now = DateTime.Now;
            var value   = new[] { now };
            var timeout = RootedTimeout.Launch(() => value[0] = DateTime.Now, 100);

            if (!timeout.Change(1000))
            {
                goto again;
            }
            Assert.IsFalse(timeout.IsCanceled);
            Assert.IsFalse(timeout.IsCompleted);
            ThreadingHelper.SpinWaitUntil(() => timeout.IsCompleted);
            Assert.Greater((value[0] - now).TotalMilliseconds, 100);
        }
Exemplo n.º 10
0
        public static void TimeoutCancel()
        {
again:
            var value = new[] { 0 };
            var timeout = RootedTimeout.Launch(() => value[0] = 1, 1000);

            Assert.IsFalse(timeout.IsCanceled);
            timeout.Cancel();
            if (timeout.IsCompleted)
            {
                Assert.AreEqual(1, value[0]);
                goto again;
            }
            Assert.IsTrue(timeout.IsCanceled);
            Assert.IsFalse(timeout.IsCompleted);
            Assert.AreEqual(0, value[0]);
        }
Exemplo n.º 11
0
        public static void TimeoutChange()
        {
            RootedTimeout timeout;
            var           value = new DateTime[1];
            DateTime      now;

            do
            {
                now      = DateTime.Now;
                value[0] = now;
                timeout  = RootedTimeout.Launch(() => value[0] = DateTime.Now, 100);
            }while (!timeout.Change(1000));

            Assert.IsFalse(timeout.IsCanceled);
            Assert.IsFalse(timeout.IsCompleted);
            ThreadingHelper.SpinWaitUntil(() => timeout.IsCompleted);
            Assert.Greater((value[0] - now).TotalMilliseconds, 100);
        }
Exemplo n.º 12
0
        public static void TimeoutRemaining()
        {
again:
            var now = DateTime.Now;
            var value     = new[] { now };
            var timeout   = RootedTimeout.Launch(() => value[0] = DateTime.Now, 500);
            var remaining = timeout.CheckRemaining();

            if (timeout.IsCompleted)
            {
                goto again;
            }
            Assert.LessOrEqual(remaining, 500);
            Thread.Sleep(1);
            var newremaining = timeout.CheckRemaining();

            Assert.LessOrEqual(newremaining, remaining);
            Assert.GreaterOrEqual(remaining, 0);
            Assert.GreaterOrEqual(newremaining, 0);
        }
Exemplo n.º 13
0
        public static void TimeoutRemaining()
        {
            long          remaining;
            RootedTimeout timeout;

            do
            {
                var now   = DateTime.Now;
                var value = new[] { now };
                timeout   = RootedTimeout.Launch(() => value[0] = DateTime.Now, 500);
                remaining = timeout.CheckRemaining();
            }while (timeout.IsCompleted);

            Assert.LessOrEqual(remaining, 500);
            Thread.Sleep(1);
            var newRemaining = timeout.CheckRemaining();

            Assert.LessOrEqual(newRemaining, remaining);
            Assert.GreaterOrEqual(remaining, 0);
            Assert.GreaterOrEqual(newRemaining, 0);
        }
Exemplo n.º 14
0
 protected static void Root(RootedTimeout rootedTimeout)
 {
     rootedTimeout._rootIndex = Interlocked.Increment(ref _lastRootIndex);
     _root.Set(rootedTimeout._rootIndex, rootedTimeout);
 }