/// <summary> /// Test SemaphoreSlim AvailableWaitHandle property /// </summary> /// <param name="initial">The initial semaphore count</param> /// <param name="maximum">The maximum semaphore count</param> /// <param name="action">SemaphoreSlim action to be called before WaitHandle</param> /// <param name="state">The expected wait handle state</param> /// <returns>True if the test succeeded, false otherwise</returns> private static void RunSemaphoreSlimTest7_AvailableWaitHandle(int initial, int maximum, SemaphoreSlimActions? action, bool state) { SemaphoreSlim semaphore = new SemaphoreSlim(initial, maximum); try { CallSemaphoreAction(semaphore, action, null); if (semaphore.AvailableWaitHandle == null) { string methodFailed = "RunSemaphoreSlimTest7_AvailableWaitHandle(" + initial + "," + maximum + "," + action + "): FAILED. "; Assert.True(false, string.Format(methodFailed + "AvailableWaitHandle failed, handle is null.")); } if (semaphore.AvailableWaitHandle.WaitOne(0) != state) { string methodFailed = "RunSemaphoreSlimTest7_AvailableWaitHandle(" + initial + "," + maximum + "," + action + "): FAILED. "; Assert.True(false, string.Format(methodFailed + "AvailableWaitHandle failed, expected " + state + " actual " + !state)); } } catch (Exception ex) { string methodFailed = "RunSemaphoreSlimTest7_AvailableWaitHandle(" + initial + "," + maximum + "," + action + "): FAILED. "; Assert.True(false, string.Format(methodFailed + "AvailableWaitHandle failed, the code threw exception " + ex)); } }
/// <summary> /// Test SemaphoreSlim AvailableWaitHandle property /// </summary> /// <param name="initial">The initial semaphore count</param> /// <param name="maximum">The maximum semaphore count</param> /// <param name="action">SemaphoreSlim action to be called before WaitHandle</param> /// <param name="state">The expected wait handle state</param> /// <returns>True if the test succeeded, false otherwise</returns> private static void RunSemaphoreSlimTest7_AvailableWaitHandle(int initial, int maximum, SemaphoreSlimActions? action, bool state) { SemaphoreSlim semaphore = new SemaphoreSlim(initial, maximum); CallSemaphoreAction(semaphore, action, null); Assert.NotNull(semaphore.AvailableWaitHandle); Assert.Equal(state, semaphore.AvailableWaitHandle.WaitOne(0)); }
/// <summary> /// Test SemaphoreSlim CurrentCount property /// </summary> /// <param name="initial">The initial semaphore count</param> /// <param name="maximum">The maximum semaphore count</param> /// <param name="action">SemaphoreSlim action to be called before CurentCount</param> /// <returns>True if the test succeeded, false otherwise</returns> private static void RunSemaphoreSlimTest5_CurrentCount(int initial, int maximum, SemaphoreSlimActions? action) { SemaphoreSlim semaphore = new SemaphoreSlim(initial, maximum); CallSemaphoreAction(semaphore, action, null); if (action == null) { Assert.Equal(initial, semaphore.CurrentCount); } else { Assert.Equal(initial + (action == SemaphoreSlimActions.Release ? 1 : -1), semaphore.CurrentCount); } }
/// <summary> /// Test SemaphoreSlim Dispose /// </summary> /// <param name="initial">The initial semaphore count</param> /// <param name="maximum">The maximum semaphore count</param> /// <param name="action">SemaphoreSlim action to be called after Dispose</param> /// <param name="exceptionType">The type of the thrown exception in case of invalid cases, /// null for valid cases</param> /// <returns>True if the test succeeded, false otherwise</returns> private static void RunSemaphoreSlimTest4_Dispose(int initial, int maximum, SemaphoreSlimActions? action, Type exceptionType) { SemaphoreSlim semaphore = new SemaphoreSlim(initial, maximum); try { semaphore.Dispose(); CallSemaphoreAction(semaphore, action, null); } catch (Exception ex) { Assert.NotNull(exceptionType); Assert.IsType(exceptionType, ex); } }
/// <summary> /// Call specific SemaphoreSlim method or property /// </summary> /// <param name="semaphore">The SemaphoreSlim instance</param> /// <param name="action">The action name</param> /// <param name="param">The action parameter, null if it takes no parameters</param> /// <returns>The action return value, null if the action returns void</returns> private static object CallSemaphoreAction (SemaphoreSlim semaphore, SemaphoreSlimActions? action, object param) { if (action == SemaphoreSlimActions.Wait) { if (param is TimeSpan) { return semaphore.Wait((TimeSpan)param); } else if (param is int) { return semaphore.Wait((int)param); } semaphore.Wait(); return null; } else if (action == SemaphoreSlimActions.WaitAsync) { if (param is TimeSpan) { return semaphore.WaitAsync((TimeSpan)param).Result; } else if (param is int) { return semaphore.WaitAsync((int)param).Result; } semaphore.WaitAsync().Wait(); return null; } else if (action == SemaphoreSlimActions.Release) { if (param != null) { return semaphore.Release((int)param); } return semaphore.Release(); } else if (action == SemaphoreSlimActions.Dispose) { semaphore.Dispose(); return null; } else if (action == SemaphoreSlimActions.CurrentCount) { return semaphore.CurrentCount; } else if (action == SemaphoreSlimActions.AvailableWaitHandle) { return semaphore.AvailableWaitHandle; } return null; }
/// <summary> /// Test SemaphoreSlim CurrentCount property /// </summary> /// <param name="initial">The initial semaphore count</param> /// <param name="maximum">The maximum semaphore count</param> /// <param name="action">SemaphoreSlim action to be called before CurentCount</param> /// <returns>True if the test succeeded, false otherwise</returns> private static void RunSemaphoreSlimTest5_CurrentCount(int initial, int maximum, SemaphoreSlimActions? action) { SemaphoreSlim semaphore = new SemaphoreSlim(initial, maximum); try { CallSemaphoreAction(semaphore, action, null); if ((action == SemaphoreSlimActions.Wait && semaphore.CurrentCount != initial - 1) || (action == SemaphoreSlimActions.WaitAsync && semaphore.CurrentCount != initial - 1) || (action == SemaphoreSlimActions.Release && semaphore.CurrentCount != initial + 1)) { string methodFailed = "RunSemaphoreSlimTest5_CurrentCount(" + initial + "," + maximum + "," + action + "): FAILED. "; Assert.True(false, string.Format(methodFailed + "CurrentCount failed")); } } catch (Exception ex) { string methodFailed = "RunSemaphoreSlimTest5_CurrentCount(" + initial + "," + maximum + "," + action + "): FAILED. "; Assert.True(false, string.Format(methodFailed + "CurrentCount failed, the code threw exception " + ex)); } }
/// <summary> /// Test SemaphoreSlim Dispose /// </summary> /// <param name="initial">The initial semaphore count</param> /// <param name="maximum">The maximum semaphore count</param> /// <param name="action">SemaphoreSlim action to be called after Dispose</param> /// <param name="exceptionType">The type of the thrown exception in case of invalid cases, /// null for valid cases</param> /// <returns>True if the test succeeded, false otherwise</returns> private static void RunSemaphoreSlimTest4_Dispose(int initial, int maximum, SemaphoreSlimActions? action, Type exceptionType) { Exception exception = null; SemaphoreSlim semaphore = new SemaphoreSlim(initial, maximum); try { semaphore.Dispose(); CallSemaphoreAction(semaphore, action, null); } catch (Exception ex) { exception = ex; } // The code threw excption and it is not expected because the excyptionType param is null if (exceptionType == null && exception != null) { string methodFailed = "RunSemaphoreSlimTest4_Dispose(" + initial + "," + maximum + "," + action + "): FAILED. "; Assert.True(false, string.Format(methodFailed + "Dispose failed, the code threw an exception, and it is not supposed to.")); } // Compare both exception types in case of the code threw exception if (exception != null && !Type.Equals(exception.GetType(), exceptionType)) { string methodFailed = "RunSemaphoreSlimTest4_Dispose(" + initial + "," + maximum + "," + action + "): FAILED. "; Assert.True(false, string.Format(methodFailed + "Dispose failed, Excption types do not match")); } }
/// <summary> /// Test SemaphoreSlim AvailableWaitHandle property /// </summary> /// <param name="initial">The initial semaphore count</param> /// <param name="maximum">The maximum semaphore count</param> /// <param name="action">SemaphoreSlim action to be called before WaitHandle</param> /// <param name="state">The expected wait handle state</param> /// <returns>True if the test succeeded, false otherwise</returns> private static bool RunSemaphoreSlimTest7_AvailableWaitHandle (int initial, int maximum, SemaphoreSlimActions? action, bool state) { TestHarness.TestLog("AvailableWaitHandle(" + initial + "," + maximum + "," + action + ")"); SemaphoreSlim semaphore = new SemaphoreSlim(initial, maximum); try { CallSemaphoreAction(semaphore, action, null); if (semaphore.AvailableWaitHandle == null) { TestHarness.TestLog("AvailableWaitHandle failed, handle is null."); return false; } if (semaphore.AvailableWaitHandle.WaitOne(0, false) != state) { TestHarness.TestLog("AvailableWaitHandle failed, expected " + state + " actual " + !state); return false; } } catch(Exception ex) { TestHarness.TestLog("AvailableWaitHandle failed, the code threw exception " + ex); return false; } TestHarness.TestLog("AvailableWaitHandle succeeded."); return true; }
/// <summary> /// Test SemaphoreSlim CurrentCount property /// </summary> /// <param name="initial">The initial semaphore count</param> /// <param name="maximum">The maximum semaphore count</param> /// <param name="action">SemaphoreSlim action to be called before CurentCount</param> /// <returns>True if the test succeeded, false otherwise</returns> private static bool RunSemaphoreSlimTest5_CurrentCount (int initial, int maximum, SemaphoreSlimActions? action) { TestHarness.TestLog("CurrentCount(" + initial + "," + maximum + "," + action + ")"); SemaphoreSlim semaphore = new SemaphoreSlim(initial, maximum); try { CallSemaphoreAction(semaphore, action, null); if ((action == SemaphoreSlimActions.Wait && semaphore.CurrentCount != initial - 1) || (action == SemaphoreSlimActions.Release && semaphore.CurrentCount != initial + 1)) { TestHarness.TestLog("CurrentCount failed"); return false; } } catch(Exception ex) { TestHarness.TestLog("CurrentCount failed, the code threw exception " + ex); return false; } TestHarness.TestLog("CurrentCount succeeded"); return true; }
/// <summary> /// Test SemaphoreSlim Dispose /// </summary> /// <param name="initial">The initial semaphore count</param> /// <param name="maximum">The maximum semaphore count</param> /// <param name="action">SemaphoreSlim action to be called after Dispose</param> /// <param name="exceptionType">The type of the thrown exception in case of invalid cases, /// null for valid cases</param> /// <returns>True if the test succeeded, false otherwise</returns> private static bool RunSemaphoreSlimTest4_Dispose (int initial, int maximum, SemaphoreSlimActions? action, Type exceptionType) { TestHarness.TestLog("Dispose(" + initial + "," + maximum + "," + action + ")"); Exception exception = null; SemaphoreSlim semaphore = new SemaphoreSlim(initial, maximum); try { semaphore.Dispose(); CallSemaphoreAction(semaphore,action,null); } catch (Exception ex) { exception = ex; } // The code threw excption and it is not expected because the excyptionType param is null if (exceptionType == null && exception != null) { TestHarness.TestLog("Dispose failed, the code threw an exception, and it is not supposed to."); return false; } // Compare both exception types in case of the code threw exception if (exception != null && !Type.Equals(exception.GetType(), exceptionType)) { TestHarness.TestLog("Dispose failed, Excption types do not match"); return false; } TestHarness.TestLog("Dispose succeeded"); return true; }