示例#1
0
        public void InvalidOperationTests()
        {
            WatchdogTimer uut = new WatchdogTimer(int.MaxValue, "InvalidOperationTests");

            bool started = false;
            bool stopped = false;
            bool reset   = false;

            uut.OnStarted += () => { started = true; };
            uut.OnStopped += () => { stopped = true; };
            uut.OnReset   += () => { reset = true; };

            Assert.Throws <InvalidOperationException>(() => uut.Stop());  // Can't stop if its not started.
            Assert.Throws <InvalidOperationException>(() => uut.Reset());

            // Ensure events didn't fire.
            Assert.IsFalse(started);
            Assert.IsFalse(stopped);
            Assert.IsFalse(reset);

            // Call Start
            uut.Start();
            Assert.IsTrue(started);

            started = false;
            // Calling start again should get an InvalidOperationException
            Assert.Throws <InvalidOperationException>(() => uut.Start());
            Assert.IsFalse(started);
            Assert.IsFalse(stopped);
            Assert.IsFalse(reset);

            uut.Reset();
            Assert.IsFalse(started);
            Assert.IsFalse(stopped);
            Assert.IsTrue(reset);
            reset = false;

            uut.Stop();
            Assert.IsFalse(started);
            Assert.IsTrue(stopped);
            Assert.IsFalse(reset);
            stopped = false;

            uut.Dispose();

            // Should get ObjectDisposedExceptions
            Assert.Throws <ObjectDisposedException>(() => uut.Start());
            Assert.Throws <ObjectDisposedException>(() => uut.Stop());
            Assert.Throws <ObjectDisposedException>(() => uut.Reset());

            Assert.IsFalse(started);
            Assert.IsFalse(stopped);
            Assert.IsFalse(reset);

            // Nothing bad should happen if we call Dispose again
            uut.Dispose();
        }
示例#2
0
 private void GlobalClickEventHandler(object sender, EventArgs e)
 {
     if (_screenSaverTimer != null)
     {
         _screenSaverTimer.Reset();
     }
 }
示例#3
0
        public void ResetTest()
        {
            using (WatchdogTimer uut = new WatchdogTimer(3000, "ResetTest"))
            {
                bool resetCalled = false;

                Exception err = new Exception("My Exception");

                uut.OnTimeoutExpired += delegate()
                {
                    resetCalled = true;
                };

                uut.Start();
                // Calling Reset every half second should
                // prevent the watchdog from firing, which is set to
                // expire after 3 seconds.
                for (int i = 0; i < 12; ++i)
                {
                    uut.Reset();
                    Thread.Sleep(500);
                }
                uut.Stop();

                Assert.IsFalse(resetCalled);
            }
        }