Пример #1
0
 public void BeginMethodTest()
 {
     bool methodCalled = false;
     // setup the method
     Func<int, int> method = n =>
                                 {
                                     methodCalled = true;
                                     return -1;
                                 };
     // setup the test instance
     var asyncSample = new AsyncBench(
         20, method);
     IAsyncResult ar = asyncSample.BeginMethod();
     // wait until method call completes
     ar.AsyncWaitHandle.WaitOne();
     // verify it is completed
     Assert.IsTrue(ar.IsCompleted, "Operation should have completed before reaching this point!");
     // verify our delegate was called
     Assert.IsTrue(methodCalled, "Test method should have been called, but it was not!");
     // The bellow assert would require more synchronization and exceeds the testing contract
     //Assert.AreEqual(-1, asyncSample.Param);
 }
Пример #2
0
 public void AsyncMethodCallbackTest()
 {
     bool methodCalled = false;
     // setup method
     Func<int, int> method = n =>
     {
         methodCalled = true;
         return -1;
     };
     // setup test instance
     var asyncSample = new AsyncBench(
         20, method);
     IAsyncResult ar = method.BeginInvoke(20, null, new State {Field = 10});
     // wait for the method to complete
     ar.AsyncWaitHandle.WaitOne();
     // verify it has completed
     Assert.IsTrue(ar.IsCompleted, "Operation should have completed before reaching this point!");
     // and was really called
     Assert.IsTrue(methodCalled, "Test method should have been called, but it was not!");
     // use IAsyncResult from our own BeginInvoke for the callback on the test instance
     asyncSample.AsyncMethodCallback(ar);
     // check that EndInvoke worked as expected
     Assert.AreEqual(-1, asyncSample.Param);
 }