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 InfluxDbOptions { BaseUri = new Uri("http://localhost"), Database = "influx", UserName = "******", Password = "******" }; var policy = new HttpPolicy(); var influxClient = MetricsInfluxDbReporterBuilder.CreateClient(settings, policy, httpMessageHandlerMock.Object); // Act var response = await influxClient.WriteAsync(Payload, CancellationToken.None); // Assert response.Success.Should().BeTrue(); }
public async Task Can_create_database_with_retention_policy() { // Arrange var httpMessageHandlerMock = new Mock <HttpMessageHandler>(); httpMessageHandlerMock.Protected().Setup <Task <HttpResponseMessage> >( "SendAsync", ItExpr.IsAny <HttpRequestMessage>(), ItExpr.IsAny <CancellationToken>()).Returns(Task.FromResult(new HttpResponseMessage(HttpStatusCode.NotFound))); var settings = new InfluxDbOptions { BaseUri = new Uri("http://localhost"), Database = "influx", CreateDataBaseIfNotExists = true, CreateDatabaseRetentionPolicy = new RetentionPolicyOptions { Duration = TimeSpan.FromMinutes(70) } }; var policy = new HttpPolicy(); var influxClient = MetricsInfluxDbReporterBuilder.CreateClient(settings, policy, httpMessageHandlerMock.Object); // Act await influxClient.WriteAsync(Payload, CancellationToken.None); httpMessageHandlerMock.Protected().Verify <Task <HttpResponseMessage> >( "SendAsync", Times.Exactly(1), ItExpr.Is <HttpRequestMessage>(message => message.RequestUri.ToString().EndsWith("CREATE DATABASE \"influx\" WITH DURATION 70m")), ItExpr.IsAny <CancellationToken>()); }
public async Task Can_write_payload_successfully() { // 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 InfluxDbOptions { BaseUri = new Uri("http://localhost"), Database = "influx" }; var policy = new HttpPolicy(); var influxClient = MetricsInfluxDbReporterBuilder.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(); }
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 InfluxDbOptions { BaseUri = new Uri("http://localhost"), Database = "influx" }; var influxClient = MetricsInfluxDbReporterBuilder.CreateClient(settings, policy, httpMessageHandlerMock.Object); foreach (var attempt in Enumerable.Range(0, 10)) { 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 = MetricsInfluxDbReporterBuilder.CreateClient(settings, policy, httpMessageHandlerMock.Object); var response = await influxClient.WriteAsync(Payload, CancellationToken.None); response.Success.Should().BeTrue(); }
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 InfluxDbOptions { BaseUri = new Uri("http://localhost"), Database = "influx" }; var influxClient = MetricsInfluxDbReporterBuilder.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>()); } } }
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 InfluxDbOptions { BaseUri = new Uri("http://localhost"), Database = "influx" }; var influxClient = MetricsInfluxDbReporterBuilder.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>()); } }