Ejemplo n.º 1
0
        public void TestSemaphoreWriterssFirst()
        {
            RwSemaphore sem = new RwSemaphore();

            sem.DownWrite();
            sem.UpWrite();
            Assert.AreEqual(sem.getWriters(), 0);
            sem.DownRead();
            sem.DownRead();
            Assert.AreEqual(sem.getReaders(), 2);
            sem.UpRead();
            sem.UpRead();
        }
        public void Test_RwFlowWithDowngradeWriter()
        {
            RwSemaphore s = new RwSemaphore();

            Thread t4 = new Thread(() => {
                try
                {
                    s.DownRead();
                    Assert.IsTrue(s.getWaitingCount() == 0);
                    s.UpRead();
                }
                catch (Exception e)
                {
                    exceptions.Add(e);
                }
            });
            Thread t3 = new Thread(() => {
                try
                {
                    s.DownWrite();
                    Assert.IsTrue(s.getWaitingCount() == 1);
                    t4.Start();
                    s.DowngradeWriter();
                    Assert.IsTrue(s.getWaitingCount() == 0);
                }
                catch (Exception e)
                {
                    exceptions.Add(e);
                }
            });
            Thread t2 = new Thread(() => {
                try
                {
                    s.DownRead();
                    Assert.IsTrue(s.getWaitingCount() == 0);
                    t3.Start();
                    s.UpRead();
                    Assert.IsTrue(s.getWaitingCount() == 0);
                }
                catch (Exception e)
                {
                    exceptions.Add(e);
                }
            });
            Thread t1 = new Thread(() => {
                try
                {
                    s.DownRead();
                    Assert.IsTrue(s.getWaitingCount() == 0);
                    t2.Start();
                    s.UpRead();
                    Assert.IsTrue(s.getWaitingCount() == 0);
                }
                catch (Exception e)
                {
                    exceptions.Add(e);
                }
            });

            t1.Start();

            Assert.AreEqual(0, exceptions.Count);
        }
        public void Test_RwFlow()
        {
            RwSemaphore s = new RwSemaphore();

            Thread t4 = new Thread(() => {
                try
                {
                    s.DownRead();
                    Assert.IsTrue(s.getWaitingCount() == 1);
                    bool exception = false;
                    try
                    {
                        s.UpWrite();
                    }
                    catch (InvalidOperationException)
                    {
                        exception = true;
                    }
                    Assert.IsTrue(exception);
                    s.UpRead();
                }
                catch (Exception e)
                {
                    exceptions.Add(e);
                }
            });
            Thread t3 = new Thread(() => {
                try
                {
                    s.DownWrite();
                    Assert.IsTrue(s.getWaitingCount() == 1);
                    t4.Start();
                    s.DownWrite();
                    Assert.IsTrue(s.getWaitingCount() == 0);
                }
                catch (Exception e)
                {
                    exceptions.Add(e);
                }
            });
            Thread t2 = new Thread(() => {
                try
                {
                    s.DownRead();
                    Assert.IsTrue(s.getWaitingCount() == 0);
                    t3.Start();
                    s.UpRead();
                    Assert.IsTrue(s.getWaitingCount() == 0);
                }
                catch (Exception e)
                {
                    exceptions.Add(e);
                }
            });
            Thread t1 = new Thread(() => {
                try
                {
                    s.DownRead();
                    Assert.IsTrue(s.getWaitingCount() == 0);
                    t2.Start();
                    s.UpRead();
                    Assert.IsTrue(s.getWaitingCount() == 0);
                }
                catch (Exception e)
                {
                    exceptions.Add(e);
                }
            });

            t1.Start();

            Assert.AreEqual(0, exceptions.Count);
        }