|
30 | 30 | import java.util.Arrays;
|
31 | 31 | import java.util.List;
|
32 | 32 |
|
| 33 | +import org.neo4j.driver.internal.async.BootstrapFactory; |
33 | 34 | import org.neo4j.driver.internal.cluster.RoutingSettings;
|
34 | 35 | import org.neo4j.driver.internal.cluster.loadbalancing.LoadBalancer;
|
35 | 36 | import org.neo4j.driver.internal.retry.RetryLogic;
|
|
38 | 39 | import org.neo4j.driver.internal.spi.Connection;
|
39 | 40 | import org.neo4j.driver.internal.spi.ConnectionPool;
|
40 | 41 | import org.neo4j.driver.internal.spi.ConnectionProvider;
|
41 |
| -import org.neo4j.driver.internal.util.Futures; |
42 | 42 | import org.neo4j.driver.v1.AuthToken;
|
43 | 43 | import org.neo4j.driver.v1.AuthTokens;
|
44 | 44 | import org.neo4j.driver.v1.Config;
|
45 | 45 | import org.neo4j.driver.v1.Driver;
|
| 46 | +import org.neo4j.driver.v1.exceptions.ServiceUnavailableException; |
46 | 47 |
|
47 | 48 | import static java.util.concurrent.CompletableFuture.completedFuture;
|
48 | 49 | import static org.hamcrest.Matchers.instanceOf;
|
49 | 50 | import static org.junit.Assert.assertArrayEquals;
|
| 51 | +import static org.junit.Assert.assertEquals; |
| 52 | +import static org.junit.Assert.assertNotNull; |
50 | 53 | import static org.junit.Assert.assertThat;
|
51 | 54 | import static org.junit.Assert.fail;
|
52 | 55 | import static org.mockito.Mockito.any;
|
53 | 56 | import static org.mockito.Mockito.mock;
|
54 | 57 | import static org.mockito.Mockito.verify;
|
55 | 58 | import static org.mockito.Mockito.when;
|
| 59 | +import static org.neo4j.driver.internal.util.Futures.failedFuture; |
56 | 60 | import static org.neo4j.driver.v1.AccessMode.READ;
|
57 | 61 | import static org.neo4j.driver.v1.Config.defaultConfig;
|
58 | 62 |
|
@@ -94,7 +98,7 @@ public void connectionPoolCloseExceptionIsSuppressedWhenDriverCreationFails() th
|
94 | 98 | {
|
95 | 99 | ConnectionPool connectionPool = connectionPoolMock();
|
96 | 100 | RuntimeException poolCloseError = new RuntimeException( "Pool close error" );
|
97 |
| - when( connectionPool.close() ).thenReturn( Futures.failedFuture( poolCloseError ) ); |
| 101 | + when( connectionPool.close() ).thenReturn( failedFuture( poolCloseError ) ); |
98 | 102 |
|
99 | 103 | DriverFactory factory = new ThrowingDriverFactory( connectionPool );
|
100 | 104 |
|
@@ -135,6 +139,41 @@ public void usesLeakLoggingSessionFactoryWhenConfigured()
|
135 | 139 | assertThat( capturedFactory.newInstance( READ, null ), instanceOf( LeakLoggingNetworkSession.class ) );
|
136 | 140 | }
|
137 | 141 |
|
| 142 | + @Test |
| 143 | + public void shouldVerifyConnectivity() |
| 144 | + { |
| 145 | + SessionFactory sessionFactory = mock( SessionFactory.class ); |
| 146 | + when( sessionFactory.verifyConnectivity() ).thenReturn( completedFuture( null ) ); |
| 147 | + when( sessionFactory.close() ).thenReturn( completedFuture( null ) ); |
| 148 | + DriverFactoryWithSessions driverFactory = new DriverFactoryWithSessions( sessionFactory ); |
| 149 | + |
| 150 | + try ( Driver driver = createDriver( driverFactory ) ) |
| 151 | + { |
| 152 | + assertNotNull( driver ); |
| 153 | + verify( sessionFactory ).verifyConnectivity(); |
| 154 | + } |
| 155 | + } |
| 156 | + |
| 157 | + @Test |
| 158 | + public void shouldThrowWhenUnableToVerifyConnectivity() |
| 159 | + { |
| 160 | + SessionFactory sessionFactory = mock( SessionFactory.class ); |
| 161 | + ServiceUnavailableException error = new ServiceUnavailableException( "Hello" ); |
| 162 | + when( sessionFactory.verifyConnectivity() ).thenReturn( failedFuture( error ) ); |
| 163 | + when( sessionFactory.close() ).thenReturn( completedFuture( null ) ); |
| 164 | + DriverFactoryWithSessions driverFactory = new DriverFactoryWithSessions( sessionFactory ); |
| 165 | + |
| 166 | + try |
| 167 | + { |
| 168 | + createDriver( driverFactory ); |
| 169 | + fail( "Exception expected" ); |
| 170 | + } |
| 171 | + catch ( ServiceUnavailableException e ) |
| 172 | + { |
| 173 | + assertEquals( "Hello", e.getMessage() ); |
| 174 | + } |
| 175 | + } |
| 176 | + |
138 | 177 | private Driver createDriver( DriverFactory driverFactory )
|
139 | 178 | {
|
140 | 179 | return createDriver( driverFactory, defaultConfig() );
|
@@ -222,4 +261,34 @@ protected ConnectionPool createConnectionPool( AuthToken authToken, SecurityPlan
|
222 | 261 | return connectionPoolMock();
|
223 | 262 | }
|
224 | 263 | }
|
| 264 | + |
| 265 | + private static class DriverFactoryWithSessions extends DriverFactory |
| 266 | + { |
| 267 | + final SessionFactory sessionFactory; |
| 268 | + |
| 269 | + DriverFactoryWithSessions( SessionFactory sessionFactory ) |
| 270 | + { |
| 271 | + this.sessionFactory = sessionFactory; |
| 272 | + } |
| 273 | + |
| 274 | + @Override |
| 275 | + protected Bootstrap createBootstrap() |
| 276 | + { |
| 277 | + return BootstrapFactory.newBootstrap( 1 ); |
| 278 | + } |
| 279 | + |
| 280 | + @Override |
| 281 | + protected ConnectionPool createConnectionPool( AuthToken authToken, SecurityPlan securityPlan, |
| 282 | + Bootstrap bootstrap, Config config ) |
| 283 | + { |
| 284 | + return connectionPoolMock(); |
| 285 | + } |
| 286 | + |
| 287 | + @Override |
| 288 | + protected SessionFactory createSessionFactory( ConnectionProvider connectionProvider, RetryLogic retryLogic, |
| 289 | + Config config ) |
| 290 | + { |
| 291 | + return sessionFactory; |
| 292 | + } |
| 293 | + } |
225 | 294 | }
|
0 commit comments