Rename to CheckHealthAsync. Add http expection wrap and return false

This commit is contained in:
Dmytro Hridin 2021-05-07 00:28:27 +03:00 committed by Ryan Nowak
parent f428174252
commit 2ddb4bb7e0
4 changed files with 30 additions and 14 deletions

View File

@ -263,11 +263,12 @@ namespace Dapr.Client
public abstract HttpRequestMessage CreateInvokeMethodRequest<TRequest>(HttpMethod httpMethod, string appId, string methodName, TRequest data);
/// <summary>
/// Perform health-check of Dapr sidecar. Return 'true' if sidecar is healthy. Otherwise 'false'.
/// Perform health-check of Dapr sidecar. Return 'true' if sidecar is healthy. Otherwise 'false'.
/// CheckHealthAsync handle <see cref="HttpRequestException"/> and will return 'false' if error will occur on transport level
/// </summary>
/// <param name="cancellationToken">A <see cref="CancellationToken" /> that can be used to cancel the operation.</param>
/// <returns>A <see cref="Task{T}" /> that will return the value when the operation has completed.</returns>
public abstract Task<bool> InvokeHealthMethodAsync(CancellationToken cancellationToken = default);
public abstract Task<bool> CheckHealthAsync(CancellationToken cancellationToken = default);
/// <summary>
/// Perform service invocation using the request provided by <paramref name="request" />. The response will

View File

@ -282,12 +282,19 @@ namespace Dapr.Client
return request;
}
public override async Task<bool> InvokeHealthMethodAsync(CancellationToken cancellationToken = default)
public override async Task<bool> CheckHealthAsync(CancellationToken cancellationToken = default)
{
var path = "/v1.0/healthz";
var request = new HttpRequestMessage(HttpMethod.Get, new Uri(this.httpEndpoint, path));
var response = await InvokeMethodWithResponseAsync(request, cancellationToken);
return response.IsSuccessStatusCode;
try
{
var response = await this.httpClient.SendAsync(request, cancellationToken);
return response.IsSuccessStatusCode;
}
catch (HttpRequestException)
{
return false;
}
}
public override async Task<HttpResponseMessage> InvokeMethodWithResponseAsync(HttpRequestMessage request, CancellationToken cancellationToken = default)

View File

@ -244,7 +244,7 @@ namespace Dapr.Client.Test
}
[Fact]
public async Task InvokeHealthMethodAsync_Success()
public async Task CheckHealthAsync_Success()
{
await using var client = TestClient.CreateForDaprClient(c =>
{
@ -252,7 +252,7 @@ namespace Dapr.Client.Test
});
var request = await client.CaptureHttpRequestAsync<bool>(async daprClient =>
await daprClient.InvokeHealthMethodAsync());
await daprClient.CheckHealthAsync());
// Get Request and validate
Assert.Equal(request.Request.Method, HttpMethod.Get);
@ -263,7 +263,7 @@ namespace Dapr.Client.Test
}
[Fact]
public async Task InvokeHealthMethodAsync_NotSuccess()
public async Task CheckHealthAsync_NotSuccess()
{
await using var client = TestClient.CreateForDaprClient(c =>
{
@ -271,7 +271,7 @@ namespace Dapr.Client.Test
});
var request = await client.CaptureHttpRequestAsync<bool>(async daprClient =>
await daprClient.InvokeHealthMethodAsync());
await daprClient.CheckHealthAsync());
// Get Request and validate
Assert.Equal(request.Request.Method, HttpMethod.Get);
@ -282,7 +282,7 @@ namespace Dapr.Client.Test
}
[Fact]
public async Task InvokeHealthMethodAsync_WrapsHttpRequestException()
public async Task CheckHealthAsync_WrapsHttpRequestException()
{
await using var client = TestClient.CreateForDaprClient(c =>
{
@ -290,12 +290,14 @@ namespace Dapr.Client.Test
});
var request = await client.CaptureHttpRequestAsync<bool>(async daprClient =>
await daprClient.InvokeHealthMethodAsync());
await daprClient.CheckHealthAsync());
Assert.Equal(request.Request.Method, HttpMethod.Get);
Assert.Equal(new Uri("https://test-endpoint:3501/v1.0/healthz").AbsoluteUri, request.Request.RequestUri.AbsoluteUri);
var exception = new HttpRequestException();
var thrown = await Assert.ThrowsAsync<InvocationException>(async () => await request.CompleteWithExceptionAsync(exception));
Assert.Same(exception, thrown.InnerException);
Assert.Null(thrown.Response);
var result = await request.CompleteWithExceptionAndResultAsync(exception);
Assert.False(result);
}
[Fact]

View File

@ -158,6 +158,12 @@ namespace Dapr
this.Capture.Response.SetException(ex);
await WithTimeout(this.Task, TimeSpan.FromSeconds(10), "Waiting for response to be completed timed out.");
}
public async Task<T> CompleteWithExceptionAndResultAsync(Exception ex)
{
this.Capture.Response.SetException(ex);
return await WithTimeout(this.Task, TimeSpan.FromSeconds(10), "Waiting for response to be completed timed out.");
}
public void Dismiss()
{