Skip to content

Commit

Permalink
✨ add Health method
Browse files Browse the repository at this point in the history
  • Loading branch information
Odonno committed Sep 19, 2023
1 parent fdef193 commit 36afdad
Show file tree
Hide file tree
Showing 7 changed files with 92 additions and 2 deletions.
48 changes: 48 additions & 0 deletions SurrealDb.Tests/HealthTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
namespace SurrealDb.Tests;

public class HealthTests
{
[Theory]
[InlineData("http://localhost:8000")]
[InlineData("ws://localhost:8000/rpc")]
public async Task ShouldBeTrueOnAValidServer(string url)
{
bool? response = null;

Func<Task> func = async () =>
{
await using var surrealDbClientGenerator = new SurrealDbClientGenerator();
var dbInfo = surrealDbClientGenerator.GenerateDatabaseInfo();
using var client = surrealDbClientGenerator.Create(url);
response = await client.Health();
};

await func.Should().NotThrowAsync();

response.Should().BeTrue();
}

[Theory]
[InlineData("http://localhost:1234")]
[InlineData("ws://localhost:1234/rpc")]
public async Task ShouldBeFalseOnAnInvalidServer(string url)
{
bool? response = null;

Func<Task> func = async () =>
{
await using var surrealDbClientGenerator = new SurrealDbClientGenerator();
var dbInfo = surrealDbClientGenerator.GenerateDatabaseInfo();
using var client = surrealDbClientGenerator.Create(url);
response = await client.Health();
};

await func.Should().NotThrowAsync();

response.Should().BeFalse();
}
}
1 change: 0 additions & 1 deletion SurrealDb.Tests/SetTests.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
using SurrealDb.Internals.Models;
using SurrealDb.Models.Response;
using System.Text;

Expand Down
15 changes: 15 additions & 0 deletions SurrealDb/Internals/SurrealDbEngine.Http.cs
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,21 @@ public void Dispose()
_singleHttpClient.Value.Dispose();
}

public async Task<bool> Health(CancellationToken cancellationToken)
{
try
{
using var wrapper = CreateHttpClientWrapper();

using var response = await wrapper.Instance.GetAsync("/health", cancellationToken);
return response.IsSuccessStatusCode;
}
catch (HttpRequestException)
{
return false;
}
}

public Task Invalidate(CancellationToken _)
{
_config.ResetAuth();
Expand Down
1 change: 1 addition & 0 deletions SurrealDb/Internals/SurrealDbEngine.Interface.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ internal interface ISurrealDbEngine : IDisposable
Task<T> Create<T>(string table, T? data, CancellationToken cancellationToken);
Task Delete(string table, CancellationToken cancellationToken);
Task<bool> Delete(Thing thing, CancellationToken cancellationToken);
Task<bool> Health(CancellationToken cancellationToken);
Task Invalidate(CancellationToken cancellationToken);
Task<TOutput> Merge<TMerge, TOutput>(TMerge data, CancellationToken cancellationToken) where TMerge : Record;
Task<T> Merge<T>(Thing thing, Dictionary<string, object> data, CancellationToken cancellationToken);
Expand Down
17 changes: 16 additions & 1 deletion SurrealDb/Internals/SurrealDbEngine.Ws.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
using SurrealDb.Internals.Auth;
using SurrealDb.Internals.Helpers;
using SurrealDb.Internals.Json;
using SurrealDb.Internals.Models;
using SurrealDb.Internals.Ws;
using SurrealDb.Models;
using SurrealDb.Models.Auth;
Expand Down Expand Up @@ -181,6 +180,22 @@ public void Dispose()
_wsClient.Dispose();
}

public async Task<bool> Health(CancellationToken cancellationToken)
{
if (_wsClient.IsStarted)
return true;

try
{
await _wsClient.StartOrFail();
return true;
}
catch (Exception)
{
return false;
}
}

public async Task Invalidate(CancellationToken cancellationToken)
{
await SendRequest("invalidate", null, cancellationToken);
Expand Down
7 changes: 7 additions & 0 deletions SurrealDb/SurrealDbClient.Interface.cs
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,13 @@ public interface ISurrealDbClient : IDisposable
/// <returns>Returns true if the record was removed successfully.</returns>
Task<bool> Delete(Thing thing, CancellationToken cancellationToken = default);

/// <summary>
/// Checks the status of the database server and storage engine.
/// </summary>
/// <param name="cancellationToken">The cancellationToken enables graceful cancellation of asynchronous operations</param>
/// <returns>Returns true if the database server and storage engine are healthy.</returns>
Task<bool> Health(CancellationToken cancellationToken = default);

/// <summary>
/// Invalidates the authentication for the current connection.
/// </summary>
Expand Down
5 changes: 5 additions & 0 deletions SurrealDb/SurrealDbClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,11 @@ public void Dispose()
_engine.Dispose();
}

public Task<bool> Health(CancellationToken cancellationToken = default)
{
return _engine.Health(cancellationToken);
}

public Task Invalidate(CancellationToken cancellationToken = default)
{
return _engine.Invalidate(cancellationToken);
Expand Down

0 comments on commit 36afdad

Please sign in to comment.