public void Execute_TaskWithSteps_VerifyLogging() { var logger = Substitute.For <ILogger>(); var concurrentExecution = Substitute.For <IConcurrentTaskExecution>(); var shutdown = Substitute.For <IShutdown>(); var console = Substitute.For <IConsoleWriter>(); var step1 = Substitute.For <IStep <SomeWorkItem> >(); var step2 = Substitute.For <IStep <SomeWorkItem> >(); var workItem = new SomeWorkItem(); var task = new TaskRunnerTesterTask <SomeWorkItem>(new[] { step1, step2 }, workItem); concurrentExecution .Handle(Arg.Is(task), Arg.Any <Arguments>(), Arg.Any <TaskLog>()) .Returns(ConcurrentTaskExecutionResult.Continue()); ITaskExecutionContext <SomeWorkItem> ContextArg() => Arg.Is <ITaskExecutionContext <SomeWorkItem> >(context => context.WorkItem == workItem); step1.ContinueWith(ContextArg()).Returns(Execution.Execute); step2.ContinueWith(ContextArg()).Returns(Execution.Execute); var subject = new TaskRunner(logger, concurrentExecution, shutdown, console); subject.Execute(task); logger.Received().LogEntry(Arg.Any <TaskLog>()); logger.Received().LogEntry(Arg.Is <StepLog>(x => x.Name == step1.Name())); logger.Received().LogEntry(Arg.Is <StepLog>(x => x.Name == step2.Name())); step1.Received().Execute(ContextArg()); step2.Received().Execute(ContextArg()); Assert.That(task.EndCalled, Is.True); }
public void Execute_FailsAtStep_VerifyLogging() { var logger = Substitute.For <ILogger>(); var concurrentExecution = Substitute.For <IConcurrentTaskExecution>(); var shutdown = Substitute.For <IShutdown>(); var console = Substitute.For <IConsoleWriter>(); var step1 = Substitute.For <IStep <SomeWorkItem> >(); var step2 = Substitute.For <IStep <SomeWorkItem> >(); var workItem = new SomeWorkItem(); var task = new TaskRunnerTesterTask <SomeWorkItem>(new[] { step1, step2 }, workItem); var throwingException = new DivideByZeroException("error"); concurrentExecution .Handle(Arg.Is(task), Arg.Any <Arguments>(), Arg.Any <TaskLog>()) .Returns(ConcurrentTaskExecutionResult.Continue()); ITaskExecutionContext <SomeWorkItem> ContextArg() => Arg.Is <ITaskExecutionContext <SomeWorkItem> >(context => context.WorkItem == workItem); step1.ContinueWith(ContextArg()).Returns(Execution.Execute); step1 .When(x => x.Execute(ContextArg())) .Do(x => throw throwingException); var subject = new TaskRunner(logger, concurrentExecution, shutdown, console); var thrownException = Assert.Throws <TaskExecutionFailedException>(() => subject.Execute(task)); Assert.That(thrownException.InnerException, Is.EqualTo(throwingException)); logger.Received().LogEntry(Arg.Any <TaskLog>()); logger.Received().LogEntry(Arg.Is <StepLog>(x => x.Name == step1.Name())); step1.Received().Execute(ContextArg()); step2.DidNotReceive().Execute(ContextArg()); logger.Received().LogError(Arg.Is(throwingException)); Assert.That(task.EndCalled, Is.False); }
public void Execute_IDisposableWorkItem_Step2Fails() { var logger = Substitute.For <ILogger>(); var concurrentExecution = Substitute.For <IConcurrentTaskExecution>(); var shutdown = Substitute.For <IShutdown>(); var console = Substitute.For <IConsoleWriter>(); int disposedCount = 0; var step1 = Substitute.For <IStep <DisposableWorkItem> >(); var step2 = Substitute.For <IStep <DisposableWorkItem> >(); var workItem = new DisposableWorkItem(() => disposedCount++); var exception = new InvalidOperationException(); var task = new TaskRunnerTesterTask <DisposableWorkItem>(new[] { step1, step2 }, workItem); concurrentExecution .Handle(Arg.Is(task), Arg.Any <Arguments>(), Arg.Any <TaskLog>()) .Returns(ConcurrentTaskExecutionResult.Continue()); ITaskExecutionContext <DisposableWorkItem> ContextArg() => Arg.Is <ITaskExecutionContext <DisposableWorkItem> >(context => context.WorkItem == workItem); step1.ContinueWith(ContextArg()).Returns(Execution.Execute); step2.ContinueWith(ContextArg()).Returns(Execution.Execute); step2 .When(x => x.Execute(ContextArg())) .Do(x => throw exception); var subject = new TaskRunner(logger, concurrentExecution, shutdown, console); var thrownException = Assert.Throws <TaskExecutionFailedException>(() => subject.Execute(task)); step1.Received().Execute(ContextArg()); step2.Received().Execute(ContextArg()); Assert.That(task.EndCalled, Is.False); Assert.That(disposedCount, Is.EqualTo(1)); Assert.That(thrownException.InnerException, Is.SameAs(exception)); }