diff --git a/Neo4j.Driver/Neo4j.Driver.Tests/DriverTests.cs b/Neo4j.Driver/Neo4j.Driver.Tests/DriverTests.cs index 2adf74fe3..7edf9d20e 100644 --- a/Neo4j.Driver/Neo4j.Driver.Tests/DriverTests.cs +++ b/Neo4j.Driver/Neo4j.Driver.Tests/DriverTests.cs @@ -149,6 +149,32 @@ public async void ShouldVerifyConnection() mock.Verify(x => x.VerifyConnectivityAndGetInfoAsync(), Times.Once); } + [Fact] + public async void ShouldTryVerifyConnection() + { + var mock = new Mock(); + mock.Setup(x => x.VerifyConnectivityAndGetInfoAsync()) + .Returns(Task.FromResult(new Mock().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(); + 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() { diff --git a/Neo4j.Driver/Neo4j.Driver/IDriver.cs b/Neo4j.Driver/Neo4j.Driver/IDriver.cs index 23226113e..9c4ad1c60 100644 --- a/Neo4j.Driver/Neo4j.Driver/IDriver.cs +++ b/Neo4j.Driver/Neo4j.Driver/IDriver.cs @@ -66,6 +66,15 @@ public interface IDriver : IDisposable, IAsyncDisposable /// A task that represents the asynchronous operation. The task result contains the connected server's info. Task GetServerInfoAsync(); + /// + /// Asynchronously verify if the driver can connect to the remote server. + /// + /// Even if this method returns false, the driver still need to be closed via + /// or disposed to free up all resources. + /// A task that represents the asynchronous operation.
+ /// The task result contains if the driver successfully connected to the remote server.
+ Task TryVerifyConnectivityAsync(); + /// /// 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 diff --git a/Neo4j.Driver/Neo4j.Driver/Internal/Driver.cs b/Neo4j.Driver/Neo4j.Driver/Internal/Driver.cs index 740e83d1b..cc6e14738 100644 --- a/Neo4j.Driver/Neo4j.Driver/Internal/Driver.cs +++ b/Neo4j.Driver/Neo4j.Driver/Internal/Driver.cs @@ -109,6 +109,20 @@ public Task GetServerInfoAsync() return _connectionProvider.VerifyConnectivityAndGetInfoAsync(); } + public async Task TryVerifyConnectivityAsync() + { + try + { + await _connectionProvider.VerifyConnectivityAndGetInfoAsync().ConfigureAwait(false); + return true; + } + catch (Exception) + { + return false; + } + } + + public Task VerifyConnectivityAsync() { return GetServerInfoAsync();