public void MergePromiseIsCanceledWhenAnyPromiseIsAlreadyCanceled() { bool canceled = false; string cancelation = "Cancel!"; var deferred = Promise.NewDeferred <int>(); Promise.Merge(deferred.Promise, Promise.Canceled <int, string>(cancelation)) .Then(v => Assert.Fail("Promise was resolved when it should have been rejected.")) .CatchCancelation(reason => { Assert.AreEqual(cancelation, reason.Value); canceled = true; }); deferred.Resolve(0); Promise.Manager.HandleCompletes(); Assert.AreEqual(true, canceled); // Clean up. GC.Collect(); Promise.Manager.HandleCompletesAndProgress(); LogAssert.NoUnexpectedReceived(); }
public void SetException(Exception exception) { #if PROMISE_CANCEL if (exception is OperationCanceledException ex) { if (_deferred == null) { Task = Promise.Canceled(ex); } else { _deferred.Cancel(ex); _deferred = null; } } else #endif { if (_deferred == null) { Task = Promise.Rejected(exception); } else { _deferred.Reject(exception); _deferred = null; } } }
public void AwaitAlreadyCanceledPromiseThrowsOperationCanceled2() { string cancelValue = "Cancel"; bool continued = false; async void Func() { try { int value = await Promise.Canceled <int, string>(cancelValue); } catch (CanceledException e) { Assert.AreEqual(cancelValue, e.Value); continued = true; } } Assert.AreEqual(false, continued); Func(); Assert.AreEqual(true, continued); Promise.Manager.HandleCompletes(); Assert.AreEqual(true, continued); TestHelper.Cleanup(); }
public void SequencePromiseIsCanceledWhenAnyPromiseIsAlreadyCanceled() { int canceled = 0; string cancelation = "Cancel!"; var deferred = Promise.NewDeferred <int>(); Promise.Sequence(() => deferred.Promise, () => Promise.Canceled <int, string>(cancelation)) .Then(() => Assert.Fail("Promise was resolved when it should have been canceled.")) .CatchCancelation(reason => { Assert.AreEqual(cancelation, reason.Value); ++canceled; }); Promise.Manager.HandleCompletes(); Assert.AreEqual(0, canceled); deferred.Resolve(0); Promise.Manager.HandleCompletes(); Assert.AreEqual(1, canceled); // Clean up. GC.Collect(); Promise.Manager.HandleCompletesAndProgress(); LogAssert.NoUnexpectedReceived(); }
public void AwaitAlreadyCanceledPromiseThrowsOperationCanceled2() { string cancelValue = "Cancel"; bool continued = false; async void Func() { try { int value = await Promise.Canceled <int, string>(cancelValue); } catch (CanceledException e) { Assert.AreEqual(cancelValue, e.Value); continued = true; } } Assert.AreEqual(false, continued); Func(); Assert.AreEqual(true, continued); Promise.Manager.HandleCompletes(); Assert.AreEqual(true, continued); // Clean up. GC.Collect(); Promise.Manager.HandleCompletesAndProgress(); LogAssert.NoUnexpectedReceived(); }
public void OnCanceledIsNotInvokedIfTokenIsCanceled() { CancelationSource cancelationSource = CancelationSource.New(); Promise.Canceled() .CatchCancelation(_ => Assert.Fail("OnCanceled was invoked."), cancelationSource.Token) .CatchCancelation(1, (cv, _) => Assert.Fail("OnCanceled was invoked."), cancelationSource.Token); Promise.Canceled <int>() .CatchCancelation(_ => Assert.Fail("OnCanceled was invoked."), cancelationSource.Token) .CatchCancelation(1, (cv, _) => Assert.Fail("OnCanceled was invoked."), cancelationSource.Token); cancelationSource.Cancel(); Promise.Manager.HandleCompletes(); cancelationSource.Dispose(); TestHelper.Cleanup(); }
public void SetException(Exception exception) { if (Task is null) { if (exception is OperationCanceledException e) { Task = Promise.Canceled(e); } else { Task = Promise.Rejected(exception); } } else { ((AsyncPromise)Task).SetException(exception); } }
public void AllPromiseIsCancelededWhenAnyPromiseIsAlreadyCanceled() { int cancelations = 0; string cancelation = "Cancel!"; var deferred = Promise.NewDeferred <int>(); var promise = Promise.Canceled <int, string>(cancelation); promise.Retain(); Promise.Manager.HandleCompletes(); Promise.All(deferred.Promise, promise) .Then(v => Assert.Fail("Promise was resolved when it should have been canceled.")) .CatchCancelation(ex => { Assert.AreEqual(cancelation, ex.Value); ++cancelations; }); Promise.All((Promise)deferred.Promise, promise) .Then(() => Assert.Fail("Promise was resolved when it should have been canceled.")) .CatchCancelation(ex => { Assert.AreEqual(cancelation, ex.Value); ++cancelations; }); deferred.Resolve(0); Promise.Manager.HandleCompletes(); Assert.AreEqual(2, cancelations); promise.Release(); Promise.Manager.HandleCompletes(); Assert.AreEqual(2, cancelations); // Clean up. GC.Collect(); Promise.Manager.HandleCompletesAndProgress(); LogAssert.NoUnexpectedReceived(); }
public void PromiseCanceledIsCanceledWithTheGivenReason1() { string expected = "Cancel"; var promise = Promise.Canceled <int, string>(expected); bool canceled = false; promise .Then(() => Assert.Fail("Promise was resolved when it should have been canceled")) .Catch(() => Assert.Fail("Promise was rejected when it should have been canceled")) .CatchCancelation(reason => { Assert.AreEqual(expected, reason.Value); canceled = true; }); Promise.Manager.HandleCompletes(); Assert.AreEqual(true, canceled); TestHelper.Cleanup(); }
public void PromiseCanceledIsCanceledWithTheGivenReason0() { string expected = "Cancel"; var promise = Promise.Canceled(expected); bool canceled = false; promise .Then(() => Assert.Fail("Promise was resolved when it should have been canceled")) .Catch(() => Assert.Fail("Promise was rejected when it should have been canceled")) .CatchCancelation(reason => { Assert.AreEqual(expected, reason.Value); canceled = true; }); Promise.Manager.HandleCompletes(); Assert.AreEqual(true, canceled); // Clean up. GC.Collect(); Promise.Manager.HandleCompletesAndProgress(); LogAssert.NoUnexpectedReceived(); }