Skip to content

Commit

Permalink
Add TryVerifyConnectivity (#693)
Browse files Browse the repository at this point in the history
* Add IDriver.TryVerifyConnectivity
  • Loading branch information
thelonelyvulpes committed Mar 20, 2023
1 parent efdc777 commit 1af151a
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 0 deletions.
26 changes: 26 additions & 0 deletions Neo4j.Driver/Neo4j.Driver.Tests/DriverTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,32 @@ public async void ShouldVerifyConnection()
mock.Verify(x => x.VerifyConnectivityAndGetInfoAsync(), Times.Once);
}

[Fact]
public async void ShouldTryVerifyConnection()
{
var mock = new Mock<IConnectionProvider>();
mock.Setup(x => x.VerifyConnectivityAndGetInfoAsync())
.Returns(Task.FromResult(new Mock<IServerInfo>().Object));

var driver = (IDriver)new Internal.Driver(new Uri("bolt://localhost"), false, mock.Object, null);
var connects = await driver.TryVerifyConnectivityAsync();

connects.Should().BeTrue();
}

[Fact]
public async void ShouldCatchInTryVerifyConnection()
{
var mock = new Mock<IConnectionProvider>();
mock.Setup(x => x.VerifyConnectivityAndGetInfoAsync())
.ThrowsAsync(new Exception("broken"));

var driver = (IDriver)new Internal.Driver(new Uri("bolt://localhost"), false, mock.Object, null);
var connects = await driver.TryVerifyConnectivityAsync();

connects.Should().BeFalse();
}

[Fact]
public async void ShouldGetInfoConnection()
{
Expand Down
9 changes: 9 additions & 0 deletions Neo4j.Driver/Neo4j.Driver/IDriver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,15 @@ public interface IDriver : IDisposable, IAsyncDisposable
/// <returns>A task that represents the asynchronous operation. The task result contains the connected server's info.</returns>
Task<IServerInfo> GetServerInfoAsync();

/// <summary>
/// Asynchronously verify if the driver can connect to the remote server.
/// </summary>
/// <remarks>Even if this method returns false, the driver still need to be closed via
/// <see cref="CloseAsync"/> or disposed to free up all resources.</remarks>
/// <returns>A task that represents the asynchronous operation.<br/>
/// The task result contains if the driver successfully connected to the remote server.</returns>
Task<bool> TryVerifyConnectivityAsync();

/// <summary>
/// Asynchronously verify if the driver can connect to the remote server by establishing a network connection with
/// the remote. If the driver fails to connect to the remote server, an error will be thrown, which can be used to further
Expand Down
14 changes: 14 additions & 0 deletions Neo4j.Driver/Neo4j.Driver/Internal/Driver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,20 @@ public Task<IServerInfo> GetServerInfoAsync()
return _connectionProvider.VerifyConnectivityAndGetInfoAsync();
}

public async Task<bool> TryVerifyConnectivityAsync()
{
try
{
await _connectionProvider.VerifyConnectivityAndGetInfoAsync().ConfigureAwait(false);
return true;
}
catch (Exception)
{
return false;
}
}


public Task VerifyConnectivityAsync()
{
return GetServerInfoAsync();
Expand Down

0 comments on commit 1af151a

Please sign in to comment.