public async Task can_write_payload_successfully_with_creds()
        {
            var httpMessageHandlerMock = new Mock <HttpMessageHandler>();

            httpMessageHandlerMock.Protected()
            .Setup <Task <HttpResponseMessage> >(
                "SendAsync",
                ItExpr.IsAny <HttpRequestMessage>(),
                ItExpr.IsAny <CancellationToken>())
            .Returns(Task.FromResult(new HttpResponseMessage(HttpStatusCode.OK)));

            var client = new DefaultLineProtocolClient(
                new LoggerFactory(),
                new InfluxDBSettings("influx", new Uri("http://localhost"))
            {
                UserName = "******",
                Password = "******"
            },
                new HttpPolicy(),
                httpMessageHandlerMock.Object);

            var response = await client.WriteAsync(_payload, CancellationToken.None);

            response.Success.Should().BeTrue();
        }
Exemplo n.º 2
0
        public async Task should_back_off_when_reached_max_failures()
        {
            var httpMessageHandlerMock = new Mock <HttpMessageHandler>();

            httpMessageHandlerMock.Protected()
            .Setup <Task <HttpResponseMessage> >("SendAsync", ItExpr.IsAny <HttpRequestMessage>(), ItExpr.IsAny <CancellationToken>())
            .Returns(Task.FromResult(new HttpResponseMessage(HttpStatusCode.BadRequest)));
            var policy = new HttpPolicy {
                FailuresBeforeBackoff = 3
            };

            var client = new DefaultLineProtocolClient(new LoggerFactory(),
                                                       new InfluxDBSettings("influx", new Uri("http://localhost")), policy, httpMessageHandlerMock.Object);

            foreach (var attempt in Enumerable.Range(0, 10))
            {
                await client.WriteAsync(_payload, CancellationToken.None);

                // ReSharper disable ConvertIfStatementToConditionalTernaryExpression
                if (attempt <= policy.FailuresBeforeBackoff)
                // ReSharper restore ConvertIfStatementToConditionalTernaryExpression
                {
                    httpMessageHandlerMock.Protected()
                    .Verify <Task <HttpResponseMessage> >("SendAsync", Times.AtLeastOnce(), ItExpr.IsAny <HttpRequestMessage>(),
                                                          ItExpr.IsAny <CancellationToken>());
                }
                else
                {
                    httpMessageHandlerMock.Protected()
                    .Verify <Task <HttpResponseMessage> >("SendAsync", Times.AtMost(3), ItExpr.IsAny <HttpRequestMessage>(),
                                                          ItExpr.IsAny <CancellationToken>());
                }
            }
        }
        public void influxdb_settings_are_required()
        {
            Action action = () =>
            {
                var client = new DefaultLineProtocolClient(new LoggerFactory(), null, new HttpPolicy());
            };

            action.ShouldThrow <ArgumentNullException>();
        }
        public void http_policy_is_required()
        {
            Action action = () =>
            {
                var client = new DefaultLineProtocolClient(new LoggerFactory(), new InfluxDBSettings("influx", new Uri("http://localhost")), null);
            };

            action.ShouldThrow <ArgumentNullException>();
        }
Exemplo n.º 6
0
        public void databse_is_required()
        {
            Action action = () =>
            {
                var client = new DefaultLineProtocolClient(new LoggerFactory(), new InfluxDBSettings(null, new Uri("http://localhost")),
                                                           new HttpPolicy());
            };

            action.ShouldThrow <ArgumentException>();
        }
        public void Influxdb_settings_are_required()
        {
            // Arrange
            Action action = () =>
            {
                // Act
                var unused = new DefaultLineProtocolClient(null, new HttpPolicy(), new HttpClient());
            };

            // Assert
            action.Should().Throw <ArgumentNullException>();
        }
Exemplo n.º 8
0
        public async Task should_back_off_when_reached_max_failures_then_retry_after_backoff_period()
        {
            var httpMessageHandlerMock = new Mock <HttpMessageHandler>();

            httpMessageHandlerMock.Protected()
            .Setup <Task <HttpResponseMessage> >("SendAsync", ItExpr.IsAny <HttpRequestMessage>(), ItExpr.IsAny <CancellationToken>())
            .Returns(Task.FromResult(new HttpResponseMessage(HttpStatusCode.BadRequest)));
            var policy = new HttpPolicy {
                FailuresBeforeBackoff = 3, BackoffPeriod = TimeSpan.FromSeconds(1)
            };

            var client = new DefaultLineProtocolClient(new LoggerFactory(),
                                                       new InfluxDBSettings("influx", new Uri("http://localhost")), policy, httpMessageHandlerMock.Object);

            foreach (var attempt in Enumerable.Range(0, 10))
            {
                await client.WriteAsync(_payload, CancellationToken.None);

                if (attempt <= policy.FailuresBeforeBackoff)
                {
                    httpMessageHandlerMock.Protected()
                    .Verify <Task <HttpResponseMessage> >("SendAsync", Times.AtLeastOnce(), ItExpr.IsAny <HttpRequestMessage>(),
                                                          ItExpr.IsAny <CancellationToken>());
                }
                else
                {
                    httpMessageHandlerMock.Protected()
                    .Verify <Task <HttpResponseMessage> >("SendAsync", Times.AtMost(3), ItExpr.IsAny <HttpRequestMessage>(),
                                                          ItExpr.IsAny <CancellationToken>());
                }
            }

            await Task.Delay(policy.BackoffPeriod);

            httpMessageHandlerMock = new Mock <HttpMessageHandler>();
            httpMessageHandlerMock.Protected()
            .Setup <Task <HttpResponseMessage> >("SendAsync", ItExpr.IsAny <HttpRequestMessage>(), ItExpr.IsAny <CancellationToken>())
            .Returns(Task.FromResult(new HttpResponseMessage(HttpStatusCode.OK)));
            client = new DefaultLineProtocolClient(new LoggerFactory(),
                                                   new InfluxDBSettings("influx", new Uri("http://localhost")), policy, httpMessageHandlerMock.Object);

            var response = await client.WriteAsync(_payload, CancellationToken.None);

            response.Success.Should().BeTrue();
        }
        public void Http_policy_is_required()
        {
            // Arrange
            Action action = () =>
            {
                var settings = new InfluxDbOptions
                {
                    BaseUri  = new Uri("http://localhost"),
                    Database = "influx"
                };

                // Act
                var unused = new DefaultLineProtocolClient(settings, null, new HttpClient());
            };

            // Assert
            action.Should().Throw <ArgumentNullException>();
        }
Exemplo n.º 10
0
        public IMetricReporter CreateMetricReporter(string name, ILoggerFactory loggerFactory)
        {
            var lineProtocolClient = new DefaultLineProtocolClient(
                loggerFactory,
                _settings.InfluxDbSettings,
                _settings.HttpPolicy);
            var payloadBuilder = new LineProtocolPayloadBuilder(_settings.DataKeys, _settings.MetricNameFormatter);

            return(new ReportRunner <LineProtocolPayload>(
                       async p =>
            {
                var result = await lineProtocolClient.WriteAsync(p.Payload());
                return result.Success;
            },
                       payloadBuilder,
                       _settings.ReportInterval,
                       name,
                       loggerFactory));
        }