コード例 #1
0
        public void TenEventUnhandledExceptionTest()
        {
            EventClass[] events = new EventClass[10];

            Action <Exception> onFail =
                delegate(Exception e)
            {
                EventException err = ( EventException )e;
                events[err.Index].Release();
            };

            using (EventExecutor executor = new EventExecutor())
            {
                executor.OnError += onFail;
                executor.Start();
                for (int i = 0; i < 10; ++i)
                {
                    EventClass     ec        = new EventClass();
                    EventException exception = new EventException(i);
                    events[i] = ec;
                    executor.AddEvent(() => ec.ExecuteAndThrow(exception));
                }

                foreach (EventClass ec in events)
                {
                    Assert.IsTrue(ec.Join(60 * 1000));     // We should not hang if this works.
                    Assert.IsTrue(ec.Executed);
                }
            }
        }
コード例 #2
0
        public void InterruptTest()
        {
            EventClass longEvent  = new EventClass();
            EventClass shortEvent = new EventClass();

            Exception err = null;

            using (InterruptibleEventExecutor uut = new InterruptibleEventExecutor(5000))
            {
                uut.OnError += delegate(Exception e)
                {
                    err = e;
                };

                uut.Start();
                uut.AddEvent(
                    delegate()
                {
                    Thread.Sleep(10000);
                    longEvent.Execute();
                }
                    );

                // Ensure getting a TheadInterruptedException doesn't
                // napalm our event executor by ensuring a second event
                // will execute.
                uut.AddEvent(
                    delegate()
                {
                    shortEvent.Execute();
                }
                    );

                Assert.IsTrue(shortEvent.Join(30 * 1000));
                Assert.IsTrue(shortEvent.Executed);
                Assert.IsFalse(longEvent.Executed);   // Should have been interrupted.
            }

            Assert.IsNotNull(err);
            Assert.IsTrue(err is ThreadInterruptedException);
        }
コード例 #3
0
        public void ThousandEventTest()
        {
            using (InterruptibleEventExecutor executor = new InterruptibleEventExecutor(5000))
            {
                executor.Start();

                EventClass[] events = new EventClass[1000];
                for (int i = 0; i < 1000; ++i)
                {
                    EventClass ec = new EventClass();
                    events[i] = ec;
                    executor.AddEvent(() => ec.Execute());
                }

                foreach (EventClass ec in events)
                {
                    // Wait for everything to complete before calling Asset.
                    Assert.IsTrue(ec.Join(30 * 1000));
                    Assert.IsTrue(ec.Executed);
                }
            }
        }
コード例 #4
0
        public void NoInterruptTest()
        {
            EventClass longEvent  = new EventClass();
            EventClass shortEvent = new EventClass();

            Exception err = null;

            using (InterruptibleEventExecutor uut = new InterruptibleEventExecutor(int.MaxValue))
            {
                uut.OnError += delegate(Exception e)
                {
                    err = e;
                };

                uut.Start();
                uut.AddEvent(
                    delegate()
                {
                    Thread.Sleep(10000);
                    longEvent.Execute();
                }
                    );

                uut.AddEvent(
                    delegate()
                {
                    shortEvent.Execute();
                }
                    );

                Assert.IsTrue(longEvent.Join(30 * 1000));
                Assert.IsTrue(longEvent.Executed);
                Assert.IsTrue(shortEvent.Join(30 * 1000));
                Assert.IsTrue(shortEvent.Executed);
            }

            Assert.IsNull(err);
        }