Пример #1
0
        public void BasicSuspendResumeTest()
        {
            Exception e = null;
            AutoResetEvent even = new AutoResetEvent(false);
            int y = 0;
            var a = new Arbitr<Message, NOPMessage>(
                (z) =>
                {
                    y += z.x;
                    even.Set();
                },
                (z) =>
                {
                    e = z;
                    even.Set();
                });

            a.Send(new Message() { x = 1 });
            bool completed = even.WaitOne(500);
            Assert.AreEqual(y, 1);
            Assert.IsTrue(completed);
            Assert.IsNull(e);

            a.Suspend();

            y = 0;
            a.Send(new Message() { x = 1 });
            completed = even.WaitOne(100);
            Assert.AreEqual(y, 0);
            Assert.IsFalse(completed);
            Assert.IsNull(e);

            a.Resume();

            completed = even.WaitOne(500);
            Assert.AreEqual(y, 1);
            Assert.IsTrue(completed);
            Assert.IsNull(e);
        }
Пример #2
0
        public void SuspendInTask()
        {
            Exception e = null;
            AutoResetEvent even = new AutoResetEvent(false);
            AutoResetEvent evenT = new AutoResetEvent(false);
            int y = 0;
            int t = 0;
            Arbitr<Message, NOPMessage> a = null;
            a = new Arbitr<Message, NOPMessage>(
                (z) =>
                {
                    y += z.x;
                    a.AddUserTaskToLine(new Task(() =>
                    {
                        t++;
                        a.Suspend();
                        evenT.Set();
                    }));
                    even.Set();
                },
                (z) =>
                {
                    e = z;
                    even.Set();
                });

            a.Suspend();
            a.Send(new Message() { x = 1 });
            a.Send(new Message() { x = -1 });
            a.Resume();
            bool completed = even.WaitOne(500);
            Assert.AreEqual(y, 1);
            Assert.IsTrue(completed);
            Assert.IsNull(e);

            completed = evenT.WaitOne(500);
            Assert.AreEqual(t, 1);
            Assert.IsTrue(completed);
            Assert.IsNull(e);
            Assert.AreEqual(y, 1);
            Assert.IsTrue(a.Suspended);

            completed = even.WaitOne(100);
            Assert.IsFalse(completed);

            a.Resume();

            completed = even.WaitOne(500);
            Assert.AreEqual(y, 0);
            Assert.IsTrue(completed);
            Assert.IsNull(e);

            completed = evenT.WaitOne(500);
            Assert.AreEqual(t, 2);
            Assert.IsTrue(completed);
            Assert.IsNull(e);
            Assert.AreEqual(y, 0);
            Assert.IsTrue(a.Suspended);
        }
Пример #3
0
        public void MultiThreadSuspendTest()
        {
            Exception e = null;

            Arbitr<Message, NOPMessage> a = null;
            a = new Arbitr<Message, NOPMessage>((x) => { MessageProc(x, a); },
                (x) => { e = x; });

            int SuspendCount = 0;

            Action<Func<Message>, Arbitr<Message, NOPMessage>, int> tr1 = MsgSender;
            Action<Func<Message>, Arbitr<Message, NOPMessage>, int> tr2 = MsgSender;
            Action tr3 = () =>
            {
                while (ActiveMsgCount > 0)
                {

                    a.Resume();
                    SuspendCount++;
                    Thread.Sleep(10);
                    a.Suspend();
                    Thread.Sleep(10);
                }
                a.Resume();
            };

            st.Reset();
            st.Start();
            a.Suspend();
            TSend(a, new Message());
            var t3 = tr3.BeginInvoke(null, null);
            var t1 = tr1.BeginInvoke(() => { return new Message(); }, a, TotalMessageCount, null, null);
            var t2 = tr2.BeginInvoke(() => { return new Message(); }, a, TotalMessageCount, null, null);

            tr1.EndInvoke(t1);
            tr2.EndInvoke(t2);
            tr3.EndInvoke(t3);

            while (ActiveMsgCount > 0) { Thread.SpinWait(0); }

            st.Stop();
            Console.WriteLine("Произведенно {0} суспендов", SuspendCount);
            Console.WriteLine("Скорость обработки  сообщений {0} в мс",
                TotalMessageCount / (st.ElapsedMilliseconds != 0 ? st.ElapsedMilliseconds : 1));

            Assert.AreEqual(SharedVar, 0);
            Assert.IsNull(e);
        }