public void semaphore_can_be_acquired_only_after_all_acquisitions_are_released() { var pool = new SemaphorePool(1); var task11 = pool.Acquire("1"); var task12 = pool.Acquire("1"); var task13 = pool.Acquire("1"); var semaphore1 = task11.Result; Assert.IsNotNull(semaphore1); Assert.AreEqual(semaphore1, task12.Result, "both semaphores should be equal"); Assert.AreEqual(semaphore1, task13.Result, "Both semaphores should be equal"); var task2 = pool.Acquire("2"); Thread.Sleep(5); Assert.IsFalse(task2.IsCompleted); pool.Release("1"); Thread.Sleep(5); Assert.IsFalse(task2.IsCompleted); pool.Release("1"); Thread.Sleep(5); Assert.IsFalse(task2.IsCompleted); pool.Release("1"); var semaphore2 = task2.Result; Assert.AreEqual(semaphore2, semaphore1, "Semaphore1 and Semaphore2 should be equal"); }
public void NoErrorWhenManySimilarRequestsAreMade() { var pool = new SemaphorePool(10, ExceptionHandlingStrategy.THROW); var tasks = new List <Task>(); for (var i = 0; i < 300; i++) { tasks.Add(Task.Run(async() => { var stringId = StaticRandom.S(10); var semaphore = await pool.AcquireAsync(stringId).ConfigureAwait(false); Assert.IsNotNull(semaphore); await semaphore.WaitAsync().ConfigureAwait(false); Assert.IsTrue(pool.AvailableSemaphoreIds.Contains(stringId)); Thread.Sleep(StaticRandom.I100); semaphore.ReleaseSafe(); pool.Release(stringId); })); } Task.WaitAll(tasks.ToArray()); Assert.IsTrue(tasks.All(a => a.IsCompletedSuccessfully)); Assert.AreEqual(10, pool.SemaphoreHolders.Count); Assert.AreEqual(0, pool.AvailableSemaphoreIds.Count); Assert.IsNull(pool.SemaphoreHolders[0].Id); Assert.IsTrue(pool.SemaphoreHolders.All(a => string.IsNullOrEmpty(a.Id) && a.UsageCount == 0)); }
public void NoErrorWhenManyRequestsAreMade() { var pool = new SemaphorePool(10, ExceptionHandlingStrategy.THROW); var tasks = new List <Task>(); var id = 0; for (var i = 0; i < 300; i++) { tasks.Add(Task.Run(() => { var stringId = id.ToString(); id++; var semaphore = pool.AcquireAsync(stringId).Result; Assert.IsNotNull(semaphore); Thread.Sleep(10); pool.Release(stringId); })); } Task.WaitAll(tasks.ToArray()); Assert.IsTrue(tasks.All(a => a.IsCompletedSuccessfully)); Assert.AreEqual(10, pool.SemaphoreHolders.Count); Assert.AreEqual(0, pool.AvailableSemaphoreIds.Count); Assert.IsNull(pool.SemaphoreHolders[0].Id); Assert.IsTrue(pool.SemaphoreHolders.All(a => string.IsNullOrEmpty(a.Id) && a.UsageCount == 0)); }
public void SemaphoreCanBeAcquiredOnlyAfterAllAcquisitionsAreReleased() { var pool = new SemaphorePool(1, ExceptionHandlingStrategy.THROW); var task11 = pool.AcquireAsync("1"); var task12 = pool.AcquireAsync("1"); var task13 = pool.AcquireAsync("1"); var semaphore1 = task11.Result; Assert.IsNotNull(semaphore1); Assert.AreEqual(semaphore1, task12.Result, "both semaphores should be equal"); Assert.AreEqual(semaphore1, task13.Result, "Both semaphores should be equal"); Assert.AreEqual(1, pool.SemaphoreHolders.Count); Assert.AreEqual(1, pool.AvailableSemaphoreIds.Count); Assert.AreEqual("1", pool.AvailableSemaphoreIds.First()); Assert.AreEqual("1", pool.SemaphoreHolders[0].Id); Assert.AreEqual(3, pool.SemaphoreHolders[0].UsageCount); var task2 = pool.AcquireAsync("2"); Thread.Sleep(5); Assert.IsFalse(task2.IsCompleted); pool.Release("1"); Thread.Sleep(5); Assert.IsFalse(task2.IsCompleted); pool.Release("1"); Thread.Sleep(5); Assert.IsFalse(task2.IsCompleted); pool.Release("1"); var semaphore2 = task2.Result; Assert.AreEqual(semaphore2, semaphore1, "Semaphore1 and Semaphore2 should be equal"); Assert.AreEqual(1, pool.SemaphoreHolders.Count); Assert.AreEqual(1, pool.AvailableSemaphoreIds.Count); Assert.AreEqual("2", pool.SemaphoreHolders[0].Id); Assert.AreEqual(1, pool.SemaphoreHolders[0].UsageCount); }
public void complex_usage_test() { var pool = new SemaphorePool(2); var task11 = pool.Acquire("1"); var task21 = pool.Acquire("2"); Thread.Sleep(5); var task12 = pool.Acquire("1"); var task22 = pool.Acquire("2"); var task3 = pool.Acquire("3"); Thread.Sleep(5); var semaphore11 = task11.Result; var semaphore12 = task12.Result; var semaphore21 = task21.Result; var semaphore22 = task22.Result; Assert.IsNotNull(semaphore11); Assert.AreEqual(semaphore12, semaphore11, "semaphore11 and semaphore12 should be equal"); Assert.IsNotNull(semaphore21); Assert.AreEqual(semaphore21, semaphore22, "semaphore21 and semaphore22 should be equal"); Assert.IsFalse(task3.IsCompleted); pool.Release("1"); Thread.Sleep(5); Assert.IsFalse(task3.IsCompleted); pool.Release("1"); Thread.Sleep(5); Assert.IsTrue(task3.IsCompleted); var task4 = pool.Acquire("4"); Thread.Sleep(5); Assert.IsFalse(task4.IsCompleted); pool.Release("2"); Thread.Sleep(5); Assert.IsFalse(task4.IsCompleted); pool.Release("2"); Thread.Sleep(5); Assert.IsTrue(task4.IsCompleted); pool.Release("3"); pool.Release("4"); Thread.Sleep(5); var task1 = pool.Acquire("1"); var task2 = pool.Acquire("2"); Thread.Sleep(5); Assert.IsTrue(task1.IsCompleted); Assert.IsTrue(task2.IsCompleted); }
public void released_semaphore_can_be_re_acquired() { var pool = new SemaphorePool(1); var task1 = pool.Acquire("1"); Thread.Sleep(5); var task2 = pool.Acquire("2"); var semaphore1 = task1.Result; Assert.IsNotNull(semaphore1); Assert.IsFalse(task2.IsCompleted); pool.Release("1"); var semaphore2 = task2.Result; Assert.AreEqual(semaphore2, semaphore1, "Semaphore1 and Semaphore2 should be equal"); }
public void no_error_when_many_requests_are_made() { var pool = new SemaphorePool(15); var tasks = new List <Task>(); var id = 0; for (var i = 0; i < 300; i++) { tasks.Add(Task.Run(() => { var stringId = id.ToString(); id++; var semaphore = pool.Acquire(stringId).Result; Assert.IsNotNull(semaphore); Thread.Sleep(10); pool.Release(stringId); })); } Task.WaitAll(tasks.ToArray()); }
public void ReleasingUnusedSemaphoreThrows() { var pool = new SemaphorePool(1, ExceptionHandlingStrategy.THROW); pool.Release("1"); }
public void ComplexUsageTest() { var pool = new SemaphorePool(2, ExceptionHandlingStrategy.THROW); var task11 = pool.AcquireAsync("1"); var task21 = pool.AcquireAsync("2"); Thread.Sleep(5); var task12 = pool.AcquireAsync("1"); var task22 = pool.AcquireAsync("2"); var task3 = pool.AcquireAsync("3"); Thread.Sleep(5); var semaphore11 = task11.Result; var semaphore12 = task12.Result; var semaphore21 = task21.Result; var semaphore22 = task22.Result; Assert.IsNotNull(semaphore11); Assert.AreEqual(semaphore12, semaphore11, "semaphore11 and semaphore12 should be equal"); Assert.IsNotNull(semaphore21); Assert.AreEqual(semaphore21, semaphore22, "semaphore21 and semaphore22 should be equal"); Assert.IsFalse(task3.IsCompleted); pool.Release("1"); Thread.Sleep(5); Assert.IsFalse(task3.IsCompleted); pool.Release("1"); Thread.Sleep(5); Assert.IsTrue(task3.IsCompleted); var task4 = pool.AcquireAsync("4"); Thread.Sleep(5); Assert.IsFalse(task4.IsCompleted); pool.Release("2"); Thread.Sleep(5); Assert.IsFalse(task4.IsCompleted); pool.Release("2"); Thread.Sleep(5); Assert.IsTrue(task4.IsCompleted); pool.Release("3"); pool.Release("4"); Thread.Sleep(5); var task1 = pool.AcquireAsync("1"); var task2 = pool.AcquireAsync("2"); Thread.Sleep(5); Assert.IsTrue(task1.IsCompleted); Assert.IsTrue(task2.IsCompleted); pool.Release("1"); pool.Release("2"); Assert.AreEqual(2, pool.SemaphoreHolders.Count); Assert.AreEqual(0, pool.AvailableSemaphoreIds.Count); Assert.IsTrue(pool.SemaphoreHolders.All(a => string.IsNullOrEmpty(a.Id) && a.UsageCount == 0)); }
public void releasing_unused_semaphore_throws() { var pool = new SemaphorePool(1); pool.Release("1"); }