public void CallbackTechnique(bool hasReturnType) { _hasReturnType = hasReturnType; LongTask longTask; if (_hasReturnType) { longTask = new LongTask <int>(LongTaskMilliseconds); } else { longTask = new LongTask(LongTaskMilliseconds); } IAsyncResult asyncResult; if (_hasReturnType) { asyncResult = ((LongTask <int>)longTask).BeginDoTask(IntInput, TaskCompleted, longTask); } else { asyncResult = longTask.BeginDoTask(TaskCompleted, longTask); } _mre.WaitOne(); //Block the main thread until async thread finishes executing the call back AssertTaskCompleted(asyncResult); Assert.False(asyncResult.CompletedSynchronously, "Should not have completed synchronously."); }
public void PollUntilCompleteTechnique(bool hasReturnType) { _hasReturnType = hasReturnType; LongTask longTask; if (_hasReturnType) { longTask = new LongTask <int>(LongTaskMilliseconds); } else { longTask = new LongTask(LongTaskMilliseconds); } IAsyncResult asyncResult = longTask.BeginDoTask(null, null); var mres = new ManualResetEventSlim(); while (!asyncResult.IsCompleted) { mres.Wait(1); } AssertTaskCompleted(asyncResult); Assert.False(asyncResult.CompletedSynchronously, "Should not have completed synchronously."); }
public void WaitUntilCompleteTechnique(bool hasReturnType) { _hasReturnType = hasReturnType; LongTask longTask; if (_hasReturnType) longTask = new LongTask<int>(LongTaskMilliseconds); else longTask = new LongTask(LongTaskMilliseconds); // Prove that the Wait-until-done technique works IAsyncResult asyncResult = longTask.BeginDoTask(null, null); longTask.EndDoTask(asyncResult); AssertTaskCompleted(asyncResult); Assert.False(asyncResult.CompletedSynchronously, "Should not have completed synchronously."); }
/// <summary> /// Method used by the callback implementation by the APM /// </summary> /// <param name="ar"></param> private void TaskCompleted(IAsyncResult ar) { if (_hasReturnType) { LongTask <int> lt = (LongTask <int>)ar.AsyncState; int retValue = lt.EndDoTask(ar); if (retValue != IntInput) { Assert.True(false, string.Format("Mismatch: Return = {0} vs Expect = {1}", retValue, IntInput)); } } else { LongTask lt = (LongTask)ar.AsyncState; lt.EndDoTask(ar); } _mre.Set(); }
public void PollUntilCompleteTechnique(bool hasReturnType) { _hasReturnType = hasReturnType; LongTask longTask; if (_hasReturnType) longTask = new LongTask<int>(LongTaskMilliseconds); else longTask = new LongTask(LongTaskMilliseconds); IAsyncResult asyncResult = longTask.BeginDoTask(null, null); while (!asyncResult.IsCompleted) { Task.Delay(300).Wait(); } AssertTaskCompleted(asyncResult); Assert.False(asyncResult.CompletedSynchronously, "Should not have completed synchronously."); }
public void WaitOnAsyncWaitHandleTechnique(bool hasReturnType) { _hasReturnType = hasReturnType; LongTask longTask; if (_hasReturnType) { longTask = new LongTask <int>(LongTaskMilliseconds); } else { longTask = new LongTask(LongTaskMilliseconds); } IAsyncResult asyncResult = longTask.BeginDoTask(null, null); asyncResult.AsyncWaitHandle.WaitOne(); AssertTaskCompleted(asyncResult); Assert.False(asyncResult.CompletedSynchronously, "Should not have completed synchronously."); }
public void WaitUntilCompleteTechnique(bool hasReturnType) { _hasReturnType = hasReturnType; LongTask longTask; if (_hasReturnType) { longTask = new LongTask <int>(LongTaskMilliseconds); } else { longTask = new LongTask(LongTaskMilliseconds); } // Prove that the Wait-until-done technique works IAsyncResult asyncResult = longTask.BeginDoTask(null, null); longTask.EndDoTask(asyncResult); AssertTaskCompleted(asyncResult); Assert.False(asyncResult.CompletedSynchronously, "Should not have completed synchronously."); }
/// <summary> /// Method that tests that the four APM patterns works /// </summary> /// <returns></returns> internal void RealRun() { IAsyncResult ar; LongTask lt = null; if (_hasReturnType) lt = new LongTask<int>(LongTaskSeconds); else lt = new LongTask(LongTaskSeconds); //1. Prove that the Wait-until-done technique works ar = lt.BeginDoTask(null, null); lt.EndDoTask(ar); // verify task completed if (!VerifyTaskCompleted(ar)) Assert.True(false, string.Format("Wait-until-done: Task is not completed")); Debug.WriteLine("Wait-until-Done -- Task completed"); //2. Prove that the Polling technique works ar = lt.BeginDoTask(null, null); while (!ar.IsCompleted) { Task delay = Task.Delay(1000); delay.Wait(); //Thread.Sleep(1000); } // verify task completed if (!VerifyTaskCompleted(ar)) Assert.True(false, string.Format("Polling: Task is not completed")); Debug.WriteLine("Polling -- Task completed"); //3. Prove the AsyncWaitHandle works ar = lt.BeginDoTask(null, null); ar.AsyncWaitHandle.WaitOne(); // verify task completed if (!VerifyTaskCompleted(ar)) Assert.True(false, string.Format("wait via AsyncWaitHandle: Task is not completed")); Debug.WriteLine("Wait on AsyncWaitHandle -- Task completed"); //4. Prove that the Callback technique works if (_hasReturnType) ar = ((LongTask<int>)lt).BeginDoTask(INTINPUT, TaskCompleted, lt); else ar = lt.BeginDoTask(TaskCompleted, lt); _mre.WaitOne(); //Block the main thread until async thread finishes executing the call back // verify task completed if (!VerifyTaskCompleted(ar)) Assert.True(false, string.Format("Callback: Task is not completed")); Debug.WriteLine("Callback -- Task completed"); //reaching this point means that the test didnt encounter any crashes or hangs. //So set the test as passed by returning true Assert.False(ar.CompletedSynchronously, "Should not have completed synchronously."); // Cleanup _mre.Dispose(); }
public void WaitOnAsyncWaitHandleTechnique(bool hasReturnType) { _hasReturnType = hasReturnType; LongTask longTask; if (_hasReturnType) longTask = new LongTask<int>(LongTaskMilliseconds); else longTask = new LongTask(LongTaskMilliseconds); IAsyncResult asyncResult = longTask.BeginDoTask(null, null); asyncResult.AsyncWaitHandle.WaitOne(); AssertTaskCompleted(asyncResult); Assert.False(asyncResult.CompletedSynchronously, "Should not have completed synchronously."); }
public void CallbackTechnique(bool hasReturnType) { _hasReturnType = hasReturnType; LongTask longTask; if (_hasReturnType) longTask = new LongTask<int>(LongTaskMilliseconds); else longTask = new LongTask(LongTaskMilliseconds); IAsyncResult asyncResult; if (_hasReturnType) asyncResult = ((LongTask<int>)longTask).BeginDoTask(IntInput, TaskCompleted, longTask); else asyncResult = longTask.BeginDoTask(TaskCompleted, longTask); _mre.WaitOne(); //Block the main thread until async thread finishes executing the call back AssertTaskCompleted(asyncResult); Assert.False(asyncResult.CompletedSynchronously, "Should not have completed synchronously."); }
/// <summary> /// Method that tests that the four APM patterns works /// </summary> /// <returns></returns> internal void RealRun() { IAsyncResult ar; LongTask lt = null; if (_hasReturnType) { lt = new LongTask <int>(LongTaskSeconds); } else { lt = new LongTask(LongTaskSeconds); } //1. Prove that the Wait-until-done technique works ar = lt.BeginDoTask(null, null); lt.EndDoTask(ar); // verify task completed if (!VerifyTaskCompleted(ar)) { Assert.True(false, string.Format("Wait-until-done: Task is not completed")); } Debug.WriteLine("Wait-until-Done -- Task completed"); //2. Prove that the Polling technique works ar = lt.BeginDoTask(null, null); while (!ar.IsCompleted) { Task delay = Task.Delay(1000); delay.Wait(); //Thread.Sleep(1000); } // verify task completed if (!VerifyTaskCompleted(ar)) { Assert.True(false, string.Format("Polling: Task is not completed")); } Debug.WriteLine("Polling -- Task completed"); //3. Prove the AsyncWaitHandle works ar = lt.BeginDoTask(null, null); ar.AsyncWaitHandle.WaitOne(); // verify task completed if (!VerifyTaskCompleted(ar)) { Assert.True(false, string.Format("wait via AsyncWaitHandle: Task is not completed")); } Debug.WriteLine("Wait on AsyncWaitHandle -- Task completed"); //4. Prove that the Callback technique works if (_hasReturnType) { ar = ((LongTask <int>)lt).BeginDoTask(INTINPUT, TaskCompleted, lt); } else { ar = lt.BeginDoTask(TaskCompleted, lt); } _mre.WaitOne(); //Block the main thread until async thread finishes executing the call back // verify task completed if (!VerifyTaskCompleted(ar)) { Assert.True(false, string.Format("Callback: Task is not completed")); } Debug.WriteLine("Callback -- Task completed"); //reaching this point means that the test didnt encounter any crashes or hangs. //So set the test as passed by returning true Assert.False(ar.CompletedSynchronously, "Should not have completed synchronously."); // Cleanup _mre.Dispose(); }