public static bool PreCanceledToken_SimpleEnumerator() { bool passed = true; TestHarness.TestLog("* PlinqCancellationTests.PreCanceledToken_SimpleEnumerator()"); OperationCanceledException caughtException = null; var cs = new CancellationTokenSource(); cs.Cancel(); int[] srcEnumerable = Enumerable.Range(0, 1000).ToArray(); ThrowOnFirstEnumerable <int> throwOnFirstEnumerable = new ThrowOnFirstEnumerable <int>(srcEnumerable); try { var query = throwOnFirstEnumerable .AsParallel() .WithCancellation(cs.Token); foreach (var item in query) { } } catch (OperationCanceledException ex) { caughtException = ex; } passed &= TestHarnessAssert.IsNotNull(caughtException, "an OCE should be throw during query opening"); passed &= TestHarnessAssert.AreEqual(cs.Token, OCEHelper.ExtractCT(caughtException), "The OCE should reference the cancellation token."); return(passed); }
// Tests that the shared state variable seems to be working correctly. private static bool RunManualResetEventSlimTest4_CombinedStateTests() { bool passed = true; TestHarness.TestLog("* RunManualResetEventSlimTest4_CombinedStateTests()"); ManualResetEventSlim mres = new ManualResetEventSlim(false, 100); int expectedCount = Environment.ProcessorCount == 1 ? 1 : 100; passed &= TestHarnessAssert.AreEqual(expectedCount, mres.SpinCount, "Spin count did not write/read correctly, expected " + expectedCount + ", actual " + mres.SpinCount); passed &= TestHarnessAssert.IsFalse(mres.IsSet, "Set did not read correctly."); mres.Set(); passed &= TestHarnessAssert.IsTrue(mres.IsSet, "Set did not write/read correctly."); return(passed); }
private static bool CancellationTokenTest_Sorting_ToArray() { bool passed = true; TestHarness.TestLog("* PlinqCancellationTests.CancellationTokenTest_Sorting_ToArray()"); int size = 10000; CancellationTokenSource tokenSource = new CancellationTokenSource(); ThreadPool.QueueUserWorkItem( (arg) => { Thread.Sleep(500); tokenSource.Cancel(); }); OperationCanceledException caughtException = null; try { // This query should run for at least a few seconds due to the sleeps in the select-delegate var query = Enumerable.Range(1, size).AsParallel() .WithCancellation(tokenSource.Token) .Select( i => { Thread.Sleep(1); return(i); }); query.ToArray(); } catch (OperationCanceledException ex) { caughtException = ex; } passed &= TestHarnessAssert.IsNotNull(caughtException, "An OCE should be thrown"); passed &= TestHarnessAssert.AreEqual(tokenSource.Token, OCEHelper.ExtractCT(caughtException), "The OCE should reference the external token."); return(passed); }
private static bool CancellationTokenTest_NonSorting_ToArray_ExternalCancel() { bool passed = true; TestHarness.TestLog("* PlinqCancellationTests.CancellationTokenTest_NonSorting_ToArray_ExternalCancel()"); int size = 10000; CancellationTokenSource tokenSource = new CancellationTokenSource(); OperationCanceledException caughtException = null; ThreadPool.QueueUserWorkItem( (arg) => { Thread.Sleep(1000); tokenSource.Cancel(); }); try { int[] output = Enumerable.Range(1, size).AsParallel() .WithCancellation(tokenSource.Token) .Select( i => { Thread.Sleep(100); return(i); }).ToArray(); } catch (OperationCanceledException ex) { caughtException = ex; } passed &= TestHarnessAssert.IsNotNull(caughtException, "An ObjectDisposedException should be thrown"); passed &= TestHarnessAssert.AreEqual(tokenSource.Token, OCEHelper.ExtractCT(caughtException), "The OCE should reference the external cancellation token."); return(passed); }
/// <summary>Tests for the Value factory throws an exception.</summary> /// <returns>True if the tests succeeds, false otherwise.</returns> private static bool RunLazyTest9_Exceptions() { TestHarness.TestLog("* RunLazyTest9_Exceptions()"); int counter = m_exceptionCounter; Lazy <string> l = new Lazy <string>(() => { m_exceptionCounter++; int zero = 0; int x = 1 / zero; return(""); }, true); string s; if (!TestHarnessAssert.EnsureExceptionThrown(() => s = l.Value, typeof(DivideByZeroException), "Expected DivideByZeroException")) { TestHarness.TestLog("failed"); return(false); } if (!TestHarnessAssert.EnsureExceptionThrown(() => s = l.Value, typeof(DivideByZeroException), "Expected DivideByZeroException")) { TestHarness.TestLog("failed"); return(false); } if (!TestHarnessAssert.AreEqual <int>(counter + 1, m_exceptionCounter, "value factory has been called twise and it should be called only once.")) { TestHarness.TestLog("failed"); return(false); } if (l.IsValueCreated) { TestHarness.TestLog("failed* IsValueCreated should return false."); return(false); } return(true); }