Skip to content

Commit ff1477a

Browse files
committed
[#1613] Minor clean up for Multithreaded*Test
Disable printing the threads messages.
1 parent 36240cc commit ff1477a

File tree

2 files changed

+81
-58
lines changed

2 files changed

+81
-58
lines changed

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

Lines changed: 41 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -5,26 +5,21 @@
55
*/
66
package org.hibernate.reactive;
77

8-
import io.vertx.core.AbstractVerticle;
9-
import io.vertx.core.DeploymentOptions;
10-
import io.vertx.core.Promise;
11-
import io.vertx.core.Vertx;
12-
import io.vertx.core.VertxOptions;
13-
import io.vertx.ext.unit.Async;
14-
import io.vertx.ext.unit.TestContext;
15-
import io.vertx.ext.unit.junit.VertxUnitRunner;
16-
import jakarta.persistence.Entity;
17-
import jakarta.persistence.GeneratedValue;
18-
import jakarta.persistence.Id;
19-
import jakarta.persistence.Table;
8+
import java.util.ArrayList;
9+
import java.util.BitSet;
10+
import java.util.List;
11+
import java.util.concurrent.CompletionStage;
12+
import java.util.concurrent.ConcurrentHashMap;
13+
import java.util.concurrent.ConcurrentMap;
14+
import java.util.concurrent.CountDownLatch;
15+
import java.util.concurrent.TimeUnit;
2016

2117
import org.hibernate.SessionFactory;
2218
import org.hibernate.boot.registry.StandardServiceRegistry;
2319
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
2420
import org.hibernate.cfg.Configuration;
2521
import org.hibernate.reactive.id.impl.ReactiveGeneratorWrapper;
2622
import org.hibernate.reactive.provider.ReactiveServiceRegistryBuilder;
27-
import org.hibernate.reactive.provider.Settings;
2823
import org.hibernate.reactive.session.ReactiveConnectionSupplier;
2924
import org.hibernate.reactive.session.impl.ReactiveSessionFactoryImpl;
3025
import org.hibernate.reactive.stage.Stage;
@@ -39,15 +34,20 @@
3934
import org.junit.Test;
4035
import org.junit.runner.RunWith;
4136

42-
import java.util.ArrayList;
43-
import java.util.BitSet;
44-
import java.util.List;
45-
import java.util.concurrent.CompletionStage;
46-
import java.util.concurrent.ConcurrentHashMap;
47-
import java.util.concurrent.ConcurrentMap;
48-
import java.util.concurrent.CountDownLatch;
49-
import java.util.concurrent.TimeUnit;
37+
import io.vertx.core.AbstractVerticle;
38+
import io.vertx.core.DeploymentOptions;
39+
import io.vertx.core.Promise;
40+
import io.vertx.core.Vertx;
41+
import io.vertx.core.VertxOptions;
42+
import io.vertx.ext.unit.Async;
43+
import io.vertx.ext.unit.TestContext;
44+
import io.vertx.ext.unit.junit.VertxUnitRunner;
45+
import jakarta.persistence.Entity;
46+
import jakarta.persistence.GeneratedValue;
47+
import jakarta.persistence.Id;
48+
import jakarta.persistence.Table;
5049

50+
import static org.hibernate.cfg.AvailableSettings.SHOW_SQL;
5151
import static org.hibernate.reactive.containers.DatabaseConfiguration.DBType.SQLSERVER;
5252

5353
/**
@@ -71,7 +71,14 @@ public class MultithreadedIdentityGenerationTest {
7171
//Should finish much sooner, but generating this amount of IDs could be slow on some CIs
7272
private static final int TIMEOUT_MINUTES = 10;
7373

74+
// Keeping this disabled because it generates a lot of queries
7475
private static final boolean LOG_SQL = false;
76+
77+
/**
78+
* If true, it will print info about the threads
79+
*/
80+
private static final boolean THREAD_PRETTY_MSG = false;
81+
7582
private static final Latch startLatch = new Latch( "start", N_THREADS );
7683
private static final Latch endLatch = new Latch( "end", N_THREADS );
7784

@@ -96,7 +103,8 @@ public static void setupSessionFactory() {
96103
Configuration configuration = new Configuration();
97104
configuration.addAnnotatedClass( EntityWithGeneratedId.class );
98105
BaseReactiveTest.setDefaultProperties( configuration );
99-
configuration.setProperty( Settings.SHOW_SQL, String.valueOf( LOG_SQL ) );
106+
configuration.setProperty( SHOW_SQL, String.valueOf( LOG_SQL ) );
107+
BaseReactiveTest.setDefaultProperties( configuration );
100108
StandardServiceRegistryBuilder builder = new ReactiveServiceRegistryBuilder()
101109
.applySettings( configuration.getProperties() )
102110
//Inject our custom vert.x instance:
@@ -141,7 +149,7 @@ public void testIdentityGenerator(TestContext context) {
141149
}
142150
} )
143151
.onFailure( context::fail )
144-
.eventually( unused -> vertx.close() );
152+
.eventually( v -> vertx.close() );
145153
}
146154

147155
private boolean allResultsAreUnique(ResultsCollector allResults) {
@@ -289,14 +297,16 @@ public void waitForEveryone() {
289297
}
290298

291299
private static void prettyOut(final String message) {
292-
final String threadName = Thread.currentThread().getName();
293-
final long l = System.currentTimeMillis();
294-
final long seconds = ( l / 1000 ) - initialSecond;
295-
//We prefix log messages by seconds since bootstrap; I'm preferring this over millisecond precision
296-
//as it's not very relevant to see exactly how long each stage took (it's actually distracting)
297-
//but it's more useful to group things coarsely when some lock or timeout introduces a significant
298-
//divide between some operations (when a starvation or timeout happens it takes some seconds).
299-
System.out.println( seconds + " - " + threadName + ": " + message );
300+
if ( THREAD_PRETTY_MSG ) {
301+
final String threadName = Thread.currentThread().getName();
302+
final long l = System.currentTimeMillis();
303+
final long seconds = ( l / 1000 ) - initialSecond;
304+
//We prefix log messages by seconds since bootstrap; I'm preferring this over millisecond precision
305+
//as it's not very relevant to see exactly how long each stage took (it's actually distracting)
306+
//but it's more useful to group things coarsely when some lock or timeout introduces a significant
307+
//divide between some operations (when a starvation or timeout happens it takes some seconds).
308+
System.out.println( seconds + " - " + threadName + ": " + message );
309+
}
300310
}
301311

302312
private static final long initialSecond = ( System.currentTimeMillis() / 1000 );

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

Lines changed: 40 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -5,36 +5,41 @@
55
*/
66
package org.hibernate.reactive;
77

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;
1611

1712
import org.hibernate.SessionFactory;
1813
import org.hibernate.boot.registry.StandardServiceRegistry;
1914
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
2015
import org.hibernate.cfg.Configuration;
2116
import org.hibernate.reactive.provider.ReactiveServiceRegistryBuilder;
22-
import org.hibernate.reactive.provider.Settings;
2317
import org.hibernate.reactive.stage.Stage;
2418
import org.hibernate.reactive.testing.DatabaseSelectionRule;
25-
import org.hibernate.reactive.util.impl.CompletionStages;
2619
import org.hibernate.reactive.vertx.VertxInstance;
20+
2721
import org.junit.AfterClass;
2822
import org.junit.BeforeClass;
2923
import org.junit.Rule;
3024
import org.junit.Test;
3125
import org.junit.runner.RunWith;
3226

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;
3639

40+
import static org.hibernate.cfg.AvailableSettings.SHOW_SQL;
3741
import static org.hibernate.reactive.containers.DatabaseConfiguration.DBType.SQLSERVER;
42+
import static org.hibernate.reactive.util.impl.CompletionStages.loop;
3843

3944
/**
4045
* This is a multi-threaded stress test, intentionally consuming some time.
@@ -75,7 +80,14 @@ public class MultithreadedInsertionTest {
7580
//Should finish much sooner, but generating this amount of IDs could be slow on some CIs
7681
private static final int TIMEOUT_MINUTES = 10;
7782

83+
// Keeping this disabled because it generates a lot of queries
7884
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+
7991
private static final Latch startLatch = new Latch( "start", N_THREADS );
8092
private static final Latch endLatch = new Latch( "end", N_THREADS );
8193

@@ -97,7 +109,7 @@ public static void setupSessionFactory() {
97109
Configuration configuration = new Configuration();
98110
configuration.addAnnotatedClass( EntityWithGeneratedId.class );
99111
BaseReactiveTest.setDefaultProperties( configuration );
100-
configuration.setProperty( Settings.SHOW_SQL, String.valueOf( LOG_SQL ) );
112+
configuration.setProperty( SHOW_SQL, String.valueOf( LOG_SQL ) );
101113
StandardServiceRegistryBuilder builder = new ReactiveServiceRegistryBuilder()
102114
.applySettings( configuration.getProperties() )
103115
//Inject our custom vert.x instance:
@@ -112,7 +124,7 @@ public static void closeSessionFactory() {
112124
stageSessionFactory.close();
113125
}
114126

115-
@Test(timeout = ( 1000 * 60 * 10 ))//10 minutes timeout
127+
@Test(timeout = ( 1000 * 60 * TIMEOUT_MINUTES ))
116128
public void testIdentityGenerator(TestContext context) {
117129
final Async async = context.async();
118130

@@ -141,9 +153,8 @@ public void start(Promise<Void> startPromise) {
141153
startLatch.reached();
142154
startLatch.waitForEveryone();//Not essential, but to ensure a good level of parallelism
143155
final String initialThreadName = Thread.currentThread().getName();
144-
stageSessionFactory.withSession(
145-
s -> storeMultipleEntities( s )
146-
)
156+
stageSessionFactory
157+
.withSession( this::storeMultipleEntities )
147158
.whenComplete( (o, throwable) -> {
148159
endLatch.reached();
149160
if ( throwable != null ) {
@@ -161,7 +172,7 @@ public void start(Promise<Void> startPromise) {
161172
}
162173

163174
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 ) );
165176
}
166177

167178
private CompletionStage<Void> storeEntity(Stage.Session s) {
@@ -233,14 +244,16 @@ public void waitForEveryone() {
233244
}
234245

235246
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+
}
244257
}
245258

246259
private static final long initialSecond = ( System.currentTimeMillis() / 1000 );

0 commit comments

Comments
 (0)