6
6
package org .hibernate .reactive ;
7
7
8
8
import io .smallrye .mutiny .Uni ;
9
+ import io .vertx .core .Promise ;
9
10
import io .vertx .core .Vertx ;
10
11
import io .vertx .core .VertxOptions ;
11
12
import io .vertx .ext .unit .Async ;
12
13
import io .vertx .ext .unit .TestContext ;
13
14
import io .vertx .ext .unit .junit .RunTestOnContext ;
14
15
import io .vertx .ext .unit .junit .Timeout ;
15
16
import io .vertx .ext .unit .junit .VertxUnitRunner ;
17
+
16
18
import org .hibernate .SessionFactory ;
17
19
import org .hibernate .boot .registry .StandardServiceRegistry ;
18
20
import org .hibernate .boot .registry .StandardServiceRegistryBuilder ;
23
25
import org .hibernate .reactive .containers .DatabaseConfiguration .DBType ;
24
26
import org .hibernate .reactive .mutiny .Mutiny ;
25
27
import org .hibernate .reactive .pool .ReactiveConnection ;
26
- import org .hibernate .reactive .pool .ReactiveConnectionPool ;
27
28
import org .hibernate .reactive .provider .ReactiveServiceRegistryBuilder ;
28
29
import org .hibernate .reactive .provider .Settings ;
29
30
import org .hibernate .reactive .provider .service .ReactiveGenerationTarget ;
30
31
import org .hibernate .reactive .stage .Stage ;
32
+ import org .hibernate .reactive .testing .SessionFactoryManager ;
31
33
import org .hibernate .reactive .vertx .VertxInstance ;
32
34
import org .hibernate .tool .schema .spi .SchemaManagementTool ;
35
+
33
36
import org .junit .After ;
37
+ import org .junit .AfterClass ;
34
38
import org .junit .Before ;
35
39
import org .junit .ClassRule ;
36
40
import org .junit .Rule ;
60
64
@ RunWith (VertxUnitRunner .class )
61
65
public abstract class BaseReactiveTest {
62
66
67
+ public static SessionFactoryManager factoryManager = new SessionFactoryManager ();
68
+
63
69
@ Rule
64
70
public Timeout rule = Timeout .seconds ( 5 * 60 );
65
71
@@ -74,8 +80,6 @@ public abstract class BaseReactiveTest {
74
80
75
81
private AutoCloseable session , statelessSession ;
76
82
private ReactiveConnection connection ;
77
- private org .hibernate .SessionFactory sessionFactory ;
78
- private ReactiveConnectionPool poolProvider ;
79
83
80
84
protected static void test (TestContext context , CompletionStage <?> work ) {
81
85
// this will be added to TestContext in the next vert.x release
@@ -151,31 +155,44 @@ public CompletionStage<Void> deleteEntities(String... entities) {
151
155
152
156
@ Before
153
157
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 () {
154
187
Configuration configuration = constructConfiguration ();
155
188
StandardServiceRegistryBuilder builder = new ReactiveServiceRegistryBuilder ()
156
189
.addService ( VertxInstance .class , (VertxInstance ) () -> vertxContextRule .vertx () )
157
190
.applySettings ( configuration .getProperties () );
158
191
addServices ( builder );
159
192
StandardServiceRegistry registry = builder .build ();
160
193
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 ;
179
196
}
180
197
181
198
protected void addServices (StandardServiceRegistryBuilder builder ) {}
@@ -226,14 +243,15 @@ public void after(TestContext context) {
226
243
connection = null ;
227
244
}
228
245
}
246
+ }
229
247
230
- if ( sessionFactory != null ) {
231
- sessionFactory . close ();
232
- }
248
+ @ AfterClass
249
+ public static void closeFactory () {
250
+ factoryManager . stop ();
233
251
}
234
252
235
253
protected Stage .SessionFactory getSessionFactory () {
236
- return sessionFactory .unwrap ( Stage .SessionFactory .class );
254
+ return factoryManager . getHibernateSessionFactory () .unwrap ( Stage .SessionFactory .class );
237
255
}
238
256
239
257
/**
@@ -260,7 +278,7 @@ protected Stage.StatelessSession openStatelessSession() {
260
278
}
261
279
262
280
protected CompletionStage <ReactiveConnection > connection () {
263
- return poolProvider .getConnection ().thenApply ( c -> connection = c );
281
+ return factoryManager . getReactiveConnectionPool () .getConnection ().thenApply ( c -> connection = c );
264
282
}
265
283
266
284
/**
@@ -277,10 +295,6 @@ protected Mutiny.Session openMutinySession() {
277
295
return newSession ;
278
296
}
279
297
280
- protected Mutiny .SessionFactory getMutinySessionFactory () {
281
- return sessionFactory .unwrap ( Mutiny .SessionFactory .class );
282
- }
283
-
284
298
protected Mutiny .StatelessSession openMutinyStatelessSession () {
285
299
if ( statelessSession != null ) {
286
300
statelessSession .close ();
@@ -290,4 +304,7 @@ protected Mutiny.StatelessSession openMutinyStatelessSession() {
290
304
return newSession ;
291
305
}
292
306
307
+ protected Mutiny .SessionFactory getMutinySessionFactory () {
308
+ return factoryManager .getHibernateSessionFactory ().unwrap ( Mutiny .SessionFactory .class );
309
+ }
293
310
}
0 commit comments