Example #1
0
        public void IfWriteAsyncThrowsWhileAwaitingHandleErrorIsCalled()
        {
            var logProcessor = new FireAndForgetLogProcessor();

            var logProvider = new FakeLogProvider();
            var logEntry    = new LogEntry();

            Error capturedError = null;
            var   waitHandle    = new AutoResetEvent(false);

            IErrorHandler errorHandler = DelegateErrorHandler.New(error =>
            {
                capturedError = error;
                waitHandle.Set();
            });

            logProcessor.Unlock().SendToLogProvider(logProvider, logEntry, errorHandler, 1);

            waitHandle.WaitOne(10000).Should().BeTrue();

            capturedError.Should().NotBeNull();
            capturedError.Exception.Message.Should().Be("oh, no.");
            capturedError.LogProvider.Should().BeSameAs(logProvider);
            capturedError.LogEntry.Should().BeSameAs(logEntry);
            capturedError.FailureCount.Should().Be(2);
        }
Example #2
0
        public void IfErrorHandlerSetsShouldRetryToTrueSendToLogProviderIsCalled()
        {
            IErrorHandler errorHandler = DelegateErrorHandler.New(error =>
            {
                if (error.FailureCount < 2)
                {
                    error.ShouldRetry = true;
                }
            });

            var logProcessor = new TestLogProcessor();

            var exception   = new Exception();
            var logProvider = new Mock <ILogProvider>().Object;
            var logEntry    = new LogEntry();

            logProcessor.Unlock().HandleError(exception, logProvider, logEntry, errorHandler, 1, "Oops: {0}", new object[] { 123 });

            logProcessor.SendToLogProviderInvocations.Count.Should().Be(1);

            var sendToLogProviderInvocation = logProcessor.SendToLogProviderInvocations[0];

            sendToLogProviderInvocation.LogProvider.Should().BeSameAs(logProvider);
            sendToLogProviderInvocation.LogEntry.Should().BeSameAs(logEntry);
            sendToLogProviderInvocation.ErrorHandler.Should().BeSameAs(errorHandler);
            sendToLogProviderInvocation.FailureCount.Should().Be(1);
        }
Example #3
0
        public void IfWriteAsyncTimesOutHandleErrorIsCalled()
        {
            var logProcessor = new BackgroundLogProcessor();

            var mockLogProvider = new Mock <ILogProvider>();
            var logEntry        = new LogEntry();

            mockLogProvider.Setup(m => m.WriteAsync(It.IsAny <LogEntry>(), It.IsAny <CancellationToken>())).Returns(() => Task.Delay(200));
            mockLogProvider.Setup(m => m.Timeout).Returns(TimeSpan.FromMilliseconds(10));

            Error capturedError = null;
            var   waitHandle    = new AutoResetEvent(false);

            IErrorHandler errorHandler = DelegateErrorHandler.New(error =>
            {
                capturedError = error;
                waitHandle.Set();
            });

            logProcessor.Unlock().SendToLogProvider(mockLogProvider.Object, logEntry, errorHandler, 1);

            waitHandle.WaitOne(10000).Should().BeTrue();

            capturedError.Should().NotBeNull();
            capturedError.IsTimeout.Should().BeTrue();
            capturedError.LogProvider.Should().BeSameAs(mockLogProvider.Object);
            capturedError.LogEntry.Should().BeSameAs(logEntry);
            capturedError.FailureCount.Should().Be(2);
        }
Example #4
0
        public void IfRetriedSendToLogProviderThrowsHandleErrorIsCalled()
        {
            IErrorHandler errorHandler = DelegateErrorHandler.New(error =>
            {
                if (error.FailureCount < 2)
                {
                    error.ShouldRetry = true;
                }
            });

            var logProcessor = new TestLogProcessor(sendToLogProviderShouldThrow: true);

            var exception   = new Exception();
            var logProvider = new Mock <ILogProvider>().Object;
            var logEntry    = new LogEntry();

            logProcessor.Unlock().HandleError(exception, logProvider, logEntry, errorHandler, 1, "Oops: {0}", new object[] { 123 });

            logProcessor.HandleErrorInvocations.Count.Should().Be(2);

            var invocation1 = logProcessor.HandleErrorInvocations[0];
            var invocation2 = logProcessor.HandleErrorInvocations[1];

            // Original HandleError call
            invocation1.Exception.Should().BeSameAs(exception);
            invocation1.LogProvider.Should().BeSameAs(logProvider);
            invocation1.LogEntry.Should().BeSameAs(logEntry);
            invocation1.FailureCount.Should().Be(1);
            invocation1.ErrorMessageFormat.Should().Be("Oops: {0}");

            // Resend HandleError call
            invocation2.Exception.Should().NotBeSameAs(exception);
            invocation2.Exception.Message.Should().Be("error.");
            invocation2.LogProvider.Should().BeSameAs(logProvider);
            invocation2.LogEntry.Should().BeSameAs(logEntry);
            invocation2.FailureCount.Should().Be(2);
            invocation2.ErrorMessageFormat.Should().Be("Error while re-sending log entry {0} to log provider {1}.");
        }
Example #5
0
        public void HandleErrorInvokesErrorHandlerCallbackWhenProvided()
        {
            Error capturedError = null;

            IErrorHandler errorHandler = DelegateErrorHandler.New(error =>
            {
                capturedError = error;
            });

            var logProcessor = new TestLogProcessor();

            var exception   = new Exception();
            var logProvider = new Mock <ILogProvider>().Object;
            var logEntry    = new LogEntry();

            logProcessor.Unlock().HandleError(exception, logProvider, logEntry, errorHandler, 321, "Oops: {0}", new object[] { 123 });

            capturedError.Should().NotBeNull();
            capturedError.Exception.Should().BeSameAs(exception);
            capturedError.LogProvider.Should().BeSameAs(logProvider);
            capturedError.LogEntry.Should().BeSameAs(logEntry);
            capturedError.FailureCount.Should().Be(321);
            capturedError.Message.Should().Be("Oops: 123");
        }
Example #6
0
        public void IfWriteAsyncThrowsWhileAwaitingHandleErrorIsCalled()
        {
            var logProcessor = new BackgroundLogProcessor();

            var logProvider = new FakeLogProvider();
            var logEntry    = new LogEntry();

            Error capturedError = null;

            IErrorHandler errorHandler = DelegateErrorHandler.New(error =>
            {
                capturedError = error;
            });

            logProcessor.Unlock().SendToLogProvider(logProvider, logEntry, errorHandler, 1);

            logProcessor.Dispose();

            capturedError.Should().NotBeNull();
            capturedError.Exception.Message.Should().Be("oh, no.");
            capturedError.LogProvider.Should().BeSameAs(logProvider);
            capturedError.LogEntry.Should().BeSameAs(logEntry);
            capturedError.FailureCount.Should().Be(2);
        }