Skip to content

Commit db703ae

Browse files
DATAGRAPH-1405 - Adapt to changed test behavior in Spring Framework 5.3 RC2.
1 parent 9ff5703 commit db703ae

File tree

1 file changed

+35
-6
lines changed

1 file changed

+35
-6
lines changed

src/test/java/org/springframework/data/neo4j/test/Neo4jExtension.java

Lines changed: 35 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
import org.neo4j.driver.SessionConfig;
4646
import org.neo4j.driver.internal.util.ServerVersion;
4747
import org.springframework.core.log.LogMessage;
48+
import org.springframework.lang.Nullable;
4849
import org.testcontainers.containers.Neo4jContainer;
4950
import org.testcontainers.utility.TestcontainersConfiguration;
5051

@@ -118,7 +119,7 @@ public void beforeAll(ExtensionContext context) throws Exception {
118119
}
119120

120121
@Override
121-
public void beforeEach(ExtensionContext context) throws Exception {
122+
public void beforeEach(ExtensionContext context) {
122123
ExtensionContext.Store contextStore = context.getStore(NAMESPACE);
123124
Neo4jConnectionSupport neo4jConnectionSupport = contextStore.get(KEY_DRIVER_INSTANCE, Neo4jConnectionSupport.class);
124125
checkRequiredFeatures(neo4jConnectionSupport, context.getTags());
@@ -173,16 +174,19 @@ public Neo4jConnectionSupport(String url, AuthToken authToken) {
173174
}
174175

175176
/**
176-
* @return A possible shared driver instance, connected to either a database running inside test containers or
177-
* running locally.
177+
* This method asserts that the current driver instance is usable before handing it out. If it isn't usable, it
178+
* creates a new one.
179+
*
180+
* @return A shared driver instance, connected to either a database running inside test containers or
181+
* running locally.
178182
*/
179183
public Driver getDriver() {
180184

181185
Driver driver = this.driverInstance;
182-
if (driver == null) {
186+
if (!isUsable(driver)) {
183187
synchronized (this) {
184188
driver = this.driverInstance;
185-
if (driver == null) {
189+
if (!isUsable(driver)) {
186190
this.driverInstance = GraphDatabase.driver(url, authToken, config);
187191
driver = this.driverInstance;
188192
}
@@ -191,6 +195,30 @@ public Driver getDriver() {
191195
return driver;
192196
}
193197

198+
/**
199+
* A driver is usable if it's not null and can verify its connectivity. This method force closes
200+
* the bean if the connectivity cannot be verified to avoid having a netty pool dangling around.
201+
*
202+
* @param driver The driver that should be checked for usability
203+
* @return true if the driver is currently usable.
204+
*/
205+
private static boolean isUsable(@Nullable Driver driver) {
206+
207+
if (driver == null) {
208+
return false;
209+
}
210+
try {
211+
driver.verifyConnectivity();
212+
return true;
213+
} catch (Exception ex) {
214+
try {
215+
driver.close();
216+
} catch (Exception nested) {
217+
}
218+
return false;
219+
}
220+
}
221+
194222
ServerVersion getServerVersion() {
195223

196224
ServerVersion serverVersion = this.cachedServerVersion;
@@ -238,7 +266,8 @@ public void close() {
238266
try {
239267
log.debug("Closing Neo4j connection support.");
240268
driverInstance.close();
241-
} catch (Exception e) {}
269+
} catch (Exception e) {
270+
}
242271
}
243272
}
244273

0 commit comments

Comments
 (0)