5
5
*/
6
6
package org .hibernate .reactive ;
7
7
8
- import io .vertx .core .*;
9
- import io .vertx .ext .unit .Async ;
10
- import io .vertx .ext .unit .TestContext ;
11
- import io .vertx .ext .unit .junit .VertxUnitRunner ;
12
- import jakarta .persistence .Entity ;
13
- import jakarta .persistence .GeneratedValue ;
14
- import jakarta .persistence .Id ;
15
- import jakarta .persistence .Table ;
8
+ import java .util .concurrent .CompletionStage ;
9
+ import java .util .concurrent .CountDownLatch ;
10
+ import java .util .concurrent .TimeUnit ;
16
11
17
12
import org .hibernate .SessionFactory ;
18
13
import org .hibernate .boot .registry .StandardServiceRegistry ;
19
14
import org .hibernate .boot .registry .StandardServiceRegistryBuilder ;
20
15
import org .hibernate .cfg .Configuration ;
21
16
import org .hibernate .reactive .provider .ReactiveServiceRegistryBuilder ;
22
- import org .hibernate .reactive .provider .Settings ;
23
17
import org .hibernate .reactive .stage .Stage ;
24
18
import org .hibernate .reactive .testing .DatabaseSelectionRule ;
25
- import org .hibernate .reactive .util .impl .CompletionStages ;
26
19
import org .hibernate .reactive .vertx .VertxInstance ;
20
+
27
21
import org .junit .AfterClass ;
28
22
import org .junit .BeforeClass ;
29
23
import org .junit .Rule ;
30
24
import org .junit .Test ;
31
25
import org .junit .runner .RunWith ;
32
26
33
- import java .util .concurrent .CompletionStage ;
34
- import java .util .concurrent .CountDownLatch ;
35
- import java .util .concurrent .TimeUnit ;
27
+ import io .vertx .core .AbstractVerticle ;
28
+ import io .vertx .core .DeploymentOptions ;
29
+ import io .vertx .core .Promise ;
30
+ import io .vertx .core .Vertx ;
31
+ import io .vertx .core .VertxOptions ;
32
+ import io .vertx .ext .unit .Async ;
33
+ import io .vertx .ext .unit .TestContext ;
34
+ import io .vertx .ext .unit .junit .VertxUnitRunner ;
35
+ import jakarta .persistence .Entity ;
36
+ import jakarta .persistence .GeneratedValue ;
37
+ import jakarta .persistence .Id ;
38
+ import jakarta .persistence .Table ;
36
39
40
+ import static org .hibernate .cfg .AvailableSettings .SHOW_SQL ;
37
41
import static org .hibernate .reactive .containers .DatabaseConfiguration .DBType .SQLSERVER ;
42
+ import static org .hibernate .reactive .util .impl .CompletionStages .loop ;
38
43
39
44
/**
40
45
* This is a multi-threaded stress test, intentionally consuming some time.
@@ -75,7 +80,14 @@ public class MultithreadedInsertionTest {
75
80
//Should finish much sooner, but generating this amount of IDs could be slow on some CIs
76
81
private static final int TIMEOUT_MINUTES = 10 ;
77
82
83
+ // Keeping this disabled because it generates a lot of queries
78
84
private static final boolean LOG_SQL = false ;
85
+
86
+ /**
87
+ * If true, it will print info about the threads
88
+ */
89
+ private static final boolean THREAD_PRETTY_MSG = false ;
90
+
79
91
private static final Latch startLatch = new Latch ( "start" , N_THREADS );
80
92
private static final Latch endLatch = new Latch ( "end" , N_THREADS );
81
93
@@ -97,7 +109,7 @@ public static void setupSessionFactory() {
97
109
Configuration configuration = new Configuration ();
98
110
configuration .addAnnotatedClass ( EntityWithGeneratedId .class );
99
111
BaseReactiveTest .setDefaultProperties ( configuration );
100
- configuration .setProperty ( Settings . SHOW_SQL , String .valueOf ( LOG_SQL ) );
112
+ configuration .setProperty ( SHOW_SQL , String .valueOf ( LOG_SQL ) );
101
113
StandardServiceRegistryBuilder builder = new ReactiveServiceRegistryBuilder ()
102
114
.applySettings ( configuration .getProperties () )
103
115
//Inject our custom vert.x instance:
@@ -112,7 +124,7 @@ public static void closeSessionFactory() {
112
124
stageSessionFactory .close ();
113
125
}
114
126
115
- @ Test (timeout = ( 1000 * 60 * 10 ))//10 minutes timeout
127
+ @ Test (timeout = ( 1000 * 60 * TIMEOUT_MINUTES ))
116
128
public void testIdentityGenerator (TestContext context ) {
117
129
final Async async = context .async ();
118
130
@@ -141,9 +153,8 @@ public void start(Promise<Void> startPromise) {
141
153
startLatch .reached ();
142
154
startLatch .waitForEveryone ();//Not essential, but to ensure a good level of parallelism
143
155
final String initialThreadName = Thread .currentThread ().getName ();
144
- stageSessionFactory .withSession (
145
- s -> storeMultipleEntities ( s )
146
- )
156
+ stageSessionFactory
157
+ .withSession ( this ::storeMultipleEntities )
147
158
.whenComplete ( (o , throwable ) -> {
148
159
endLatch .reached ();
149
160
if ( throwable != null ) {
@@ -161,7 +172,7 @@ public void start(Promise<Void> startPromise) {
161
172
}
162
173
163
174
private CompletionStage <Void > storeMultipleEntities ( Stage .Session s ) {
164
- return CompletionStages . loop ( 0 , ENTITIES_STORED_PER_THREAD , index -> storeEntity ( s ) );
175
+ return loop ( 0 , ENTITIES_STORED_PER_THREAD , index -> storeEntity ( s ) );
165
176
}
166
177
167
178
private CompletionStage <Void > storeEntity (Stage .Session s ) {
@@ -233,14 +244,16 @@ public void waitForEveryone() {
233
244
}
234
245
235
246
private static void prettyOut (final String message ) {
236
- final String threadName = Thread .currentThread ().getName ();
237
- final long l = System .currentTimeMillis ();
238
- final long seconds = ( l / 1000 ) - initialSecond ;
239
- //We prefix log messages by seconds since bootstrap; I'm preferring this over millisecond precision
240
- //as it's not very relevant to see exactly how long each stage took (it's actually distracting)
241
- //but it's more useful to group things coarsely when some lock or timeout introduces a significant
242
- //divide between some operations (when a starvation or timeout happens it takes some seconds).
243
- System .out .println ( seconds + " - " + threadName + ": " + message );
247
+ if ( THREAD_PRETTY_MSG ) {
248
+ final String threadName = Thread .currentThread ().getName ();
249
+ final long l = System .currentTimeMillis ();
250
+ final long seconds = ( l / 1000 ) - initialSecond ;
251
+ //We prefix log messages by seconds since bootstrap; I'm preferring this over millisecond precision
252
+ //as it's not very relevant to see exactly how long each stage took (it's actually distracting)
253
+ //but it's more useful to group things coarsely when some lock or timeout introduces a significant
254
+ //divide between some operations (when a starvation or timeout happens it takes some seconds).
255
+ System .out .println ( seconds + " - " + threadName + ": " + message );
256
+ }
244
257
}
245
258
246
259
private static final long initialSecond = ( System .currentTimeMillis () / 1000 );
0 commit comments