public void WrapNativeAsyncInvoke_ASynchronousCompletionCallsEndFromCallback() { StubSupportingCompletedSynchronously myNativeStub = null; int endFunctionCallCount = 0; Task t = Utility.WrapNativeAsyncInvoke( (callback) => { myNativeStub = new StubSupportingCompletedSynchronously(callback) { IsCompletedSynchronously_Internal = false }; return(myNativeStub); }, (adapter) => Interlocked.Increment(ref endFunctionCallCount), CancellationToken.None, "test"); Assert.AreEqual <int>(0, endFunctionCallCount); // invoke the callback myNativeStub.get_Callback().Invoke(myNativeStub); Assert.IsTrue(t.Wait(taskWaitTimeout)); Assert.AreEqual <int>(1, endFunctionCallCount); }
public void WrapNativeAsyncInvoke_SynchronousCompletionCallsEndAfterBegin() { StubSupportingCompletedSynchronously myNativeStub = null; int endFunctionCallCount = 0; Task t = Utility.WrapNativeAsyncInvoke( (callback) => { myNativeStub = new StubSupportingCompletedSynchronously(callback) { IsCompletedSynchronously_Internal = true }; return(myNativeStub); }, (adapter) => Interlocked.Increment(ref endFunctionCallCount), CancellationToken.None, "test"); Assert.IsTrue(t.Wait(taskWaitTimeout)); // since the call completed synchronously the end function should be called without invoking the callback Assert.AreEqual <int>(1, endFunctionCallCount); // invoke the callback myNativeStub.get_Callback().Invoke(myNativeStub); // the end function should not have been called again Assert.AreEqual <int>(1, endFunctionCallCount); }