Ejemplo n.º 1
0
        public void TestMaxInsideInTwoRegions()
        {
            var region = new ThrottledRegion(1, 1, Timeout.Infinite);
            var key = 1;
            var key2 = 2;
            Thread t1 = new Thread(() => { TryEnterSuccess(region, key); }); // enter region1
            Thread t2 = new Thread(() => { TryEnterSuccess(region, key); }); // wait region1
            Thread t3 = new Thread(() => { TryEnterFail(region, key); }); // Fail region1

            Thread t4 = new Thread(() => { TryEnterSuccess(region, key2); }); // enter region2
            Thread t5 = new Thread(() => { TryEnterSuccess(region, key2); }); // wait region2
            Thread t6 = new Thread(() => { TryEnterFail(region, key2); }); // fail region2

            t1.Start();
            t1.Join();
            t2.Start(); // will wait
            Thread.Sleep(100); //give some time to run before the t3
            t3.Start();
            t3.Join();
            region.Leave(key);
            t2.Join();

            t4.Start();
            t4.Join();
            t5.Start(); // will wait
            Thread.Sleep(100); //give some time to run before the t6
            t6.Start();
            t6.Join();
            region.Leave(key2);
            t5.Join();
        }
Ejemplo n.º 2
0
        public void Test_RegionFull()
        {
            ThrottledRegion tr      = new ThrottledRegion(1, 1, Timeout.Infinite);
            bool            waiting = false;

            new Thread(() => {
                try
                {
                    Assert.IsTrue(tr.TryEnter(1));
                    waiting = true;
                    tr.Leave(1);
                }
                catch (Exception e)
                {
                    exceptions.Add(e);
                }
            }).Start();
            while (!waiting)
            {
                ;
            }
            int start = Environment.TickCount;

            Assert.IsTrue(tr.TryEnter(1));
            int elapsed = Environment.TickCount - start;

            tr.Leave(1);
            Assert.AreEqual(0, exceptions.Count);
        }
Ejemplo n.º 3
0
        public void TestTimeout()
        {
            var region = new ThrottledRegion(2, 2, 1000);
            var key = 1;
            Thread t1 = new Thread(() => { TryEnterSuccess(region, key); }); // enter
            Thread t2 = new Thread(() => { TryEnterSuccess(region, key); }); // enter
            Thread t3 = new Thread(() => { TryEnterFail(region, key); }); // wait

            t1.Start();
            t2.Start();
            t1.Join();
            t2.Join();
            t3.Start();
            Thread.Sleep(3000); // make sure t3 gets the timeout
            t3.Join();
        }
Ejemplo n.º 4
0
        public void TestInterruptedEException()
        {
            maxInside   = 1;
            maxWaiting  = 1;
            waitTimeout = Timeout.Infinite;
            ThrottledRegion region = new ThrottledRegion(maxInside, maxWaiting, waitTimeout);
            Thread          t1     = new Thread(() => { doWork(region, 1, true); });
            Thread          t2     = new Thread(() => { doWork(region, 2, true); });
            Thread          t3     = new Thread(() => { doWork(region, 3, false); });

            t1.Start(); // t1 entra
            t1.Join();
            t2.Start(); // t2 fica em espera
            Thread.Sleep(100);
            t2.Interrupt();
            t2.Join();
        }
Ejemplo n.º 5
0
        public void TestSucess()
        {
            var region = new ThrottledRegion(2, 2, Timeout.Infinite);
            var key = 1;
            Thread t1 = new Thread(() => { TryEnterSuccess(region, key); }); // enter
            Thread t2 = new Thread(() => { TryEnterSuccess(region, key); }); // enter
            Thread t3 = new Thread(() => { TryEnterSuccess(region, key); }); // wait

            t1.Start();
            t2.Start();
            t1.Join();
            t2.Join();
            t3.Start();
            Thread.Sleep(1000); // make sure t3 is waiting
            region.Leave(key); // t3 can enter
            t3.Join();
        }
Ejemplo n.º 6
0
        public void TestTimeout()
        {
            maxInside   = 2;
            maxWaiting  = 2;
            waitTimeout = 1000;
            ThrottledRegion region = new ThrottledRegion(maxInside, maxWaiting, waitTimeout);
            Thread          t1     = new Thread(() => { doWork(region, 1, true); });
            Thread          t2     = new Thread(() => { doWork(region, 2, true); });
            Thread          t3     = new Thread(() => { doWork(region, 3, false); });

            t1.Start(); // t1 entra
            t2.Start(); // t2 entra
            t1.Join();
            t2.Join();
            t3.Start();
            Thread.Sleep(2000); // para t3 sair por timeout
            t3.Join();
        }
Ejemplo n.º 7
0
        public void TestMaxInsideFull()
        {
            maxInside   = 1;
            maxWaiting  = 1;
            waitTimeout = Timeout.Infinite;
            ThrottledRegion region = new ThrottledRegion(maxInside, maxWaiting, waitTimeout);
            Thread          t1     = new Thread(() => { doWork(region, 1, true); });
            Thread          t2     = new Thread(() => { doWork(region, 2, true); });
            Thread          t3     = new Thread(() => { doWork(region, 3, false); });

            t1.Start();        // t1 entra
            t1.Join();
            t2.Start();        // t2 fica em espera
            Thread.Sleep(100);
            t3.Start();        // sai pq nao há espaço
            t3.Join();
            region.Leave(key); // t2 entra
            t2.Join();
        }
Ejemplo n.º 8
0
        public void TestSuccess()
        {
            maxInside   = 2;
            maxWaiting  = 2;
            waitTimeout = Timeout.Infinite; // para nunca sairem por timeout
            ThrottledRegion region = new ThrottledRegion(maxInside, maxWaiting, waitTimeout);
            Thread          t1     = new Thread(() => { doWork(region, 1, true); });
            Thread          t2     = new Thread(() => { doWork(region, 2, true); });
            Thread          t3     = new Thread(() => { doWork(region, 3, true); });

            t1.Start(); // t1 entra
            t2.Start(); // t2 entra
            t1.Join();
            t2.Join();
            t3.Start();
            Thread.Sleep(1000); // t3 fica em espera
            region.Leave(key);  // t3 entra
            t3.Join();
        }
Ejemplo n.º 9
0
 private void doWork(ThrottledRegion tregion, int threadId, bool expected)
 {
     try
     {
         bool res = tregion.TryEnter(key);
         Console.WriteLine("thread " + threadId + " returned " + res);
         if (expected)
         {
             Assert.IsTrue(res);
         }
         else
         {
             Assert.IsFalse(res);
         }
     } catch (ThreadInterruptedException e)
     {
         Console.WriteLine("thread " + threadId + " returned " + e.Message);
         Assert.Fail();
     }
 }
Ejemplo n.º 10
0
        public void Test_InterruptedThread()
        {
            ThrottledRegion tr = new ThrottledRegion(1, 1, Timeout.Infinite);

            Assert.IsTrue(tr.TryEnter(1));

            Thread t1 = new Thread(() => {
                try
                {
                    tr.TryEnter(1);
                }
                catch (ThreadInterruptedException e)
                {
                    exceptions.Add(e);
                }
            });

            t1.Start();
            t1.Interrupt();
            t1.Join();

            Assert.AreEqual(1, exceptions.Count);
        }
Ejemplo n.º 11
0
        public void Test_MultipleThreadsInside()
        {
            ThrottledRegion tr = new ThrottledRegion(2, 1, Timeout.Infinite);

            Thread t1 = new Thread(() => {
                try
                {
                    Assert.IsTrue(tr.TryEnter(1));
                    Thread.Sleep(150);
                    tr.Leave(1);
                }
                catch (Exception e)
                {
                    exceptions.Add(e);
                }
            });

            t1.Start();
            Assert.IsTrue(tr.TryEnter(1));
            tr.Leave(1);
            t1.Join();

            Assert.AreEqual(0, exceptions.Count);
        }
Ejemplo n.º 12
0
 public void TryEnterSuccess(ThrottledRegion region, int key)
 {
     Assert.IsTrue(region.TryEnter(key));
 }
Ejemplo n.º 13
0
 public void TryEnterFail(ThrottledRegion region, int key)
 {
     Assert.IsFalse(region.TryEnter(key));
 }