public void CancelationSourceLifecycle_Fact() { CancellationSource source; using (source = new CancellationSource()) { source.IsCancelationRequested.ShouldBeFalse(); source.IsDisposed.ShouldBeFalse(); source.Cancel(); source.IsCancelationRequested.ShouldBeTrue(); source.IsDisposed.ShouldBeFalse(); source.Cancel(); source.IsCancelationRequested.ShouldBeTrue(); source.IsDisposed.ShouldBeFalse(); } source.IsDisposed.ShouldBeTrue(); }
public void RegisterBeforeCancel_Fact() { object callbackCreated = null; var source = new CancellationSource(); source.Token.Register(() => callbackCreated = new object()); callbackCreated.ShouldBeNull(); source.Cancel(); callbackCreated.ShouldNotBeNull(); }
public void MergeAndCancelSecond_Fact() { var source1 = new CancellationSource(); var source2 = new CancellationSource(); var merged = CancellationSource.Merge(source1, source2); source2.Cancel(); source1.IsCancelationRequested.ShouldBeFalse(); source2.IsCancelationRequested.ShouldBeTrue(); merged.IsCancelationRequested.ShouldBeTrue(); //ensure safety in calling other cancels source1.Cancel(); merged.Cancel(); }
public void DisposedThrows_Fact() { CancellationSource source; using (source = new CancellationSource()) { source.IsCancelationRequested.ShouldBeFalse(); } try { source.Cancel(); } catch (ObjectDisposedException) { return; } throw new Exception("CancellationSource not disposed."); }
public void WaitTimeoutOccurs_Fact() { var cancellationSource = new CancellationSource(); var task = Task.Run(token => { while (!token.IsCancellationRequested) { Thread.Sleep(10); } }, cancellationSource); bool waitSuccess = task.Wait(15); waitSuccess.ShouldBeFalse(); cancellationSource.Cancel(); EnsureQuietDisposal(task); }
public void CancelActionTask_NoisyWait_Fact() { var cancellationSource = new CancellationSource(); var task = Task.Run(token => { for (int i = 0; i < 1000000; i++) { token.ThrowIfCancellationRequested(); Thread.Sleep(10); } }, cancellationSource); Thread.Sleep(15); cancellationSource.Cancel(); var waitException = Trap.WaitException(task); ValidateCancellationException(waitException); ValidateCanceledTask(task); EnsureQuietDisposal(task); }
public void CancelActionTask_QuietBackground_Fact() { var cancellationSource = new CancellationSource(); var task = Task.Run(token => { for (int i = 0; i < 1000000; i++) { token.ThrowIfCancellationRequested(); Thread.Sleep(10); } }, cancellationSource); Thread.Sleep(15); cancellationSource.Cancel(); while (!task.IsComplete) { Thread.Sleep(5); } ValidateCanceledTask(task); EnsureQuietDisposal(task); }
public void ActionTask_CancelTaskAndActionContinuation_Fact() { var cs = new CancellationSource(); var task = Task.Run(token => { cs.Cancel(); Thread.Sleep(10); token.ThrowIfCancellationRequested(); throw new Exception("We should have been canceled, and never see this."); }, cs); var continuation = task.ContinueWith((previous, token) => { // we should never see the following exception throw new Exception("Pre canceled continuation should not run"); }, cs); continuation.Wait(); task.Status.ShouldEqual(TaskStatus.Canceled); continuation.Status.ShouldEqual(TaskStatus.Canceled); EnsureQuietDisposal(task); EnsureQuietDisposal(continuation); }