Skip to content

Commit d00dc3e

Browse files
michael-simonsmp911de
authored andcommitted
Retain driver EventLoop during tests.
We now keep an instance to the event loop to avoid recurring recreation of threads. Closes #2838
1 parent bfbfc05 commit d00dc3e

File tree

1 file changed

+23
-4
lines changed

1 file changed

+23
-4
lines changed

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

+23-4
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@
1515
*/
1616
package org.springframework.data.neo4j.test;
1717

18+
import io.netty.channel.EventLoopGroup;
19+
import io.netty.channel.nio.NioEventLoopGroup;
20+
import io.netty.util.concurrent.DefaultThreadFactory;
1821
import org.apache.commons.logging.Log;
1922
import org.junit.jupiter.api.extension.BeforeAllCallback;
2023
import org.junit.jupiter.api.extension.BeforeEachCallback;
@@ -23,21 +26,25 @@
2326
import org.junit.platform.commons.support.ReflectionSupport;
2427
import org.neo4j.driver.AccessMode;
2528
import org.neo4j.driver.AuthToken;
29+
import org.neo4j.driver.AuthTokenManagers;
2630
import org.neo4j.driver.AuthTokens;
2731
import org.neo4j.driver.Config;
2832
import org.neo4j.driver.Driver;
29-
import org.neo4j.driver.GraphDatabase;
3033
import org.neo4j.driver.Logging;
3134
import org.neo4j.driver.Record;
3235
import org.neo4j.driver.Session;
3336
import org.neo4j.driver.SessionConfig;
37+
import org.neo4j.driver.internal.DriverFactory;
38+
import org.neo4j.driver.internal.SecuritySettings;
39+
import org.neo4j.driver.internal.security.SecurityPlans;
3440
import org.springframework.core.log.LogMessage;
3541
import org.springframework.lang.Nullable;
3642
import org.testcontainers.containers.Neo4jContainer;
3743
import org.testcontainers.utility.TestcontainersConfiguration;
3844

3945
import java.lang.reflect.Field;
4046
import java.lang.reflect.Modifier;
47+
import java.net.URI;
4148
import java.util.List;
4249
import java.util.Locale;
4350
import java.util.Map;
@@ -82,12 +89,14 @@ public class Neo4jExtension implements BeforeAllCallback, BeforeEachCallback {
8289
private static final String SYS_PROPERTY_FORCE_CONTAINER_REUSE = "SDN_FORCE_REUSE_OF_CONTAINERS";
8390
private static final Log log = org.apache.commons.logging.LogFactory.getLog(Neo4jExtension.class);
8491

85-
private static Set<String> COMMUNITY_EDITION_INDICATOR = Set.of("community");
92+
private static final Set<String> COMMUNITY_EDITION_INDICATOR = Set.of("community");
93+
private static final Set<String> COMMERCIAL_EDITION_INDICATOR = Set.of("commercial", "enterprise");
8694

87-
private static Set<String> COMMERCIAL_EDITION_INDICATOR = Set.of("commercial", "enterprise");
95+
private static final EventLoopGroup EVENT_LOOP_GROUP = new NioEventLoopGroup(new DefaultThreadFactory(Neo4jExtension.class, true));
8896

8997
@Override
9098
public void beforeAll(ExtensionContext context) throws Exception {
99+
91100
List<Field> injectableFields = ReflectionSupport.findFields(context.getRequiredTestClass(),
92101
field -> Modifier.isStatic(field.getModifiers()) && field.getType() == Neo4jConnectionSupport.class,
93102
HierarchyTraversalMode.BOTTOM_UP);
@@ -163,6 +172,8 @@ private void checkRequiredFeatures(Neo4jConnectionSupport neo4jConnectionSupport
163172
*/
164173
public static final class Neo4jConnectionSupport implements ExtensionContext.Store.CloseableResource {
165174

175+
private final DriverFactory driverFactory;
176+
166177
public final String url;
167178

168179
public final AuthToken authToken;
@@ -182,6 +193,7 @@ public Neo4jConnectionSupport(String url, AuthToken authToken) {
182193
this.config = Config.builder().withLogging(Logging.slf4j())
183194
.withMaxConnectionPoolSize(Runtime.getRuntime().availableProcessors())
184195
.build();
196+
this.driverFactory = new DriverFactory();
185197
}
186198

187199
/**
@@ -198,14 +210,21 @@ public Driver getDriver() {
198210
synchronized (this) {
199211
driver = this.driverInstance;
200212
if (!isUsable(driver)) {
201-
this.driverInstance = GraphDatabase.driver(url, authToken, config);
213+
this.driverInstance = createDriverInstance();
202214
driver = this.driverInstance;
203215
}
204216
}
205217
}
206218
return driver;
207219
}
208220

221+
private Driver createDriverInstance() {
222+
var uri = URI.create(url);
223+
var settings = new SecuritySettings(config.encrypted(), config.trustStrategy());
224+
var securityPlan = SecurityPlans.createSecurityPlan(settings, uri.getScheme());
225+
return this.driverFactory.newInstance(uri, AuthTokenManagers.basic(() -> authToken), config, securityPlan, EVENT_LOOP_GROUP, null);
226+
}
227+
209228
/**
210229
* A driver is usable if it's not null and can verify its connectivity. This method force closes
211230
* the bean if the connectivity cannot be verified to avoid having a netty pool dangling around.

0 commit comments

Comments
 (0)