public void ItMustNotBeCalledMoreThanOnce() { CancelationSource cancelationSource = CancelationSource.New(); var deferred = Promise.NewDeferred(cancelationSource.Token); Assert.AreEqual(Promise.State.Pending, deferred.State); deferred.Retain(); var cancelCount = 0; deferred.Promise.CatchCancelation(r => ++ cancelCount); cancelationSource.Cancel("Cancel value"); Promise.Manager.HandleCompletes(); Assert.Throws <InvalidOperationException>(() => cancelationSource.Cancel("Cancel value") ); deferred.Release(); Promise.Manager.HandleCompletes(); Assert.AreEqual(1, cancelCount); cancelationSource.Dispose(); TestHelper.Cleanup(); }
public void CancelationSourceInvalidOperations() { CancelationSource cancelationSource = new CancelationSource(); Assert.Throws <InvalidOperationException>(() => { var _ = cancelationSource.Token; }); Assert.Throws <InvalidOperationException>(() => { cancelationSource.Cancel(); }); Assert.Throws <InvalidOperationException>(() => { cancelationSource.Cancel("Cancel"); }); Assert.Throws <InvalidOperationException>(() => { cancelationSource.Dispose(); }); TestHelper.Cleanup(); }
public void FirstIsCanceledWhenAllPromisesAreCanceled() { CancelationSource cancelationSource1 = CancelationSource.New(); var deferred1 = Promise.NewDeferred <int>(cancelationSource1.Token); CancelationSource cancelationSource2 = CancelationSource.New(); var deferred2 = Promise.NewDeferred <int>(cancelationSource2.Token); var canceled = 0; string cancelation = "Cancel!"; Promise.First(deferred1.Promise, deferred2.Promise) .Then(i => Assert.Fail("Promise was resolved when it should have been rejected.")) .CatchCancelation(reason => { Assert.AreEqual(cancelation, reason.Value); ++canceled; }); Promise.First((Promise)deferred1.Promise, deferred2.Promise) .Then(() => Assert.Fail("Promise was resolved when it should have been rejected.")) .CatchCancelation(reason => { Assert.AreEqual(cancelation, reason.Value); ++canceled; }); cancelationSource1.Cancel("Different Cancel!"); Promise.Manager.HandleCompletes(); Assert.AreEqual(0, canceled); cancelationSource2.Cancel(cancelation); Promise.Manager.HandleCompletes(); Assert.AreEqual(2, canceled); cancelationSource1.Dispose(); cancelationSource2.Dispose(); TestHelper.Cleanup(); }
public void FirstIsResolvedWhenSecondPromiseIsCanceledThenFirstPromiseIsResolved() { var deferred1 = Promise.NewDeferred <int>(); CancelationSource cancelationSource = CancelationSource.New(); var deferred2 = Promise.NewDeferred <int>(cancelationSource.Token); var resolved = 0; Promise.First(deferred1.Promise, deferred2.Promise) .Then(i => { Assert.AreEqual(5, i); ++resolved; }); Promise.First((Promise)deferred1.Promise, deferred2.Promise) .Then(() => ++ resolved); cancelationSource.Cancel("Cancel!"); Promise.Manager.HandleCompletes(); Assert.AreEqual(0, resolved); deferred1.Resolve(5); Promise.Manager.HandleCompletes(); Assert.AreEqual(2, resolved); cancelationSource.Dispose(); TestHelper.Cleanup(); }
public void SequencePromiseIsCanceledWhenTokenIsCanceled2() { CancelationSource cancelationSource = CancelationSource.New(); int invokedIndex = -1; Promise.Sequence( cancelationSource.Token, () => { invokedIndex = 0; cancelationSource.Cancel(); return(Promise.Resolved()); }, () => { invokedIndex = 1; return(Promise.Resolved()); } ); Promise.Manager.HandleCompletes(); Assert.AreEqual(0, invokedIndex); cancelationSource.Dispose(); TestHelper.Cleanup(); }
public void SequencePromiseIsCanceledWhenTokenIsCanceled0() { CancelationSource cancelationSource = CancelationSource.New(); bool canceled = false; int invokedIndex = -1; Promise.Sequence( cancelationSource.Token, () => { invokedIndex = 0; return(Promise.Resolved()); }, () => { invokedIndex = 1; return(Promise.Resolved()); } ) .CatchCancelation(reason => { canceled = true; }); cancelationSource.Cancel(); Promise.Manager.HandleCompletes(); Assert.AreEqual(-1, invokedIndex); Assert.IsTrue(canceled); cancelationSource.Dispose(); TestHelper.Cleanup(); }
public void SequenceDelegatesStopGettingInvokedWhenAPromiseIsCanceled() { CancelationSource cancelationSource = CancelationSource.New(); var deferred1 = Promise.NewDeferred(cancelationSource.Token); var deferred2 = Promise.NewDeferred(); int invokes = 0; Promise.Sequence(() => { ++invokes; return(deferred1.Promise); }, () => { ++invokes; return(deferred2.Promise); }) .Then(() => Assert.Fail("Promise was resolved when it should have been canceled.")); Assert.AreEqual(0, invokes); Promise.Manager.HandleCompletes(); Assert.AreEqual(1, invokes); cancelationSource.Cancel("Cancel!"); Promise.Manager.HandleCompletes(); Assert.AreEqual(1, invokes); deferred2.Resolve(); Promise.Manager.HandleCompletes(); Assert.AreEqual(1, invokes); cancelationSource.Dispose(); TestHelper.Cleanup(); }
public void SequencePromiseIsCanceledWhenSecondPromiseIsCanceled() { var deferred1 = Promise.NewDeferred(); CancelationSource cancelationSource = CancelationSource.New(); var deferred2 = Promise.NewDeferred(cancelationSource.Token); var cancelations = 0; Promise.Sequence(() => deferred1.Promise, () => deferred2.Promise) .Then(() => Assert.Fail("Promise was resolved when it should have been canceled.")) .CatchCancelation(e => ++ cancelations); Promise.Manager.HandleCompletes(); Assert.AreEqual(0, cancelations); deferred1.Resolve(); Promise.Manager.HandleCompletes(); Assert.AreEqual(0, cancelations); cancelationSource.Cancel("Cancel!"); Promise.Manager.HandleCompletes(); Assert.AreEqual(1, cancelations); cancelationSource.Dispose(); TestHelper.Cleanup(); }
public void OnProgressWillNoLongerBeInvokedWhenTokenIsCanceled1() { var deferred = Promise.NewDeferred(); CancelationSource cancelationSource = CancelationSource.New(); float progress = float.NaN; deferred.Promise .Progress(1, (cv, p) => progress = p, cancelationSource.Token); deferred.ReportProgress(0.25f); Promise.Manager.HandleCompletesAndProgress(); Assert.AreEqual(0.25f, progress, TestHelper.progressEpsilon); cancelationSource.Cancel(); deferred.ReportProgress(0.5f); Promise.Manager.HandleCompletesAndProgress(); Assert.AreEqual(0.25f, progress, TestHelper.progressEpsilon); deferred.Resolve(); Promise.Manager.HandleCompletesAndProgress(); Assert.AreEqual(0.25f, progress, TestHelper.progressEpsilon); cancelationSource.Dispose(); TestHelper.Cleanup(); }
public void OnProgressWillNoLongerBeInvokedWhenPromiseIsCanceled9() { var deferred = Promise.NewDeferred(); Assert.AreEqual(Promise.State.Pending, deferred.State); CancelationSource cancelationSource = CancelationSource.New(); var deferred2 = Promise.NewDeferred(cancelationSource.Token); Assert.AreEqual(Promise.State.Pending, deferred2.State); float progress = float.NaN; deferred.Promise .Then(() => deferred2.Promise) .ThenDuplicate() .Progress(p => progress = p); deferred.ReportProgress(0.5f); Promise.Manager.HandleCompletesAndProgress(); Assert.AreEqual(0.25f, progress, TestHelper.progressEpsilon); deferred.Resolve(); Promise.Manager.HandleCompletesAndProgress(); Assert.AreEqual(0.5f, progress, TestHelper.progressEpsilon); deferred2.ReportProgress(0.5f); cancelationSource.Cancel(); Promise.Manager.HandleCompletesAndProgress(); Assert.AreEqual(0.5f, progress, TestHelper.progressEpsilon); cancelationSource.Dispose(); TestHelper.Cleanup(); }
public void FirstIsCancelededWhenSecondPromiseIsRejectedThenFirstPromiseIsCanceled() { CancelationSource cancelationSource = CancelationSource.New(); var deferred1 = Promise.NewDeferred <int>(cancelationSource.Token); var deferred2 = Promise.NewDeferred <int>(); var canceled = 0; string cancelation = "Cancel!"; Promise.First(deferred1.Promise, deferred2.Promise) .Then(i => Assert.Fail("Promise was resolved when it should have been rejected.")) .CatchCancelation(reason => { Assert.AreEqual(cancelation, reason.Value); ++canceled; }); Promise.First((Promise)deferred1.Promise, deferred2.Promise) .Then(() => Assert.Fail("Promise was resolved when it should have been rejected.")) .CatchCancelation(reason => { Assert.AreEqual(cancelation, reason.Value); ++canceled; }); deferred2.Reject("Error!"); deferred2.Promise.Catch((string _) => { }); Promise.Manager.HandleCompletes(); Assert.AreEqual(0, canceled); cancelationSource.Cancel(cancelation); Promise.Manager.HandleCompletes(); Assert.AreEqual(2, canceled); cancelationSource.Dispose(); TestHelper.Cleanup(); }
public void OnContinueCancelReasonWhenPromiseIsCanceled1() { CancelationSource cancelationSource1 = CancelationSource.New(); var deferred = Promise.NewDeferred(cancelationSource1.Token); CancelationSource cancelationSource2 = CancelationSource.New(); var deferredInt = Promise.NewDeferred <int>(cancelationSource2.Token); TestHelper.AddContinueCallbacks <int, string>(deferred.Promise, onContinue: r => { Assert.AreEqual(r.State, Promise.State.Canceled); Assert.AreEqual(null, r.CancelContainer.ValueType); } ); TestHelper.AddContinueCallbacks <int, int, string>(deferredInt.Promise, onContinue: r => { Assert.AreEqual(r.State, Promise.State.Canceled); Assert.AreEqual(null, r.CancelContainer.ValueType); } ); cancelationSource1.Cancel(); cancelationSource2.Cancel(); Promise.Manager.HandleCompletes(); cancelationSource1.Dispose(); cancelationSource2.Dispose(); TestHelper.Cleanup(); }
public void RaceIsCanceledWhenSecondPromiseIsCanceledFirst() { var deferred1 = Promise.NewDeferred <int>(); CancelationSource cancelationSource = CancelationSource.New(); var deferred2 = Promise.NewDeferred <int>(cancelationSource.Token); var invoked = 0; string expected = "Error!"; Promise.Race(deferred1.Promise, deferred2.Promise) .Then(i => Assert.Fail("Promise was resolved when it should have been canceled.")) .CatchCancelation(reason => { Assert.AreEqual(expected, reason.Value); ++invoked; }); Promise.Race((Promise)deferred1.Promise, deferred2.Promise) .Then(() => Assert.Fail("Promise was resolved when it should have been canceled.")) .CatchCancelation(reason => { Assert.AreEqual(expected, reason.Value); ++invoked; }); cancelationSource.Cancel(expected); Promise.Manager.HandleCompletes(); Assert.AreEqual(2, invoked); deferred1.Resolve(5); cancelationSource.Dispose(); TestHelper.Cleanup(); }
public void IfOnCanceledIsNullThrow() { CancelationSource cancelationSource1 = CancelationSource.New(); var deferred = Promise.NewDeferred(cancelationSource1.Token); Assert.AreEqual(Promise.State.Pending, deferred.State); Assert.Throws <ArgumentNullException>(() => { deferred.Promise.CatchCancelation(100, default(Action <int, ReasonContainer>)); }); cancelationSource1.Cancel(); CancelationSource cancelationSource2 = CancelationSource.New(); var deferredInt = Promise.NewDeferred <int>(cancelationSource2.Token); Assert.AreEqual(Promise.State.Pending, deferredInt.State); Assert.Throws <ArgumentNullException>(() => { deferredInt.Promise.CatchCancelation(100, default(Action <int, ReasonContainer>)); }); cancelationSource2.Cancel(); cancelationSource1.Dispose(); cancelationSource2.Dispose(); TestHelper.Cleanup(); }
public void AllPromiseIsCanceledWhenBothPromisesAreCanceled() { CancelationSource cancelationSource1 = CancelationSource.New(); CancelationSource cancelationSource2 = CancelationSource.New(); var deferred1 = Promise.NewDeferred <int>(cancelationSource1.Token); var deferred2 = Promise.NewDeferred <int>(cancelationSource2.Token); var cancelations = 0; Promise.All(deferred1.Promise, deferred2.Promise) .Then(v => Assert.Fail("Promise was resolved when it should have been canceled.")) .CatchCancelation(e => { ++cancelations; }); Promise.All((Promise)deferred1.Promise, deferred2.Promise) .Then(() => Assert.Fail("Promise was resolved when it should have been canceled.")) .CatchCancelation(e => { ++cancelations; }); cancelationSource1.Cancel("Cancel!"); Promise.Manager.HandleCompletes(); Assert.AreEqual(2, cancelations); cancelationSource2.Cancel("Cancel!"); Promise.Manager.HandleCompletes(); Assert.AreEqual(2, cancelations); cancelationSource1.Dispose(); cancelationSource2.Dispose(); TestHelper.Cleanup(); }
public void ItMustNotBeCalledBeforePromiseIsCanceled() { string cancelReason = "Cancel value"; var canceled = false; CancelationSource cancelationSource = CancelationSource.New(); var deferred = Promise.NewDeferred(cancelationSource.Token); Assert.AreEqual(Promise.State.Pending, deferred.State); deferred.Promise.CatchCancelation(r => { Assert.AreEqual(cancelReason, r.Value); canceled = true; }); Promise.Manager.HandleCompletes(); Assert.False(canceled); cancelationSource.Cancel(cancelReason); Promise.Manager.HandleCompletes(); Assert.True(canceled); cancelationSource.Dispose(); TestHelper.Cleanup(); }
public void AsyncPromiseIsCanceledFromPromise2() { CancelationSource cancelationSource = CancelationSource.New(); var deferred = Promise.NewDeferred <int>(cancelationSource.Token); string expected = "Cancel"; bool canceled = false; async Promise <int> Func() { return(await deferred.Promise); } Func() .CatchCancelation(e => { Assert.AreEqual(expected, e.Value); canceled = true; }); Promise.Manager.HandleCompletes(); Assert.AreEqual(false, canceled); cancelationSource.Cancel(expected); Promise.Manager.HandleCompletes(); Assert.AreEqual(true, canceled); cancelationSource.Dispose(); TestHelper.Cleanup(); }
public void OnContinueIsNotInvokedIfTokenIsAlreadyCanceled() { CancelationSource cancelationSource = CancelationSource.New(); cancelationSource.Cancel(); var deferred = Promise.NewDeferred(); var deferredInt = Promise.NewDeferred <int>(); TestHelper.AddContinueCallbacksWithCancelation <float, string>( deferred.Promise, onContinue: _ => Assert.Fail("OnContinue was invoked."), onContinueCapture: (_, __) => Assert.Fail("OnContinue was invoked."), cancelationToken: cancelationSource.Token ); TestHelper.AddContinueCallbacksWithCancelation <int, float, string>( deferredInt.Promise, onContinue: _ => Assert.Fail("OnContinue was invoked."), onContinueCapture: (_, __) => Assert.Fail("OnContinue was invoked."), cancelationToken: cancelationSource.Token ); deferred.Resolve(); deferredInt.Resolve(1); Promise.Manager.HandleCompletes(); cancelationSource.Dispose(); TestHelper.Cleanup(); }
public void PromiseIsCanceledFromToken() { CancelationSource cancelationSource = CancelationSource.New(); var deferred = Promise.NewDeferred(); var deferredInt = Promise.NewDeferred <int>(); int cancelCallbacks = 0; TestHelper.AddResolveCallbacksWithCancelation <float, string>( deferred.Promise, cancelationToken: cancelationSource.Token, onCancel: _ => ++ cancelCallbacks, onCancelCapture: (_, __) => ++ cancelCallbacks ); TestHelper.AddCallbacksWithCancelation <float, object, string>( deferred.Promise, cancelationToken: cancelationSource.Token, onCancel: _ => ++ cancelCallbacks, onCancelCapture: (_, __) => ++ cancelCallbacks ); TestHelper.AddContinueCallbacksWithCancelation <float, string>( deferred.Promise, cancelationToken: cancelationSource.Token, onCancel: _ => ++ cancelCallbacks, onCancelCapture: (_, __) => ++ cancelCallbacks ); TestHelper.AddResolveCallbacksWithCancelation <int, float, string>( deferredInt.Promise, cancelationToken: cancelationSource.Token, onCancel: _ => ++ cancelCallbacks, onCancelCapture: (_, __) => ++ cancelCallbacks ); TestHelper.AddCallbacksWithCancelation <int, float, object, string>( deferredInt.Promise, cancelationToken: cancelationSource.Token, onCancel: _ => ++ cancelCallbacks, onCancelCapture: (_, __) => ++ cancelCallbacks ); TestHelper.AddContinueCallbacksWithCancelation <int, float, string>( deferredInt.Promise, cancelationToken: cancelationSource.Token, onCancel: _ => ++ cancelCallbacks, onCancelCapture: (_, __) => ++ cancelCallbacks ); cancelationSource.Cancel(); deferred.Resolve(); deferredInt.Resolve(1); Promise.Manager.HandleCompletes(); Assert.AreEqual( TestHelper.cancelVoidCallbacks + TestHelper.cancelTCallbacks, cancelCallbacks ); cancelationSource.Dispose(); TestHelper.Cleanup(); }
public void IfPromiseIsCanceledAndAPreviousPromiseIsAlsoCanceledPromiseMustBeCanceledWithTheInitialCancelReason() { var deferred = Promise.NewDeferred(); Assert.AreEqual(Promise.State.Pending, deferred.State); bool invoked = false; object cancelValue = "Cancel"; CancelationSource cancelationSource = CancelationSource.New(); deferred.Promise .Then(() => { }, cancelationSource.Token) .CatchCancelation(reason => { Assert.AreEqual(cancelValue, reason.Value); invoked = true; }) .Finally(cancelationSource.Dispose); cancelationSource.Cancel(cancelValue); deferred.Resolve(); Promise.Manager.HandleCompletes(); Assert.AreEqual(true, invoked); TestHelper.Cleanup(); }
public void OnRejectedIsNotInvokedIfTokenIsAlreadyCanceled() { CancelationSource cancelationSource = CancelationSource.New(); cancelationSource.Cancel(); var deferred = Promise.NewDeferred(); var deferredInt = Promise.NewDeferred <int>(); TestHelper.AddCallbacksWithCancelation <float, object, string>( deferred.Promise, onReject: _ => Assert.Fail("OnRejected was invoked."), onUnknownRejection: () => Assert.Fail("OnRejected was invoked."), onRejectCapture: _ => Assert.Fail("OnRejected was invoked."), onUnknownRejectionCapture: _ => Assert.Fail("OnRejected was invoked."), cancelationToken: cancelationSource.Token ); TestHelper.AddCallbacksWithCancelation <int, float, object, string>( deferredInt.Promise, onReject: _ => Assert.Fail("OnRejected was invoked."), onUnknownRejection: () => Assert.Fail("OnRejected was invoked."), onRejectCapture: _ => Assert.Fail("OnRejected was invoked."), onUnknownRejectionCapture: _ => Assert.Fail("OnRejected was invoked."), cancelationToken: cancelationSource.Token ); deferred.Reject("Reject"); deferredInt.Reject("Reject"); Assert.Throws <AggregateException>(Promise.Manager.HandleCompletes); cancelationSource.Dispose(); TestHelper.Cleanup(); }
public void IfPromiseIsCanceledOnResolveAndOnRejectedMustNotBeInvoked() { CancelationSource cancelationSource1 = CancelationSource.New(); var deferred = Promise.NewDeferred(cancelationSource1.Token); Assert.AreEqual(Promise.State.Pending, deferred.State); CancelationSource cancelationSource2 = CancelationSource.New(); var deferredInt = Promise.NewDeferred <int>(cancelationSource2.Token); Assert.AreEqual(Promise.State.Pending, deferredInt.State); Action resolveAssert = () => Assert.Fail("Promise was resolved when it should have been canceled."); Action rejectAssert = () => Assert.Fail("Promise was rejected when it should have been canceled."); TestHelper.AddCallbacks <int, object, string>(deferred.Promise, onResolve: resolveAssert, onReject: _ => rejectAssert(), onUnknownRejection: rejectAssert); TestHelper.AddCallbacks <int, bool, object, string>(deferredInt.Promise, onResolve: _ => resolveAssert(), onReject: _ => rejectAssert(), onUnknownRejection: rejectAssert); cancelationSource1.Cancel(); cancelationSource2.Cancel(); Promise.Manager.HandleCompletes(); cancelationSource1.Dispose(); cancelationSource2.Dispose(); TestHelper.Cleanup(); }
public void MayTransitionToEitherTheFulfilledOrRejectedOrCanceledState() { var deferred = Promise.NewDeferred(); Assert.AreEqual(Promise.State.Pending, deferred.State); deferred.Resolve(); Assert.AreEqual(Promise.State.Resolved, deferred.State); deferred = Promise.NewDeferred(); Assert.AreEqual(Promise.State.Pending, deferred.State); deferred.Reject("Fail Value"); Assert.AreEqual(Promise.State.Rejected, deferred.State); CancelationSource cancelationSource = CancelationSource.New(); deferred = Promise.NewDeferred(cancelationSource.Token); Assert.AreEqual(Promise.State.Pending, deferred.State); cancelationSource.Cancel(); Assert.AreEqual(Promise.State.Canceled, deferred.State); Assert.Throws <AggregateException>(Promise.Manager.HandleCompletes); cancelationSource.Dispose(); TestHelper.Cleanup(); }
public void OnContinueIsInvokedWhenPromiseIsCanceled() { CancelationSource cancelationSource1 = CancelationSource.New(); var deferred = Promise.NewDeferred(cancelationSource1.Token); CancelationSource cancelationSource2 = CancelationSource.New(); var deferred2 = Promise.NewDeferred(cancelationSource2.Token); int voidFinallyFired = 0; int intFinallyFired = 0; TestHelper.AddContinueCallbacks <int, string>(deferred.Promise, onContinue: r => ++ voidFinallyFired ); TestHelper.AddContinueCallbacks <int, string>(deferred2.Promise, onContinue: r => ++ intFinallyFired ); cancelationSource1.Cancel(); cancelationSource2.Cancel("Cancel"); Promise.Manager.HandleCompletes(); Assert.AreEqual(TestHelper.continueVoidCallbacks * 2, voidFinallyFired); Assert.AreEqual(TestHelper.continueTCallbacks * 2, intFinallyFired); cancelationSource1.Dispose(); cancelationSource2.Dispose(); TestHelper.Cleanup(); }
public void CancelAwaitedPromiseThrowsOperationCanceled2() { CancelationSource cancelationSource = CancelationSource.New(); var deferred = Promise.NewDeferred <int>(cancelationSource.Token); string cancelValue = "Cancel"; bool continued = false; async void Func() { try { int value = await deferred.Promise; } catch (CanceledException e) { Assert.AreEqual(cancelValue, e.Value); continued = true; } } Func(); Promise.Manager.HandleCompletes(); Assert.AreEqual(false, continued); cancelationSource.Cancel(cancelValue); Promise.Manager.HandleCompletes(); Assert.AreEqual(true, continued); cancelationSource.Dispose(); TestHelper.Cleanup(); }
public void OnFinallyWillBeInvokedWithCapturedValue2() { CancelationSource cancelationSource1 = CancelationSource.New(); var deferred = Promise.NewDeferred(cancelationSource1.Token); CancelationSource cancelationSource2 = CancelationSource.New(); var deferredInt = Promise.NewDeferred <int>(cancelationSource2.Token); string expected = "expected"; bool invoked = false; deferred.Promise .Finally(expected, cv => { Assert.AreEqual(expected, cv); invoked = true; }); deferredInt.Promise .Finally(expected, cv => { Assert.AreEqual(expected, cv); invoked = true; }); cancelationSource1.Cancel(); cancelationSource2.Cancel(); var cancelationSource3 = CancelationSource.New(); deferred = Promise.NewDeferred(cancelationSource3.Token); var cancelationSource4 = CancelationSource.New(); deferredInt = Promise.NewDeferred <int>(cancelationSource4.Token); deferred.Promise .Finally(expected, cv => { Assert.AreEqual(expected, cv); invoked = true; }); deferredInt.Promise .Finally(expected, cv => { Assert.AreEqual(expected, cv); invoked = true; }); cancelationSource3.Cancel("Cancel"); cancelationSource4.Cancel("Cancel"); Promise.Manager.HandleCompletes(); Assert.AreEqual(true, invoked); cancelationSource1.Dispose(); cancelationSource2.Dispose(); cancelationSource3.Dispose(); cancelationSource4.Dispose(); TestHelper.Cleanup(); }
public void OnFinallyIsInvokedWhenPromiseIsCanceled() { CancelationSource cancelationSource1 = CancelationSource.New(); var deferred = Promise.NewDeferred(cancelationSource1.Token); CancelationSource cancelationSource2 = CancelationSource.New(); var deferredInt = Promise.NewDeferred <int>(cancelationSource2.Token); bool voidFinallyFired = false; bool intFinallyFired = false; deferred.Promise.Finally(() => voidFinallyFired = true); deferredInt.Promise.Finally(() => intFinallyFired = true); cancelationSource1.Cancel(); cancelationSource2.Cancel(); Promise.Manager.HandleCompletes(); Assert.AreEqual(true, voidFinallyFired); Assert.AreEqual(true, intFinallyFired); cancelationSource1.Dispose(); cancelationSource2.Dispose(); voidFinallyFired = false; intFinallyFired = false; cancelationSource1 = CancelationSource.New(); deferred = Promise.NewDeferred(cancelationSource1.Token); cancelationSource2 = CancelationSource.New(); deferredInt = Promise.NewDeferred <int>(cancelationSource2.Token); deferred.Promise.Finally(() => voidFinallyFired = true); deferredInt.Promise.Finally(() => intFinallyFired = true); cancelationSource1.Cancel("Cancel"); cancelationSource2.Cancel("Cancel"); Promise.Manager.HandleCompletes(); Assert.AreEqual(true, voidFinallyFired); Assert.AreEqual(true, intFinallyFired); cancelationSource1.Dispose(); cancelationSource2.Dispose(); TestHelper.Cleanup(); }
public void CancelationSourceIsValidAfterCancel1() { CancelationSource cancelationSource = CancelationSource.New(); cancelationSource.Cancel("Canceled"); Assert.IsTrue(cancelationSource.IsValid); cancelationSource.Dispose(); TestHelper.Cleanup(); }
public void CancelationSourceCancelationRequestedAfterCanceled0() { CancelationSource cancelationSource = CancelationSource.New(); cancelationSource.Cancel(); Assert.IsTrue(cancelationSource.IsCancelationRequested); cancelationSource.Dispose(); TestHelper.Cleanup(); }
public void MustHaveAReasonWhichMustNotChange_0() { CancelationSource cancelationSource = CancelationSource.New(); var deferred = Promise.NewDeferred(cancelationSource.Token); Assert.AreEqual(Promise.State.Pending, deferred.State); deferred.Retain(); object cancelation = null; string expected = "Cancel Value"; Action rejectAssert = () => Assert.Fail("Promise was rejected when it should have been resolved."); Action resolveAssert = () => Assert.Fail("Promise was resolved when it should have been rejected."); TestHelper.AddCallbacks <int, object, string>(deferred.Promise, onResolve: resolveAssert, onReject: failValue => rejectAssert(), onUnknownRejection: rejectAssert); deferred.Promise.CatchCancelation(cancelValue => Assert.AreEqual(expected, cancelation = cancelValue.Value)); cancelationSource.Cancel(expected); Promise.Manager.HandleCompletes(); Assert.AreEqual(expected, cancelation); TestHelper.AddCallbacks <int, object, string>(deferred.Promise, onResolve: resolveAssert, onReject: failValue => rejectAssert(), onUnknownRejection: rejectAssert); deferred.Promise.CatchCancelation(cancelValue => Assert.AreEqual(expected, cancelation = cancelValue.Value)); Assert.Throws <InvalidOperationException>(() => cancelationSource.Cancel("Different Cancel Value") ); deferred.Release(); Promise.Manager.HandleCompletes(); Assert.AreEqual(expected, cancelation); cancelationSource.Dispose(); TestHelper.Cleanup(); }