public void Test_ReadDiagramsAsync_Cancelled() { // Arrange. var progress = new Mock <IProgress <ReadDiagramsProgress> >(); progress.Setup(p => p.Report(It.IsAny <ReadDiagramsProgress>())); var tcs = new CancellationTokenSource(); tcs.Cancel(); // Act. var readTask = diagramIO.ReadDiagramsAsync(currentDirectory, progress.Object, tcs.Token); // Assert. AssertThat.Throws <TaskCanceledException>(() => readTask); Assert.True(readTask.IsCanceled); }
public void AppendToCollection_CalledAfterTheFirstItemIsRequested_ThrowsExpectedException() { // Arrange var container = ContainerFactory.New(); var registration1 = Lifestyle.Transient.CreateRegistration <IPlugin, PluginImpl>(container); var registration2 = Lifestyle.Transient.CreateRegistration <IPlugin, PluginImpl2>(container); container.AppendToCollection(typeof(IPlugin), registration1); var instances = container.GetAllInstances <IPlugin>().ToArray(); // Act Action action = () => container.AppendToCollection(typeof(IPlugin), registration2); // Assert AssertThat.Throws <InvalidOperationException>(action); }
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); }