Пример #1
0
        public void TestPerformWork()
        {
            WaitAndContinueList work = new WaitAndContinueList();
            SampleWork          item1, item2;
            IWaitAndContinue    signaled;

            work.AddWork(item1 = new SampleWork());
            work.AddWork(item2 = new SampleWork());

            Assert.AreEqual(2, work.Count);
            Assert.IsFalse(work.IsEmpty);

            //normally done as a loop: while(work.Perform(timeout)) { }
            Assert.IsFalse(work.PerformWork(1));
            item2.Ready.Set();
            Assert.IsTrue(work.PerformWork(0));
            Assert.IsTrue(item2.Completed);
            Assert.IsTrue(item2.Disposed);

            Assert.IsFalse(work.PerformWork(1));
            item1.Cancel.Set();
            Assert.IsTrue(work.PerformWork(0, out signaled));
            Assert.IsTrue(ReferenceEquals(signaled, item1));
            Assert.IsTrue(item1.Completed);
            Assert.IsTrue(item1.Cancelled);
            Assert.IsTrue(item1.Disposed);

            Assert.IsTrue(work.IsEmpty);
            Assert.AreEqual(0, work.Count);
            Assert.IsFalse(work.PerformWork(1));
        }
Пример #2
0
        public void TestWaitAndContinueWorker()
        {
            WaitAndContinueWorker work;

            using (work = new WaitAndContinueWorker())
            {
                work.OnError += delegate(object o, ErrorEventArgs e) { throw new Exception("Failed.", e.GetException()); };

                SampleWork item = new SampleWork();
                work.AddWork(item);

                Assert.IsFalse(work.IsEmpty);

                item.Ready.Set();
                Assert.IsTrue(item.CompletedEvent.WaitOne(100, false));
                Assert.IsTrue(item.Completed);
                Assert.IsFalse(item.Cancelled);

                while (!item.Disposed)
                {
                    Thread.Sleep(0);
                }

                Assert.IsTrue(item.Disposed);
                Assert.IsTrue(work.IsEmpty);
            }
            Assert.IsTrue(work.IsEmpty);
        }
Пример #3
0
 public EntityAction Update(SampleWork entity)
 {
     unit.Context.Entry(entity).State = System.Data.Entity.EntityState.Modified;
     if (unit.SaveChanges() == EntityAction.Success)
     {
         return(EntityAction.Updated);
     }
     return(EntityAction.Exception);
 }
Пример #4
0
        public EntityAction Create(SampleWork entity)
        {
            unit.Context.SampleWork.Add(entity);

            if (unit.SaveChanges() == EntityAction.Success)
            {
                return(EntityAction.Added);
            }
            return(EntityAction.Exception);
        }
Пример #5
0
        public void TestAddWorkToDisposedWorker()
        {
            WaitAndContinueWorker work = new WaitAndContinueWorker();

            work.Complete(false, 100);
            work.Dispose();

            SampleWork item = new SampleWork();

            work.AddWork(item);
        }
Пример #6
0
        public void TestCompletedEnqueue()
        {
            WaitAndContinueList work = new WaitAndContinueList();
            SampleWork          item = new SampleWork();

            item.Completed = true;
            Assert.IsFalse(item.Disposed);
            work.AddWork(item);
            Assert.IsTrue(work.IsEmpty);
            Assert.IsTrue(item.Disposed);
        }
Пример #7
0
        public void TestAbortWork()
        {
            using (WaitAndContinueWorker work = new WaitAndContinueWorker())
            {
                work.OnError += delegate(object o, ErrorEventArgs e) { throw new Exception("Failed.", e.GetException()); };

                SampleWork item = new SampleWork();
                work.AddWork(item);

                Assert.IsFalse(work.IsEmpty);
                work.Abort();

                Assert.IsFalse(item.Completed);
                Assert.IsTrue(item.Disposed);
            }
        }
Пример #8
0
        public void TestHardAbort()
        {
            using (WaitAndContinueWorker work = new WaitAndContinueWorker())
            {
                work.OnError += delegate(object o, ErrorEventArgs e) { throw new Exception("Failed.", e.GetException()); };

                SampleWork item = new SampleWork();
                item.SleepForever = true;
                work.AddWork(item);

                item.Ready.Set();
                Assert.IsTrue(item.Sleeping.WaitOne(1000, false));
                work.Abort();

                Assert.IsFalse(item.Completed);
                Assert.IsTrue(item.Disposed);
            }
        }
Пример #9
0
        public void TestWorkerException()
        {
            using (WaitAndContinueWorker work = new WaitAndContinueWorker())
            {
                SampleWork item = new SampleWork();
                item.WorkThrows = true;
                work.AddWork(item);

                ManualResetEvent mreError = new ManualResetEvent(false);
                Exception        error    = null;
                work.OnError += delegate(object o, ErrorEventArgs e) { error = e.GetException(); mreError.Set(); };

                item.Ready.Set();
                Assert.IsTrue(mreError.WaitOne(1000, false));
                Assert.IsTrue(error is ArgumentOutOfRangeException);
                Assert.IsTrue(((ArgumentOutOfRangeException)error).ParamName == "WorkThrows");
            }
        }
Пример #10
0
        public void TestPerformDisposedWork()
        {
            WaitAndContinueList work = new WaitAndContinueList();
            SampleWork          item = new SampleWork();

            Assert.IsFalse(item.Disposed);
            work.AddWork(item);

            Assert.IsFalse(work.PerformWork(0));
            item.Dispose();

            Assert.IsFalse(work.PerformWork(0));

            item.Completed = true;//Normally this would be set in the Dispose method of the WorkItem, but we are testing
            Assert.IsFalse(work.PerformWork(0));

            Assert.IsTrue(work.IsEmpty);
            Assert.IsTrue(item.Disposed);
        }
Пример #11
0
        public void TestPerformByAbandonMutex()
        {
            Mutex  abandondMutex = new Mutex();
            Thread t             = new Thread(delegate() { abandondMutex.WaitOne(); });

            t.Start();
            t.Join();

            WaitAndContinueList work = new WaitAndContinueList();
            SampleWork          item = new SampleWork();

            item.Mutex = abandondMutex;

            Assert.IsFalse(item.Disposed);
            work.AddWork(item);
            Assert.IsTrue(work.PerformWork(0));
            Assert.IsTrue(work.IsEmpty);
            Assert.IsTrue(item.Disposed);
            Assert.IsTrue(item.MutexAcquired);
        }
Пример #12
0
        public void TestPerformWorkThrows()
        {
            WaitAndContinueList work = new WaitAndContinueList();
            SampleWork          item = new SampleWork();

            item.WorkThrows = true;
            work.AddWork(item);

            Assert.IsFalse(work.IsEmpty);
            Assert.IsFalse(work.PerformWork(1));
            item.Ready.Set();
            try
            {
                work.PerformWork(0);
                Assert.Fail("Expected exception.");
            }
            catch (ArgumentOutOfRangeException ae)
            {
                Assert.AreEqual("WorkThrows", ae.ParamName);
            }
            Assert.IsTrue(work.IsEmpty);
            Assert.IsTrue(item.Disposed);
            Assert.IsFalse(item.Completed); // incomplete, but still disposed... abandond due to exception.
        }
        public void TestPerformWorkThrows()
        {
            WaitAndContinueList work = new WaitAndContinueList();
            SampleWork item = new SampleWork();
            item.WorkThrows = true;
            work.AddWork(item);

            Assert.IsFalse(work.IsEmpty);
            Assert.IsFalse(work.PerformWork(1));
            item.Ready.Set();
            try
            {
                work.PerformWork(0);
                Assert.Fail("Expected exception.");
            }
            catch (ArgumentOutOfRangeException ae)
            {
                Assert.AreEqual("WorkThrows", ae.ParamName);
            }
            Assert.IsTrue(work.IsEmpty);
            Assert.IsTrue(item.Disposed);
            Assert.IsFalse(item.Completed); // incomplete, but still disposed... abandond due to exception.
        }
        public void TestAbortWork()
        {
            using (WaitAndContinueWorker work = new WaitAndContinueWorker())
            {
                work.OnError += delegate(object o, ErrorEventArgs e) { throw new Exception("Failed.", e.GetException()); };

                SampleWork item = new SampleWork();
                work.AddWork(item);

                Assert.IsFalse(work.IsEmpty);
                work.Abort();

                Assert.IsFalse(item.Completed);
                Assert.IsTrue(item.Disposed);
            }
        }
        public void TestPerformDisposedWork()
        {
            WaitAndContinueList work = new WaitAndContinueList();
            SampleWork item = new SampleWork();

            Assert.IsFalse(item.Disposed);
            work.AddWork(item);

            Assert.IsFalse(work.PerformWork(0));
            item.Dispose();

            Assert.IsFalse(work.PerformWork(0));

            item.Completed = true;//Normally this would be set in the Dispose method of the WorkItem, but we are testing
            Assert.IsFalse(work.PerformWork(0));

            Assert.IsTrue(work.IsEmpty);
            Assert.IsTrue(item.Disposed);
        }
 public void TestCompletedEnqueue()
 {
     WaitAndContinueList work = new WaitAndContinueList();
     SampleWork item = new SampleWork();
     item.Completed = true;
     Assert.IsFalse(item.Disposed);
     work.AddWork(item);
     Assert.IsTrue(work.IsEmpty);
     Assert.IsTrue(item.Disposed);
 }
        public void TestAddWorkToDisposedWorker()
        {
            WaitAndContinueWorker work = new WaitAndContinueWorker();
            work.Complete(false, 100);
            work.Dispose();

            SampleWork item = new SampleWork();
            work.AddWork(item);
        }
        public void TestWorkerException()
        {
            using (WaitAndContinueWorker work = new WaitAndContinueWorker())
            {
                SampleWork item = new SampleWork();
                item.WorkThrows = true;
                work.AddWork(item);

                ManualResetEvent mreError = new ManualResetEvent(false);
                Exception error = null;
                work.OnError += delegate(object o, ErrorEventArgs e) { error = e.GetException(); mreError.Set(); };

                item.Ready.Set();
                Assert.IsTrue(mreError.WaitOne(1000, false));
                Assert.IsTrue(error is ArgumentOutOfRangeException);
                Assert.IsTrue(((ArgumentOutOfRangeException)error).ParamName == "WorkThrows");
            }
        }
        public void TestHardAbort()
        {
            using (WaitAndContinueWorker work = new WaitAndContinueWorker())
            {
                work.OnError += delegate(object o, ErrorEventArgs e) { throw new Exception("Failed.", e.GetException()); };

                SampleWork item = new SampleWork();
                item.SleepForever = true;
                work.AddWork(item);

                item.Ready.Set();
                Assert.IsTrue(item.Sleeping.WaitOne(1000, false));
                work.Abort();

                Assert.IsFalse(item.Completed);
                Assert.IsTrue(item.Disposed);
            }
        }
        public void TestPerformByAbandonMutex()
        {
            Mutex abandondMutex = new Mutex();
            Thread t = new Thread(delegate() { abandondMutex.WaitOne(); });
            t.Start();
            t.Join();

            WaitAndContinueList work = new WaitAndContinueList();
            SampleWork item = new SampleWork();
            item.Mutex = abandondMutex;

            Assert.IsFalse(item.Disposed);
            work.AddWork(item);
            Assert.IsTrue(work.PerformWork(0));
            Assert.IsTrue(work.IsEmpty);
            Assert.IsTrue(item.Disposed);
            Assert.IsTrue(item.MutexAcquired);
        }
        public void TestPerformWork()
        {
            WaitAndContinueList work = new WaitAndContinueList();
            SampleWork item1, item2;
            IWaitAndContinue signaled;

            work.AddWork(item1 = new SampleWork());
            work.AddWork(item2 = new SampleWork());

            Assert.AreEqual(2, work.Count);
            Assert.IsFalse(work.IsEmpty);

            //normally done as a loop: while(work.Perform(timeout)) { }
            Assert.IsFalse(work.PerformWork(1));
            item2.Ready.Set();
            Assert.IsTrue(work.PerformWork(0));
            Assert.IsTrue(item2.Completed);
            Assert.IsTrue(item2.Disposed);

            Assert.IsFalse(work.PerformWork(1));
            item1.Cancel.Set();
            Assert.IsTrue(work.PerformWork(0, out signaled));
            Assert.IsTrue(ReferenceEquals(signaled, item1));
            Assert.IsTrue(item1.Completed);
            Assert.IsTrue(item1.Cancelled);
            Assert.IsTrue(item1.Disposed);

            Assert.IsTrue(work.IsEmpty);
            Assert.AreEqual(0, work.Count);
            Assert.IsFalse(work.PerformWork(1));
        }
        public void TestWaitAndContinueWorker()
        {
            WaitAndContinueWorker work;
            using (work = new WaitAndContinueWorker())
            {
                work.OnError += delegate(object o, ErrorEventArgs e) { throw new Exception("Failed.", e.GetException()); };

                SampleWork item = new SampleWork();
                work.AddWork(item);

                Assert.IsFalse(work.IsEmpty);

                item.Ready.Set();
                Assert.IsTrue(item.CompletedEvent.WaitOne(100, false));
                Assert.IsTrue(item.Completed);
                Assert.IsFalse(item.Cancelled);

                while (!item.Disposed)
                    Thread.Sleep(0);

                Assert.IsTrue(item.Disposed);
                Assert.IsTrue(work.IsEmpty);
            }
            Assert.IsTrue(work.IsEmpty);
        }