public void Test_ImageFormat_Change_CancelsOtherRefreshTasks() { // Arrange. diagram.File = new FileInfo("TestFile.puml"); diagram.ImageFile = new FileInfo("image.png"); bool killSwitch = false; var tasks = new List <Task>(2); compiler.Setup(c => c.CompileToImageAsync(It.IsAny <string>(), It.IsAny <ImageFormat>(), It.IsAny <CancellationToken>())) .Returns((string content, ImageFormat format, CancellationToken token) => { var task = Task.Run(() => { while (!killSwitch) { token.ThrowIfCancellationRequested(); Thread.Sleep(100); } return((ImageSource)null); }, token); tasks.Add(task); return(task); }); editor = CreateEditor(); editor.RefreshAsync(); // Act. editor.ImageFormat = ImageFormat.SVG; // Assert. Assert.Equal(2, tasks.Count); AssertThat.Throws <OperationCanceledException>(() => tasks[0]); Assert.Equal(TaskStatus.Canceled, tasks[0].Status); killSwitch = true; AssertThat.DoesNotThrow(() => tasks[1]); Assert.Equal(TaskStatus.RanToCompletion, tasks[1].Status); }