App.Metrics.Reporting.GrafanaCloudHostedMetrics.Facts.DefaultLineProtocolClientTests.Should_back_off_when_reached_max_failures_then_retry_after_backoff_period C# (CSharp) 메소드

Should_back_off_when_reached_max_failures_then_retry_after_backoff_period() 개인적인 메소드

        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 MetricsReportingHostedMetricsOptions
                           {
                               HostedMetrics = new HostedMetricsOptions
                                               {
                                                   BaseUri = new Uri("http://localhost"),
                                                   ApiKey = "123"
                                               },
                               HttpPolicy = new HttpPolicy()
                           };
            var hostedMetricsClient = HostedMetricsReporterBuilder.CreateClient(settings, policy, httpMessageHandlerMock.Object);

            foreach (var attempt in Enumerable.Range(0, 10))
            {
                await hostedMetricsClient.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)));

            hostedMetricsClient = HostedMetricsReporterBuilder.CreateClient(settings, policy, httpMessageHandlerMock.Object);

            var response = await hostedMetricsClient.WriteAsync(Payload, CancellationToken.None);

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