public void execute_logs_any_error_raised_by_the_inner_action()
        {
            var waitHandle = new ManualResetEventSlim();
            var logger = new LoggerMock(MockBehavior.Loose);
            var loggerService = new LoggerServiceMock(MockBehavior.Loose);
            var action = new ActionMock(MockBehavior.Loose);

            logger
                .When(x => x.Log(LogLevel.Error, It.IsAny<string>()))
                .Do(waitHandle.Set);

            loggerService
                .When(x => x.GetLogger(It.IsAny<Type>()))
                .Return(logger);

            action
                .When(x => x.Execute(It.IsAny<ExecutionContext>()))
                .Return(Observable.Throw<Unit>(new InvalidOperationException("Something bad happened")));

            var sut = new DoNotAwaitActionBuilder()
                .WithLoggerService(loggerService)
                .WithInnerAction(action)
                .Build();

            sut.Execute(new ExecutionContext());

            Assert.True(waitHandle.Wait(TimeSpan.FromSeconds(3)));
        }
        public void save_async_logs_an_error_if_a_save_callback_fails()
        {
            var logger = new LoggerMock(MockBehavior.Loose);
            var loggerService = new LoggerServiceMock(MockBehavior.Loose);

            loggerService
                .When(x => x.GetLogger(typeof(StateService)))
                .Return(logger);

            var sut = new StateServiceBuilder()
                .WithLoggerService(loggerService)
                .Build();

            sut.RegisterSaveCallback(_ => Observable.Throw<Unit>(new Exception("whatever")));

            sut.SaveAsync();

            logger
                .Verify(x => x.Error(It.IsAny<Exception>(), It.IsAny<string>(), It.IsAny<object[]>()))
                .WasCalledExactlyOnce();
        }
        public void save_async_ignores_any_null_tasks_returned_by_saved_callbacks()
        {
            var logger = new LoggerMock(MockBehavior.Loose);
            var loggerService = new LoggerServiceMock(MockBehavior.Loose);

            loggerService
                .When(x => x.GetLogger(typeof(StateService)))
                .Return(logger);

            var sut = new StateServiceBuilder()
                .WithLoggerService(loggerService)
                .Build();

            var firstExecuted = false;
            var secondExecuted = false;
            sut
                .RegisterSaveCallback(
                    _ =>
                    {
                        firstExecuted = true;
                        return Observable.Return(Unit.Default);
                    });
            sut.RegisterSaveCallback(_ => null);
            sut
                .RegisterSaveCallback(
                    _ =>
                    {
                        secondExecuted = true;
                        return Observable.Return(Unit.Default);
                    });

            sut.SaveAsync();

            Assert.True(firstExecuted);
            Assert.True(secondExecuted);

            loggerService
                .Verify(x => x.GetLogger(typeof(StateService)))
                .WasCalledExactlyOnce();

            logger
                .Verify(x => x.Error(It.IsAny<string>()))
                .WasNotCalled();

            logger
                .Verify(x => x.Error(It.IsAny<string>(), It.IsAny<object[]>()))
                .WasNotCalled();

            logger
                .Verify(x => x.Error(It.IsAny<Exception>(), It.IsAny<string>(), It.IsAny<object[]>()))
                .WasNotCalled();
        }