public void CheckHealth_should_use_long_timeout_for_endpoints_with_faulty_statuses(HealthStatus status) { var expectedTime = TimeSpan.FromMilliseconds(300); _settings.Setup(s => s.HealthyResponseTimeLimit).Returns(TimeSpan.FromSeconds(5)); _settings.Setup(s => s.ShortTimeOut).Returns(TimeSpan.FromMilliseconds(30)); _settings.Setup(s => s.FailureTimeOut).Returns(expectedTime); _monitor .Setup(m => m.CheckHealthAsync(_endpoint.Address, It.IsAny <CancellationToken>())) .Returns(async(string address, CancellationToken token) => { if (_endpoint.Health == null) { return(new HealthInfo(status)); } await Task.Delay(TimeSpan.FromSeconds(2), token); return(new HealthInfo(HealthStatus.Healthy)); }); //First state _endpoint.CheckHealth(_sampler, new CancellationToken()).Wait(); Assert.Equal(status.ToString(), _endpoint.Health.Status.ToString()); //Timed out state var result = _sampler.CheckHealth(_endpoint, new CancellationToken()).Result; Assert.Equal(EndpointStatus.Faulty, result.Status); AssertResponseTime(result, expectedTime); AssertCheckTime(result); }
public void GetEndpoint_should_return_endpoint_information_with_details(EndpointStatus status) { Guid id = Guid.NewGuid(); var healthSampler = new Mock <IHealthSampler>(); var endpoint = new Endpoint(id, MonitorMock.GetMock("monitor").Object, "address", "name", "group"); var token = new CancellationToken(); var endpointHealth = new EndpointHealth(DateTime.UtcNow, TimeSpan.FromSeconds(1), status, new Dictionary <string, string> { { "a", "b" }, { "c", "d" } }); healthSampler.Setup(s => s.CheckHealth(endpoint, token)).Returns(Task.FromResult(endpointHealth)); endpoint.CheckHealth(healthSampler.Object, token).Wait(); _endpointRegistry.Setup(r => r.GetById(id)).Returns(endpoint); var result = _controller.GetEndpoint(id) as OkNegotiatedContentResult <EndpointDetails>; Assert.NotNull(result); AssertEndpoint(endpoint, result.Content); Assert.Equal(status, result.Content.Status); Assert.Equal(endpointHealth.CheckTimeUtc, result.Content.LastCheckUtc); Assert.Equal(endpointHealth.ResponseTime, result.Content.LastResponseTime); Assert.Equal(endpointHealth.Details, result.Content.Details); }
public void CheckHealth_should_update_the_endpoint_with_its_health_status() { var endpoint = new Endpoint(Guid.Empty, MonitorMock.Mock("monitor"), "address", "name", "group"); var sampler = new Mock <IHealthSampler>(); var token = new CancellationToken(); var result = new EndpointHealth(DateTime.UtcNow, TimeSpan.Zero, EndpointStatus.Healthy); sampler.Setup(s => s.CheckHealth(endpoint, token)).Returns(Task.FromResult(result)); endpoint.CheckHealth(sampler.Object, token).Wait(); Assert.Same(result, endpoint.Health); }
private async Task <Endpoint> CreateTaskFor(Endpoint endpoint) { await Task.Delay(GetRandomizedDelay()); while (!_cancellation.IsCancellationRequested && !endpoint.IsDisposed) { var delay = Task.Delay(_settings.HealthCheckInterval); await endpoint.CheckHealth(_sampler, _cancellation.Token); await delay; } return(endpoint); }