예제 #1
0
        public void TestSubStop()
        {
            // prepare subStart call tracking
            var    callCount = 0;
            Action subStop   = () => callCount++;

            // initialize worker
            using (var worker = new MockWorker(subStop: subStop))
            {
                // verify subStop has not been called
                Assert.AreEqual(0, callCount);

                // stop the worker before it has been started
                worker.Stop();

                // verify subStop has not been called
                Assert.AreEqual(0, callCount);

                // start the worker
                worker.Start();

                // verify subStop has not been called
                Assert.AreEqual(0, callCount);

                // stop the worker
                worker.Stop();

                // verify subStop has been called
                Assert.AreEqual(1, callCount);
            }
        }
예제 #2
0
        public void TestSubStop()
        {
            // prepare subStart call tracking
            var callCount = 0;
            Action subStop = () => callCount++;

            // initialize worker
            using (var worker = new MockWorker(subStop: subStop))
            {
                // verify subStop has not been called
                Assert.AreEqual(0, callCount);

                // stop the worker before it has been started
                worker.Stop();

                // verify subStop has not been called
                Assert.AreEqual(0, callCount);

                // start the worker
                worker.Start();

                // verify subStop has not been called
                Assert.AreEqual(0, callCount);

                // stop the worker
                worker.Stop();

                // verify subStop has been called
                Assert.AreEqual(1, callCount);
            }
        }
예제 #3
0
        public void TestNotifyWork()
        {
            // prepare workAction call tracking
            var callEvent = new AutoResetEvent(false);
            Func<Task> workAction = () => { callEvent.Set(); return Task.FromResult(false); };

            // initialize worker
            using (var worker = new MockWorker(workAction))
            {
                // verify workAction has not been called
                var wasCalled = callEvent.WaitOne(1000);
                Assert.IsFalse(wasCalled);

                // start worker
                worker.Start();

                // verify workAction has not been called
                wasCalled = callEvent.WaitOne(1000);
                Assert.IsFalse(wasCalled);

                // notify worker
                worker.NotifyWork();

                // verify workAction has been called
                wasCalled = callEvent.WaitOne();
                Assert.IsTrue(wasCalled);

                // stop worker
                worker.Stop();

                // verify workAction has not been called
                wasCalled = callEvent.WaitOne(1000);
                Assert.IsFalse(wasCalled);
            }
        }
예제 #4
0
        public void TestNotifyWork()
        {
            // prepare workAction call tracking
            var         callEvent  = new AutoResetEvent(false);
            Func <Task> workAction = () => { callEvent.Set(); return(Task.CompletedTask); };

            // initialize worker
            using (var worker = new MockWorker(workAction))
            {
                // verify workAction has not been called
                var wasCalled = callEvent.WaitOne(1000);
                Assert.IsFalse(wasCalled);

                // start worker
                worker.Start();

                // verify workAction has not been called
                wasCalled = callEvent.WaitOne(1000);
                Assert.IsFalse(wasCalled);

                // notify worker
                worker.NotifyWork();

                // verify workAction has been called
                wasCalled = callEvent.WaitOne();
                Assert.IsTrue(wasCalled);

                // stop worker
                worker.Stop();

                // verify workAction has not been called
                wasCalled = callEvent.WaitOne(1000);
                Assert.IsFalse(wasCalled);
            }
        }
예제 #5
0
        public void TestStartNotified()
        {
            // prepare workAction call tracking
            var    callEvent  = new AutoResetEvent(false);
            Action workAction = () => callEvent.Set();

            // initialize worker
            using (var worker = new MockWorker(workAction))
            {
                // verify workAction has not been called
                var wasCalled = callEvent.WaitOne(10);
                Assert.IsFalse(wasCalled);

                // notify worker before it has been started
                worker.NotifyWork();

                // verify workAction has not been called
                wasCalled = callEvent.WaitOne(10);
                Assert.IsFalse(wasCalled);

                // start worker, it has already been notified
                worker.Start();

                // verify workAction has been called
                wasCalled = callEvent.WaitOne(1000);
                Assert.IsTrue(wasCalled);

                // stop worker
                worker.Stop();

                // verify workAction has not been called
                wasCalled = callEvent.WaitOne(10);
                Assert.IsFalse(wasCalled);
            }
        }
예제 #6
0
        public void TestRestartNotified()
        {
            // prepare workAction call tracking
            var    callEvent  = new AutoResetEvent(false);
            Action workAction = () => callEvent.Set();

            // initialize worker
            using (var worker = new MockWorker(workAction))
            {
                // start worker
                worker.Start();

                // verify workAction has not been called
                var wasCalled = callEvent.WaitOne(10);
                Assert.IsFalse(wasCalled);

                // notify worker
                worker.NotifyWork();

                // verify workAction has been called
                wasCalled = callEvent.WaitOne(10);
                Assert.IsTrue(wasCalled);

                // stop worker
                worker.Stop();

                // wait for worker to idle
                worker.WaitForIdle();

                // notify worker again
                worker.NotifyWork();

                //TODO calling callEvent.WaitOne(10) cancels the notify and fails the test, i'm not sure why
                //TODO calling Thread.Sleep(10) instead of callEvent.WaitOne has the same effect
                // verify workAction has not been called
                wasCalled = callEvent.WaitOne(10);
                Assert.IsFalse(wasCalled);

                // start worker again, it has already been notified
                worker.Start();

                // verify workAction has been called
                wasCalled = callEvent.WaitOne(1000);
                Assert.IsTrue(wasCalled);
            }
        }
예제 #7
0
        public void TestRestart()
        {
            // prepare workAction call tracking
            var    callEvent  = new AutoResetEvent(false);
            Action workAction = () => callEvent.Set();

            // initialize worker
            using (var worker = new MockWorker(workAction))
            {
                // start worker
                worker.Start();

                // verify workAction has not been called
                var wasCalled = callEvent.WaitOne(1000);
                Assert.IsFalse(wasCalled);

                // notify worker
                worker.NotifyWork();

                // verify workAction has been called
                wasCalled = callEvent.WaitOne(10);
                Assert.IsTrue(wasCalled);

                // stop worker
                worker.Stop();

                // wait for worker to idle
                worker.WaitForIdle();

                // verify workAction has not been called
                wasCalled = callEvent.WaitOne(10);
                Assert.IsFalse(wasCalled);

                // start worker again
                worker.Start();

                // notify worker
                worker.NotifyWork();

                // verify workAction has been called
                wasCalled = callEvent.WaitOne(10);
                Assert.IsTrue(wasCalled);
            }
        }