Skip to content

Commit 41db127

Browse files
committed
[#478] Create session factory once per test class
I've also refactored some because they would get stuck on some databases otherwise.
1 parent 60942b4 commit 41db127

8 files changed

+396
-508
lines changed

hibernate-reactive-core/src/test/java/org/hibernate/reactive/BaseReactiveTest.java

Lines changed: 47 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,15 @@
66
package org.hibernate.reactive;
77

88
import io.smallrye.mutiny.Uni;
9+
import io.vertx.core.Promise;
910
import io.vertx.core.Vertx;
1011
import io.vertx.core.VertxOptions;
1112
import io.vertx.ext.unit.Async;
1213
import io.vertx.ext.unit.TestContext;
1314
import io.vertx.ext.unit.junit.RunTestOnContext;
1415
import io.vertx.ext.unit.junit.Timeout;
1516
import io.vertx.ext.unit.junit.VertxUnitRunner;
17+
1618
import org.hibernate.SessionFactory;
1719
import org.hibernate.boot.registry.StandardServiceRegistry;
1820
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
@@ -23,14 +25,16 @@
2325
import org.hibernate.reactive.containers.DatabaseConfiguration.DBType;
2426
import org.hibernate.reactive.mutiny.Mutiny;
2527
import org.hibernate.reactive.pool.ReactiveConnection;
26-
import org.hibernate.reactive.pool.ReactiveConnectionPool;
2728
import org.hibernate.reactive.provider.ReactiveServiceRegistryBuilder;
2829
import org.hibernate.reactive.provider.Settings;
2930
import org.hibernate.reactive.provider.service.ReactiveGenerationTarget;
3031
import org.hibernate.reactive.stage.Stage;
32+
import org.hibernate.reactive.testing.SessionFactoryManager;
3133
import org.hibernate.reactive.vertx.VertxInstance;
3234
import org.hibernate.tool.schema.spi.SchemaManagementTool;
35+
3336
import org.junit.After;
37+
import org.junit.AfterClass;
3438
import org.junit.Before;
3539
import org.junit.ClassRule;
3640
import org.junit.Rule;
@@ -60,6 +64,8 @@
6064
@RunWith(VertxUnitRunner.class)
6165
public abstract class BaseReactiveTest {
6266

67+
public static SessionFactoryManager factoryManager = new SessionFactoryManager();
68+
6369
@Rule
6470
public Timeout rule = Timeout.seconds( 5 * 60 );
6571

@@ -74,8 +80,6 @@ public abstract class BaseReactiveTest {
7480

7581
private AutoCloseable session, statelessSession;
7682
private ReactiveConnection connection;
77-
private org.hibernate.SessionFactory sessionFactory;
78-
private ReactiveConnectionPool poolProvider;
7983

8084
protected static void test(TestContext context, CompletionStage<?> work) {
8185
// this will be added to TestContext in the next vert.x release
@@ -151,31 +155,44 @@ public CompletionStage<Void> deleteEntities(String... entities) {
151155

152156
@Before
153157
public void before(TestContext context) {
158+
Async async = context.async();
159+
vertxContextRule.vertx()
160+
.executeBlocking(
161+
// schema generation is a blocking operation and so it causes an
162+
// exception when run on the Vert.x event loop. So call it using
163+
// Vertx.executeBlocking()
164+
this::startFactoryManager,
165+
event -> {
166+
if ( event.succeeded() ) {
167+
async.complete();
168+
}
169+
else {
170+
context.fail( event.cause() );
171+
}
172+
}
173+
);
174+
}
175+
176+
private void startFactoryManager(Promise<Object> p) {
177+
try {
178+
factoryManager.start( () -> createHibernateSessionFactory() );
179+
p.complete();
180+
}
181+
catch (Throwable e) {
182+
p.fail( e );
183+
}
184+
}
185+
186+
private SessionFactory createHibernateSessionFactory() {
154187
Configuration configuration = constructConfiguration();
155188
StandardServiceRegistryBuilder builder = new ReactiveServiceRegistryBuilder()
156189
.addService( VertxInstance.class, (VertxInstance) () -> vertxContextRule.vertx() )
157190
.applySettings( configuration.getProperties() );
158191
addServices( builder );
159192
StandardServiceRegistry registry = builder.build();
160193
configureServices( registry );
161-
162-
// schema generation is a blocking operation and so it causes an
163-
// exception when run on the Vert.x event loop. So call it using
164-
// Vertx.executeBlocking()
165-
Async async = context.async();
166-
vertxContextRule.vertx().<SessionFactory>executeBlocking(
167-
p -> p.complete( configuration.buildSessionFactory( registry ) ),
168-
r -> {
169-
if ( r.failed() ) {
170-
context.fail( r.cause() );
171-
}
172-
else {
173-
sessionFactory = r.result();
174-
poolProvider = registry.getService( ReactiveConnectionPool.class );
175-
async.complete();
176-
}
177-
}
178-
);
194+
SessionFactory sessionFactory = configuration.buildSessionFactory( registry );
195+
return sessionFactory;
179196
}
180197

181198
protected void addServices(StandardServiceRegistryBuilder builder) {}
@@ -226,14 +243,15 @@ public void after(TestContext context) {
226243
connection = null;
227244
}
228245
}
246+
}
229247

230-
if ( sessionFactory != null ) {
231-
sessionFactory.close();
232-
}
248+
@AfterClass
249+
public static void closeFactory() {
250+
factoryManager.stop();
233251
}
234252

235253
protected Stage.SessionFactory getSessionFactory() {
236-
return sessionFactory.unwrap( Stage.SessionFactory.class );
254+
return factoryManager.getHibernateSessionFactory().unwrap( Stage.SessionFactory.class );
237255
}
238256

239257
/**
@@ -260,7 +278,7 @@ protected Stage.StatelessSession openStatelessSession() {
260278
}
261279

262280
protected CompletionStage<ReactiveConnection> connection() {
263-
return poolProvider.getConnection().thenApply( c -> connection = c );
281+
return factoryManager.getReactiveConnectionPool().getConnection().thenApply( c -> connection = c );
264282
}
265283

266284
/**
@@ -277,10 +295,6 @@ protected Mutiny.Session openMutinySession() {
277295
return newSession;
278296
}
279297

280-
protected Mutiny.SessionFactory getMutinySessionFactory() {
281-
return sessionFactory.unwrap( Mutiny.SessionFactory.class );
282-
}
283-
284298
protected Mutiny.StatelessSession openMutinyStatelessSession() {
285299
if ( statelessSession != null ) {
286300
statelessSession.close();
@@ -290,4 +304,7 @@ protected Mutiny.StatelessSession openMutinyStatelessSession() {
290304
return newSession;
291305
}
292306

307+
protected Mutiny.SessionFactory getMutinySessionFactory() {
308+
return factoryManager.getHibernateSessionFactory().unwrap( Mutiny.SessionFactory.class );
309+
}
293310
}

hibernate-reactive-core/src/test/java/org/hibernate/reactive/CacheTest.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
import org.hibernate.annotations.Cache;
1010
import org.hibernate.cfg.Configuration;
1111
import org.hibernate.cfg.Environment;
12+
13+
import org.junit.After;
1214
import org.junit.Test;
1315

1416
import javax.persistence.Cacheable;
@@ -32,6 +34,11 @@ protected Configuration constructConfiguration() {
3234
return configuration;
3335
}
3436

37+
@After
38+
public void cleanDB(TestContext context) {
39+
getSessionFactory().close();
40+
}
41+
3542
@Test
3643
public void testCacheWithHQL(TestContext context) {
3744
org.hibernate.Cache cache = getSessionFactory().getCache();

0 commit comments

Comments
 (0)