Ejemplo n.º 1
0
        public async Task SetLogger_UpdatesLogger()
        {
            var test         = new LogRequestHandlerTest();
            var secondLogger = new Mock <ILogger>(MockBehavior.Strict);

            test.Handler.SetLogger(secondLogger.Object);

            var responseHandler = new Mock <IResponseHandler>(MockBehavior.Strict);

            responseHandler.Setup(x => x.SendResponseAsync(
                                      It.IsNotNull <Message>(),
                                      It.IsNotNull <LogResponse>(),
                                      It.IsAny <CancellationToken>()))
            .Returns(Task.FromResult(0));

            await test.Handler.HandleResponseAsync(
                Mock.Of <IConnection>(),
                test.Request,
                responseHandler.Object,
                CancellationToken.None);

            test.Logger.Verify();
            responseHandler.Verify();
            secondLogger.Verify();
        }
Ejemplo n.º 2
0
        public void SetLogger_ThrowsForNullLogger()
        {
            var test      = new LogRequestHandlerTest();
            var exception = Assert.Throws <ArgumentNullException>(
                () => test.Handler.SetLogger(logger: null));

            Assert.Equal("logger", exception.ParamName);
        }
Ejemplo n.º 3
0
        public async Task HandleResponseAsync_ThrowsIfCancelled()
        {
            var test = new LogRequestHandlerTest();

            await Assert.ThrowsAsync <OperationCanceledException>(
                () => test.Handler.HandleResponseAsync(
                    Mock.Of <IConnection>(),
                    test.Request,
                    Mock.Of <IResponseHandler>(),
                    new CancellationToken(canceled: true)));
        }
Ejemplo n.º 4
0
        public async Task HandleResponseAsync_ThrowsForNullResponseHandler()
        {
            var test = new LogRequestHandlerTest();

            var exception = await Assert.ThrowsAsync <ArgumentNullException>(
                () => test.Handler.HandleResponseAsync(
                    Mock.Of <IConnection>(),
                    test.Request,
                    responseHandler: null,
                    cancellationToken: CancellationToken.None));

            Assert.Equal("responseHandler", exception.ParamName);
        }
Ejemplo n.º 5
0
        private async Task HandleResponseAsync(
            LogLevel handlerLogLevel,
            LogLevel requestLogLevel,
            MessageResponseCode responseCode,
            bool expectLog)
        {
            var test    = new LogRequestHandlerTest(handlerLogLevel);
            var payload = new LogRequest(requestLogLevel, message: "a");
            var request = MessageUtilities.Create(
                requestId: "b",
                type: MessageType.Request,
                method: MessageMethod.Log,
                payload: payload);

            var responseHandler = new Mock <IResponseHandler>(MockBehavior.Strict);

            responseHandler.Setup(x => x.SendResponseAsync(
                                      It.Is <Message>(message => message == request),
                                      It.Is <LogResponse>(response => response.ResponseCode == responseCode),
                                      It.IsAny <CancellationToken>()))
            .Returns(Task.FromResult(0));

            if (expectLog)
            {
                test.Logger.Setup(
                    x => x.Log(It.Is <ILogMessage>(m => m.Message == payload.Message && m.Level == requestLogLevel)));
            }

            await test.Handler.HandleResponseAsync(
                Mock.Of <IConnection>(),
                request,
                responseHandler.Object,
                CancellationToken.None);

            test.Logger.Verify();
        }
Ejemplo n.º 6
0
        public void CancellationToken_IsNone()
        {
            var test = new LogRequestHandlerTest();

            Assert.Equal(CancellationToken.None, test.Handler.CancellationToken);
        }