public void HandleException_removes_newline()
        {
            var handler = new ActionExceptionHandler(_loggerMock.Object, _exceptionFormatterMock.Object);


            var reasonPhrase = "one" + Environment.NewLine + "two";

            var exception = new Exception(reasonPhrase);

            var actionContext = new HttpActionContext {
                Response = new HttpResponseMessage()
            };
            var context = new HttpActionExecutedContext(actionContext, exception);

            _loggerMock
            .Setup(x => x.Error(It.IsAny <string>(), exception));
            _exceptionFormatterMock
            .Setup(x => x.GetEntireExceptionStack(exception))
            .Returns(reasonPhrase);

            handler.HandleException(context);

            var actualReasonPhrase = actionContext.Response.ReasonPhrase;

            Assert.AreEqual(actualReasonPhrase, "one two");
        }
        public void HandleException_trims_message_if_too_long()
        {
            var handler = new ActionExceptionHandler(_loggerMock.Object, _exceptionFormatterMock.Object);


            var reasonPhrase = "".PadRight(ActionExceptionHandler.MaxStatusDescriptionLength, 'z');

            reasonPhrase += "a";

            var exception = new Exception(reasonPhrase);

            var actionContext = new HttpActionContext {
                Response = new HttpResponseMessage()
            };
            var context = new HttpActionExecutedContext(actionContext, exception);

            _loggerMock
            .Setup(x => x.Error(It.IsAny <string>(), exception));
            _exceptionFormatterMock
            .Setup(x => x.GetEntireExceptionStack(exception))
            .Returns(reasonPhrase);

            handler.HandleException(context);

            var actualReasonPhrase = actionContext.Response.ReasonPhrase;

            Assert.AreEqual(ActionExceptionHandler.MaxStatusDescriptionLength, actualReasonPhrase.Length);
            Assert.That(actualReasonPhrase, !Contains.Item('a'));
        }
        public void HandleException_does_nothing_if_no_exception()
        {
            var handler = new ActionExceptionHandler(_loggerMock.Object, _exceptionFormatterMock.Object);

            var context = new HttpActionExecutedContext {Exception = null};

            handler.HandleException(context);

            Assert.IsFalse(handler.ExceptionHandled);
        }
        public void HandleException_does_nothing_if_no_exception()
        {
            var handler = new ActionExceptionHandler(_loggerMock.Object, _exceptionFormatterMock.Object);

            var context = new HttpActionExecutedContext {
                Exception = null
            };

            handler.HandleException(context);

            Assert.IsFalse(handler.ExceptionHandled);
        }
        public void HandleException_exception_logged()
        {
            var handler = new ActionExceptionHandler(_loggerMock.Object, _exceptionFormatterMock.Object);

            const string reasonPhrase = "dumb thing don't work";
            var exception = new Exception(reasonPhrase);

            var actionContext = new HttpActionContext { Response = new HttpResponseMessage() };
            var context = new HttpActionExecutedContext(actionContext, exception);

            _loggerMock
                .Setup(x => x.Error(It.IsAny<string>(), exception));
            _exceptionFormatterMock
                .Setup(x => x.GetEntireExceptionStack(exception))
                .Returns(reasonPhrase);

            handler.HandleException(context);

            _loggerMock.VerifyAll();
        }
        public void ShouldCallExceptionHandlerOnUncaughtException()
        {
            var exceptionSignal     = new CountdownEvent(1);
            var exceptionHandler    = new ActionExceptionHandler <StubEvent>(x => exceptionSignal.Signal());
            var eventHandler        = new ActionEventHandler <StubEvent>(x => throw new NullReferenceException());
            var batchEventProcessor = new BatchEventProcessor <StubEvent>(_ringBuffer, _sequenceBarrier, eventHandler);

            _ringBuffer.AddGatingSequences(batchEventProcessor.Sequence);

            batchEventProcessor.SetExceptionHandler(exceptionHandler);

            var task = Task.Run(() => batchEventProcessor.Run());

            _ringBuffer.Publish(_ringBuffer.Next());

            Assert.IsTrue(exceptionSignal.Wait(TimeSpan.FromSeconds(2)));

            batchEventProcessor.Halt();

            Assert.IsTrue(task.Wait(500));
        }
        public void HandleException_removes_newline()
        {
            var handler = new ActionExceptionHandler(_loggerMock.Object, _exceptionFormatterMock.Object);

            var reasonPhrase = "one" + Environment.NewLine + "two";

            var exception = new Exception(reasonPhrase);

            var actionContext = new HttpActionContext { Response = new HttpResponseMessage() };
            var context = new HttpActionExecutedContext(actionContext, exception);

            _loggerMock
                .Setup(x => x.Error(It.IsAny<string>(), exception));
            _exceptionFormatterMock
                .Setup(x => x.GetEntireExceptionStack(exception))
                .Returns(reasonPhrase);

            handler.HandleException(context);

            var actualReasonPhrase = actionContext.Response.ReasonPhrase;
            Assert.AreEqual(actualReasonPhrase, "one two");
        }
        public void HandleException_exception_logged()
        {
            var handler = new ActionExceptionHandler(_loggerMock.Object, _exceptionFormatterMock.Object);


            const string reasonPhrase = "dumb thing don't work";
            var          exception    = new Exception(reasonPhrase);

            var actionContext = new HttpActionContext {
                Response = new HttpResponseMessage()
            };
            var context = new HttpActionExecutedContext(actionContext, exception);

            _loggerMock
            .Setup(x => x.Error(It.IsAny <string>(), exception));
            _exceptionFormatterMock
            .Setup(x => x.GetEntireExceptionStack(exception))
            .Returns(reasonPhrase);

            handler.HandleException(context);

            _loggerMock.VerifyAll();
        }
        public void HandleException_trims_message_if_too_long()
        {
            var handler = new ActionExceptionHandler(_loggerMock.Object, _exceptionFormatterMock.Object);

            var reasonPhrase = "".PadRight(ActionExceptionHandler.MaxStatusDescriptionLength, 'z');
            reasonPhrase += "a";

            var exception = new Exception(reasonPhrase);

            var actionContext = new HttpActionContext { Response = new HttpResponseMessage() };
            var context = new HttpActionExecutedContext(actionContext, exception);

            _loggerMock
                .Setup(x => x.Error(It.IsAny<string>(), exception));
            _exceptionFormatterMock
                .Setup(x => x.GetEntireExceptionStack(exception))
                .Returns(reasonPhrase);

            handler.HandleException(context);

            var actualReasonPhrase = actionContext.Response.ReasonPhrase;
            Assert.AreEqual(ActionExceptionHandler.MaxStatusDescriptionLength, actualReasonPhrase.Length);
            Assert.That(actualReasonPhrase, !Contains.Item('a'));
        }