public static void RunRefactoringTests_NegativeTests() { TaskCompletionSource<int> tr = new TaskCompletionSource<int>(); int temp = 0; Task<int> f = Task.Factory.StartNew<int>((object i) => { return (int)i; }, 1, CancellationToken.None, TaskCreationOptions.None, TaskScheduler.Current); Task t; temp = f.Result; if (temp != 1) { Assert.True(false, string.Format("RunRefactoringTests - Task.Factory.StartNew<int>(Func<object, int>, object, CT, options, TaskScheduler). > FAILED. Delegate failed to execute.")); } f = new TaskCompletionSource<int>().Task; try { f.Start(); Assert.True(false, string.Format("RunRefactoringTests - TaskCompletionSource<int>.Task (should throw exception): > FAILED. No exception thrown.")); } catch (Exception) { //Assert.True(false, string.Format(" > caught exception: {0}", e.Message)); } t = new Task(delegate { temp = 100; }); t.Start(); try { t.Start(); Assert.True(false, string.Format("RunRefactoringTests - Restarting Task: > FAILED. No exception thrown, when there should be.")); } catch (Exception) { //Assert.True(false, string.Format(" > caught exception: {0}", e.Message)); } // If we don't do this, the asynchronous setting of temp=100 in the delegate could // screw up some tests below. t.Wait(); try { t = new Task(delegate { temp = 100; }, (TaskCreationOptions)10000); Assert.True(false, string.Format("RunRefactoringTests - Illegal Options CTor Task: > FAILED. No exception thrown, when there should be.")); } catch (Exception) { } try { t = new Task(null); Assert.True(false, string.Format("RunRefactoringTests - Task ctor w/ null action: > FAILED. No exception thrown.")); } catch (Exception) { } try { t = Task.Factory.StartNew(null); Assert.True(false, string.Format("RunRefactoringTests - Task.Factory.StartNew() w/ Null Action: > FAILED. No exception thrown.")); } catch (Exception) { } t = new Task(delegate { }); Task t2 = t.ContinueWith(delegate { }); try { t2.Start(); Assert.True(false, string.Format("RunRefactoringTests - Task.Start() on Continuation Task: > FAILED. No exception thrown.")); } catch (Exception) { } t = new Task(delegate { }); try { t.Start(null); Assert.True(false, string.Format("RunRefactoringTests - Task.Start() with null taskScheduler: > FAILED. No exception thrown.")); } catch (Exception) { } t = Task.Factory.StartNew(delegate { }); try { t = Task.Factory.StartNew(delegate { }, CancellationToken.None, TaskCreationOptions.None, (TaskScheduler)null); Assert.True(false, string.Format("RunRefactoringTests - Task.Factory.StartNew() with null taskScheduler: > FAILED. No exception thrown.")); } catch (Exception) { } tr = new TaskCompletionSource<int>(); tr.SetException(new Exception("some exception")); try { tr.SetResult(5); Assert.True(false, string.Format("RunRefactoringTests - TaskCompletionSource set Result after setting Exception: > FAILED. No exception thrown.")); } catch (Exception) { } finally { //prevent finalize from crashing on exception Exception e2 = tr.Task.Exception; } tr = new TaskCompletionSource<int>(); tr.SetResult(5); try { tr.SetException(new Exception("some exception")); Assert.True(false, string.Format("RunRefactoringTests - TaskCompletionSource set Exception after setting Result > FAILED. No exception thrown.")); } catch (Exception) { } finally { // clean up. temp = tr.Task.Result; } }