Пример #1
0
        public void RepeatedPostpone_MultiShot()
        {
            int count = 200;
            SimpleTimerHarness   harness = new SimpleTimerHarness();
            VersionedTimer <int> timer   = new VersionedTimer <int>(456, harness.Callback);

            using ( timer )
            {
                for (int i = 0; i < count; i++)
                {
                    timer.Change(1000, 100, i);

                    Thread.Sleep(50);
                }

                // Verify the timer fires repeatedly.
                for (int i = 0; i < 10; i++)
                {
                    harness.Wait();
                }

                timer.Change(Timeout.Infinite, Timeout.Infinite, 0);

                Assert.AreEqual(10, harness.Callbacks);
                Assert.AreEqual(456, harness.ObservedState);
                Assert.AreEqual(count - 1, harness.ObservedVersion);
            }
        }
Пример #2
0
        public void DisposeAfterDisableMultiShot()
        {
            for (int i = 0; i < 100; i++)
            {
                VersionedTimer <int> timer;
                SimpleTimerHarness   harness = new SimpleTimerHarness();

                timer = new VersionedTimer <int>(123, harness.Callback);

                using ( timer )
                {
                    timer.Change(10, 10, 0);

                    Assert.IsTrue(harness.Wait(), "Timer did not fire.");
                    Assert.IsTrue(harness.Wait(), "Timer did not fire.");
                    Assert.IsTrue(harness.Wait(), "Timer did not fire.");

                    timer.Change(Timeout.Infinite, Timeout.Infinite, 0);
                }
            }
        }
Пример #3
0
        public void Dispose_AllowsMultipleDisposes()
        {
            SimpleTimerHarness   harness = new SimpleTimerHarness();
            VersionedTimer <int> timer   = new VersionedTimer <int>(123, harness.Callback);

            timer.Change(10, Timeout.Infinite, 0);

            Assert.IsTrue(harness.Wait(), "Timer did not fire.");

            timer.Dispose();
            timer.Dispose();
        }
Пример #4
0
        public void SingleShot()
        {
            SimpleTimerHarness harness = new SimpleTimerHarness();

            using (var timer = new VersionedTimer <int>(123, harness.Callback))
            {
                harness.ExpectDelays(100, Timeout.Infinite);
                timer.Change(100, Timeout.Infinite, 1);

                // Give the timer some rope to hang itself by if it's accidentally going to fire
                // multiple times.
                Thread.Sleep(500);

                Assert.IsTrue(harness.Wait(), "Timer never fired.");
                Assert.AreEqual(123, harness.ObservedState, "Timer fired with wrong state.");
                Assert.AreEqual(1, harness.ObservedVersion, "Timer fired with wrong version.");
                Assert.AreEqual(1, harness.Callbacks, "Timer fired wrong number of times.");
                Assert.AreEqual(0, harness.TimeoutError.TotalMilliseconds, 30, "Timer timeout was inaccurate.");
            }
        }
Пример #5
0
        public void MultiShot()
        {
            SimpleTimerHarness harness = new SimpleTimerHarness();
            Stopwatch          watch   = new Stopwatch();

            watch.Start();
            watch.Reset();

            using (var timer = new VersionedTimer <int>(123, harness.Callback))
            {
                harness.ExpectDelays(75, 150);
                timer.Change(75, 150, 1);
                watch.Start();

                Thread.Sleep(75 + 150 * 4);

                for (int i = 1; i <= 5; i++)
                {
                    Assert.IsTrue(harness.Wait(), string.Format("Timer period #{0} never fired.", i));
                }

                timer.Change(Timeout.Infinite, Timeout.Infinite, 2);
                watch.Stop();

                TimeSpan elapsed = watch.Elapsed;

                int numFirings = (int)((elapsed.TotalMilliseconds - 75.0) / 150.0) + 1;

                Assert.AreEqual(123, harness.ObservedState, 123, "Timer fired with wrong state.");
                Assert.AreEqual(1, harness.ObservedVersion, "Timer fired with wrong version.");
                Assert.AreEqual(numFirings, harness.Callbacks, "Timer fired wrong number of times.");
                Assert.AreEqual(0, harness.TimeoutError.TotalMilliseconds, 30, "Timer timeout was inaccurate.");
                Assert.AreEqual(0, harness.PeriodErrors.Average(x => x.TotalMilliseconds), 30, "Timer period was inaccurate.");
                Assert.AreEqual(0, harness.PeriodErrors.Max(x => x.TotalMilliseconds), 30, "Timer period was inaccurate.");

                Trace.WriteLine(string.Format(
                                    "{0} callbacks occurred in {1:0.000} ms.",
                                    harness.Callbacks, elapsed.TotalMilliseconds
                                    ));
            }
        }
Пример #6
0
        public void VerifyObjectDisposedException()
        {
            VersionedTimer <int> timer;
            SimpleTimerHarness   harness = new SimpleTimerHarness();

            timer = new VersionedTimer <int>(123, harness.Callback);

            try
            {
                timer.Change(10, Timeout.Infinite, 0);
                Assert.IsTrue(harness.Wait(), "Timer did not fire.");
            }
            finally
            {
                timer.Dispose();
            }

            Assert2.Throws <ObjectDisposedException>(() =>
            {
                timer.Change(100, 100, 1);
            });
        }