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); }
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(); }
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); }
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(); }
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(); }
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(); }