Ejemplo n.º 1
0
        public void SafeContinueWith_OnExceptionExceptionSet_HandlesException()
        {
            SpecificException  specificException = new SpecificException();
            Action <Exception> onException       = new Mock <Action <Exception> >().Object;

            SafeExecutionHelpers.Initialize();
            var mockHelpers = new Mock <ISafeExecutionHelpers>();

            SafeExecutionHelpers.SetImplementation(mockHelpers.Object);

            var dts = new DeterministicTaskScheduler(shouldThrowExceptions: false);
            var sut = new SafeTask();

            sut.SafeContinueWith <Exception>(
                Task.Factory.StartNew(
                    () => throw specificException,
                    CancellationToken.None,
                    TaskCreationOptions.None,
                    dts),
                onException, //The crux
                dts);

            dts.RunTasksUntilIdle();

            Assert.Contains(specificException, dts.Exceptions);
            mockHelpers.Verify(h => h.HandleException(specificException, onException));

            SafeExecutionHelpers.RevertToDefaultImplementation();
        }
Ejemplo n.º 2
0
        public void SafeContinueWith_DefaultExceptionHandlerSet_HandlesException()
        {
            SpecificException  specificException       = new SpecificException();
            Action <Exception> defaultExceptionHandler = new Mock <Action <Exception> >().Object;

            SafeExecutionHelpers.Initialize();
            var mockHelpers = new Mock <ISafeExecutionHelpers>();

            //Crux - DefaultHandler returns non-null delegate
            mockHelpers.SetupGet(h => h.DefaultExceptionHandler).Returns(defaultExceptionHandler);

            SafeExecutionHelpers.SetImplementation(mockHelpers.Object);

            var dts = new DeterministicTaskScheduler(shouldThrowExceptions: false);
            var sut = new SafeTask();

            sut.SafeContinueWith <Exception>(
                Task.Factory.StartNew(
                    () => throw specificException,
                    CancellationToken.None,
                    TaskCreationOptions.None,
                    dts),
                null,
                dts);

            dts.RunTasksUntilIdle();

            Assert.Contains(specificException, dts.Exceptions);
            mockHelpers.Verify(h => h.HandleException <Exception>(specificException, null));

            SafeExecutionHelpers.RevertToDefaultImplementation();
        }
Ejemplo n.º 3
0
        public void ExecuteT_SecondCallAfterException_Executes()
        {
            int times = 0;

            async Task MockTask(string text)
            {
                await Task.Delay(Delay);

                if (times++ == 0)
                {
                    throw new Exception(); //Throws only on first try
                }
            }

            var      dts     = new DeterministicTaskScheduler(shouldThrowExceptions: false);
            ICommand command = new SafeCommand <string>(MockTask, dts, null, null);

            command.Execute("test");
            dts.RunTasksUntilIdle();

            command.Execute("test");
            dts.RunTasksUntilIdle();

            Assert.NotEmpty(dts.Exceptions);
            Assert.Equal(2, times);
        }
Ejemplo n.º 4
0
        public void ExecuteT_IViewModelBase_CalledTwice_FiresOnceIfBusy()
        {
            int times = 0;

            async Task MockTask(string text)
            {
                await Task.Delay(Delay);

                times++;
            }

            var mockVm = new Mock <IViewModelBase>();

            var      dts     = new DeterministicTaskScheduler(shouldThrowExceptions: false);
            ICommand command = new SafeCommand <string>(MockTask, dts, mockVm.Object);

            command.Execute("test");
            command.Execute("test");
            dts.RunTasksUntilIdle();

            Assert.Equal(1, times);
            mockVm.VerifyGet(vm => vm.IsBusy, Times.Exactly(2));
            mockVm.VerifySet(vm => vm.IsBusy = true);
            mockVm.VerifySet(vm => vm.IsBusy = false);
        }
Ejemplo n.º 5
0
        public void SafeContinueWith_NothingSet_HandleExceptionRuns()
        {
            SpecificException specificException = new SpecificException();

            SafeExecutionHelpers.RevertToDefaultImplementation();
            var mockHelpers = new Mock <ISafeExecutionHelpers>();

            SafeExecutionHelpers.Implementation = mockHelpers.Object;

            var dts = new DeterministicTaskScheduler(shouldThrowExceptions: false);
            var sut = new SafeTask();

            sut.SafeContinueWith <Exception>(
                Task.Factory.StartNew(
                    () => throw specificException,
                    CancellationToken.None,
                    TaskCreationOptions.None,
                    dts),
                null,
                dts);

            dts.RunTasksUntilIdle();

            Assert.Contains(specificException, dts.Exceptions);
            mockHelpers.Verify(h => h.HandleException <Exception>(specificException, null));

            SafeExecutionHelpers.RevertToDefaultImplementation();
        }
Ejemplo n.º 6
0
        public void AsyncCommand_ExecuteAsync_StringParameter_Test(string parameter)
        {
            //Arrange
            var      dts     = new DeterministicTaskScheduler();
            ICommand command = new SafeCommand <string>(StringParameterTask, dts, null, null);

            //Act
            command.Execute(parameter);
            dts.RunTasksUntilIdle();

            //Assert
        }
Ejemplo n.º 7
0
        public void ExecuteT_isBlockingFalse_CalledTwice_FiresTwiceIfBusy()
        {
            int times = 0;

            async Task MockTask(string text)
            {
                await Task.Delay(Delay);

                times++;
            }

            var      dts     = new DeterministicTaskScheduler(shouldThrowExceptions: false);
            ICommand command = new SafeCommand <string>(MockTask, dts, isBlocking: false);

            command.Execute("test");
            command.Execute("test");
            dts.RunTasksUntilIdle();

            Assert.Equal(2, times);
        }
        public void Execute_CalledTwice_FiresTwiceIfNotBusy()
        {
            int times = 0;

            void MockTask(string text)
            {
                times++;
            }

            var      dts     = new DeterministicTaskScheduler(shouldThrowExceptions: false);
            ICommand command = new SafeCommand <string>(MockTask);

            command.Execute("test");
            dts.RunTasksUntilIdle();

            command.Execute("test");
            dts.RunTasksUntilIdle();

            Assert.Equal(2, times);
        }
        public void ExecuteT_IBusy_isBlockingFalse_IsBusyPropertyUpdates()
        {
            int times = 0;

            async Task MockTask(string text)
            {
                await Task.Delay(Delay);

                times++;
            }

            var mockVm = new Mock <IBusy>();

            var      dts     = new DeterministicTaskScheduler(shouldThrowExceptions: false);
            ICommand command = new SafeCommand <string>(MockTask, dts, mockVm.Object);

            command.Execute("test");
            dts.RunTasksUntilIdle();

            Assert.Equal(1, times);
            mockVm.VerifyGet(vm => vm.IsBusy, Times.Exactly(1));
            mockVm.VerifySet(vm => vm.IsBusy = true);
            mockVm.VerifySet(vm => vm.IsBusy = false);
        }