示例#1
0
        public async Task Can_write_payload_successfully_with_creds()
        {
            // Arrange
            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 settings = new InfluxDb2Options
            {
                BaseUri = new Uri("http://localhost"),
                Token   = "token"
            };

            var policy       = new HttpPolicy();
            var influxClient = MetricsInfluxDb2ReporterBuilder.CreateClient(settings, policy, httpMessageHandlerMock.Object);

            // Act
            LineProtocolWriteResult response;

            using (var payload = new MemoryStream(Encoding.UTF8.GetBytes(Payload)))
            {
                response = await influxClient.WriteAsync(payload, CancellationToken.None);
            }

            // Assert
            response.Success.Should().BeTrue();
        }
示例#2
0
        internal static InfluxDb2ProtocolClient CreateClient(
            InfluxDb2Options influxDb2Options,
            HttpPolicy httpPolicy,
            HttpMessageHandler httpMessageHandler = null)
        {
            var httpClient = httpMessageHandler == null
                ? new HttpClient()
                : new HttpClient(httpMessageHandler);

            httpClient.BaseAddress = influxDb2Options.BaseUri;
            httpClient.Timeout     = httpPolicy.Timeout;

            if (string.IsNullOrWhiteSpace(influxDb2Options.Token))
            {
                return(new InfluxDb2ProtocolClient(
                           influxDb2Options,
                           httpPolicy,
                           httpClient));
            }

            httpClient.BaseAddress = influxDb2Options.BaseUri;
            httpClient.Timeout     = httpPolicy.Timeout;
            if (!string.IsNullOrWhiteSpace(influxDb2Options.Token))
            {
                httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Token", influxDb2Options.Token);
            }

            return(new InfluxDb2ProtocolClient(
                       influxDb2Options,
                       httpPolicy,
                       httpClient));
        }
 public InfluxDb2ProtocolClient(
     InfluxDb2Options influxDbOptions,
     HttpPolicy httpPolicy,
     HttpClient httpClient)
 {
     _influxDbOptions       = influxDbOptions ?? throw new ArgumentNullException(nameof(influxDbOptions));
     _httpClient            = httpClient;
     _backOffPeriod         = httpPolicy?.BackoffPeriod ?? throw new ArgumentNullException(nameof(httpPolicy));
     _failuresBeforeBackoff = httpPolicy.FailuresBeforeBackoff;
     _failureAttempts       = 0;
 }
        public void Can_generate_influx_write_endpoint()
        {
            var settings = new InfluxDb2Options
            {
                Organization = "testorg",
                Bucket       = "testbucket",
                BaseUri      = new Uri("http://localhost"),
                Precision    = "s"
            };

            settings.Endpoint.Should().Be("api/v2/write?org=testorg&bucket=testbucket&precision=s");
        }
示例#5
0
        public async Task Should_back_off_when_reached_max_failures()
        {
            // Arrange
            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.FromMinutes(1)
            };
            var settings = new InfluxDb2Options
            {
                BaseUri      = new Uri("http://localhost"),
                Organization = "influx",
                Bucket       = "bucket",
            };
            var influxClient = MetricsInfluxDb2ReporterBuilder.CreateClient(settings, policy, httpMessageHandlerMock.Object);

            // Act
            foreach (var attempt in Enumerable.Range(0, 10))
            {
                using (var payload = new MemoryStream(Encoding.UTF8.GetBytes(Payload)))
                {
                    await influxClient.WriteAsync(payload, CancellationToken.None);
                }

                // ReSharper disable ConvertIfStatementToConditionalTernaryExpression
                if (attempt <= policy.FailuresBeforeBackoff)
                {
                    // ReSharper restore ConvertIfStatementToConditionalTernaryExpression
                    // Assert
                    httpMessageHandlerMock.Protected().Verify <Task <HttpResponseMessage> >(
                        "SendAsync",
                        Times.AtLeastOnce(),
                        ItExpr.IsAny <HttpRequestMessage>(),
                        ItExpr.IsAny <CancellationToken>());
                }
                else
                {
                    // Assert
                    httpMessageHandlerMock.Protected().Verify <Task <HttpResponseMessage> >(
                        "SendAsync",
                        Times.AtMost(6), // TODO: Starting failing when running all tests with 2.0.0 upgrade, should be 3
                        ItExpr.IsAny <HttpRequestMessage>(),
                        ItExpr.IsAny <CancellationToken>());
                }
            }
        }
示例#6
0
        public void Http_policy_is_required()
        {
            // Arrange
            Action action = () =>
            {
                var settings = new InfluxDb2Options
                {
                    BaseUri      = new Uri("http://localhost"),
                    Organization = "influx",
                    Bucket       = "bucket"
                };

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

            // Assert
            action.Should().Throw <ArgumentNullException>();
        }
示例#7
0
        public async Task Should_reset_failure_attempts_in_case_of_success_request()
        {
            // Arrange
            var httpMessageHandlerMock = new Mock <HttpMessageHandler>();
            int callCount = 0;

            httpMessageHandlerMock.Protected().Setup <Task <HttpResponseMessage> >(
                "SendAsync",
                ItExpr.IsAny <HttpRequestMessage>(),
                ItExpr.IsAny <CancellationToken>()).
            ReturnsAsync(
                () => ++ callCount % 2 == 0
                                           ? new HttpResponseMessage(HttpStatusCode.BadRequest)
                                           : new HttpResponseMessage(HttpStatusCode.OK));
            var policy = new HttpPolicy {
                FailuresBeforeBackoff = 3, BackoffPeriod = TimeSpan.FromMinutes(1)
            };
            var settings = new InfluxDb2Options
            {
                BaseUri      = new Uri("http://localhost"),
                Organization = "influx",
                Bucket       = "bucket",
            };
            var influxClient = MetricsInfluxDb2ReporterBuilder.CreateClient(settings, policy, httpMessageHandlerMock.Object);

            // Act
            foreach (var attempt in Enumerable.Range(0, 10))
            {
                using (var payload = new MemoryStream(Encoding.UTF8.GetBytes(Payload)))
                {
                    await influxClient.WriteAsync(payload, CancellationToken.None);
                }

                httpMessageHandlerMock.Protected().Verify <Task <HttpResponseMessage> >(
                    "SendAsync",
                    Times.Exactly(attempt + 1),
                    ItExpr.IsAny <HttpRequestMessage>(),
                    ItExpr.IsAny <CancellationToken>());
            }
        }
示例#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 settings = new InfluxDb2Options
            {
                BaseUri      = new Uri("http://localhost"),
                Organization = "influx",
                Bucket       = "bucket",
            };
            var influxClient = MetricsInfluxDb2ReporterBuilder.CreateClient(settings, policy, httpMessageHandlerMock.Object);

            foreach (var attempt in Enumerable.Range(0, 10))
            {
                using (var payload = new MemoryStream(Encoding.UTF8.GetBytes(Payload)))
                {
                    await influxClient.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)));

            influxClient = MetricsInfluxDb2ReporterBuilder.CreateClient(settings, policy, httpMessageHandlerMock.Object);

            LineProtocolWriteResult response;

            using (var payload = new MemoryStream(Encoding.UTF8.GetBytes(Payload)))
            {
                response = await influxClient.WriteAsync(payload, CancellationToken.None);
            }

            response.Success.Should().BeTrue();
        }