Skip to content

Commit 2195e96

Browse files
committed
HHH-19916 - Drop JUnit 4 usage
Not converted... * org.hibernate.orm.test.type.AbstractJavaTimeTypeTest subtypes - crazy parameterization (see org.hibernate.orm.test.tm.InterceptorTransactionTest)
1 parent 6687615 commit 2195e96

File tree

1 file changed

+49
-219
lines changed

1 file changed

+49
-219
lines changed

hibernate-core/src/test/java/org/hibernate/orm/test/idgen/enhanced/HiloOptimizerConcurrencyTest.java

Lines changed: 49 additions & 219 deletions
Original file line numberDiff line numberDiff line change
@@ -4,257 +4,87 @@
44
*/
55
package org.hibernate.orm.test.idgen.enhanced;
66

7-
import java.util.Arrays;
8-
import java.util.List;
9-
import java.util.concurrent.Callable;
10-
import java.util.concurrent.CopyOnWriteArrayList;
11-
import java.util.concurrent.CountDownLatch;
12-
import java.util.concurrent.ExecutionException;
13-
import java.util.concurrent.ExecutorService;
14-
import java.util.concurrent.Executors;
15-
import java.util.concurrent.TimeUnit;
167
import jakarta.persistence.Entity;
178
import jakarta.persistence.GeneratedValue;
189
import jakarta.persistence.Id;
19-
20-
import org.hibernate.Session;
2110
import org.hibernate.annotations.GenericGenerator;
2211
import org.hibernate.annotations.Parameter;
23-
import org.hibernate.boot.registry.StandardServiceRegistry;
12+
import org.hibernate.boot.MetadataSources;
2413
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
14+
import org.hibernate.boot.spi.MetadataImplementor;
2515
import org.hibernate.engine.spi.SessionFactoryImplementor;
26-
import org.hibernate.exception.ConstraintViolationException;
27-
16+
import org.hibernate.engine.spi.SessionImplementor;
17+
import org.hibernate.service.ServiceRegistry;
2818
import org.hibernate.testing.orm.junit.JiraKey;
29-
import org.hibernate.testing.junit4.BaseNonConfigCoreFunctionalTestCase;
30-
import org.junit.Test;
19+
import org.hibernate.testing.orm.transaction.TransactionUtil;
20+
import org.hibernate.tool.schema.Action;
21+
import org.junit.jupiter.api.Test;
3122

32-
import static org.junit.Assert.fail;
23+
import java.util.function.Consumer;
24+
25+
import static org.hibernate.cfg.SchemaToolingSettings.HBM2DDL_AUTO;
3326

3427
/**
35-
* Demonstrates HHH-3628 issue with rolling over buckets in HiLoOptimizer. There are
36-
* two variants of the test which do pretty much the same thing - one in sessions in
37-
* parallel threads and one simply performing actions in sequence in two sessions.
38-
* Possibly the threaded version is somewhat redundant given that the simpler test
39-
* also exhibits the problem.
40-
*
4128
* @author Richard Barnes 4 May 2016
4229
*/
4330
@JiraKey(value = "HHH-3628")
44-
public class HiloOptimizerConcurrencyTest extends BaseNonConfigCoreFunctionalTestCase {
45-
46-
private boolean createSchema = true;
47-
48-
private ExecutorService executor = Executors.newFixedThreadPool( 2 );
49-
50-
@Override
51-
protected Class[] getAnnotatedClasses() {
52-
return new Class[] {
53-
HibPerson.class
54-
};
55-
}
31+
public class HiloOptimizerConcurrencyTest {
5632

5733
@Test
58-
public void testTwoSessionsParallelGeneration() {
59-
createSchema = true;
60-
61-
StandardServiceRegistry serviceRegistry = serviceRegistry();
62-
SessionFactoryImplementor sessionFactory = sessionFactory();
63-
64-
try {
65-
final Session session1 = openSession();
34+
public void testTwoSessionsSerialGeneration() {
35+
inSessionFactory( true, (sf1) -> {
36+
var session1 = sf1.openSession();
6637

67-
try {
68-
session1.beginTransaction();
69-
HibPerson p = new HibPerson();
38+
TransactionUtil.inTransaction( session1, (SessionImplementor s) -> {
39+
var p = new HibPerson();
7040
session1.persist( p );
71-
}
72-
finally {
73-
session1.getTransaction().commit();
74-
}
75-
76-
createSchema = false;
77-
buildResources();
78-
79-
final Session session2 = openSession();
80-
try {
81-
session2.beginTransaction();
82-
HibPerson p = new HibPerson();
83-
session2.persist( p );
84-
}
85-
finally {
86-
session2.getTransaction().commit();
87-
}
41+
} );
8842

89-
final List<Throwable> errs = new CopyOnWriteArrayList<>();
43+
inSessionFactory( false, (sf2) -> {
44+
var session2 = sf2.openSession();
9045

91-
CountDownLatch firstLatch = new CountDownLatch( 1 );
92-
CountDownLatch secondLatch = new CountDownLatch( 1 );
46+
TransactionUtil.inTransaction( session2, (SessionImplementor s) -> {
47+
var p = new HibPerson();
48+
session2.persist( p );
49+
} );
9350

94-
Callable<Void> callable1 = () -> {
95-
try {
96-
for ( int i = 2; i < 6; i++ ) {
97-
try {
98-
session1.beginTransaction();
99-
HibPerson p = new HibPerson();
100-
session1.persist( p );
101-
}
102-
finally {
103-
session1.getTransaction().commit();
104-
}
105-
}
106-
firstLatch.countDown();
107-
secondLatch.await();
108-
try {
109-
session1.beginTransaction();
110-
HibPerson p = new HibPerson();
51+
for ( int i = 2; i < 6; i++ ) {
52+
TransactionUtil.inTransaction( session1, (SessionImplementor s) -> {
53+
var p = new HibPerson();
11154
session1.persist( p );
112-
}
113-
finally {
114-
session1.getTransaction().commit();
115-
}
116-
}
117-
catch (Throwable t) {
118-
errs.add( t );
119-
}
120-
121-
return null;
122-
};
123-
124-
Callable<Void> callable2 = () -> {
125-
try {
126-
firstLatch.await();
127-
secondLatch.countDown();
128-
try {
129-
session2.beginTransaction();
130-
HibPerson p = new HibPerson();
131-
session2.persist( p );
132-
}
133-
finally {
134-
session2.getTransaction().commit();
135-
}
136-
}
137-
catch (Throwable t) {
138-
errs.add( t );
55+
} );
13956
}
14057

141-
return null;
142-
};
143-
144-
executor.invokeAll( Arrays.asList(
145-
callable1, callable2
146-
) , 30, TimeUnit.SECONDS).forEach( c -> {
147-
try {
148-
c.get();
149-
}
150-
catch (InterruptedException|ExecutionException e) {
151-
Thread.interrupted();
152-
fail(e.getMessage());
153-
}
154-
});
155-
156-
for(Throwable ex : errs) {
157-
fail(ex.getMessage());
158-
}
159-
}
160-
catch (InterruptedException e) {
161-
fail(e.getMessage());
162-
}
163-
finally {
164-
releaseResources( serviceRegistry, sessionFactory );
165-
}
166-
}
58+
TransactionUtil.inTransaction( session2, (SessionImplementor s) -> {
59+
var p = new HibPerson();
60+
session2.persist( p );
61+
} );
16762

168-
@Test
169-
public void testTwoSessionsSerialGeneration() {
170-
createSchema = true;
171-
rebuildSessionFactory();
172-
StandardServiceRegistry serviceRegistry = serviceRegistry();
173-
SessionFactoryImplementor sessionFactory = sessionFactory();
174-
175-
try {
176-
final Session session1 = openSession();
177-
178-
try {
179-
session1.beginTransaction();
180-
HibPerson p = new HibPerson();
181-
session1.persist( p );
182-
}
183-
finally {
184-
session1.getTransaction().commit();
185-
}
186-
187-
createSchema = false;
188-
buildResources();
189-
190-
final Session session2 = openSession();
191-
session2.beginTransaction();
192-
try {
193-
HibPerson p = new HibPerson();
194-
session2.persist( p );
195-
}
196-
finally {
197-
session2.getTransaction().commit();
198-
}
199-
200-
for ( int i = 2; i < 6; i++ ) {
201-
session1.beginTransaction();
202-
try {
203-
HibPerson p = new HibPerson();
63+
TransactionUtil.inTransaction( session1, (SessionImplementor s) -> {
64+
var p = new HibPerson();
20465
session1.persist( p );
205-
}
206-
finally {
207-
session1.getTransaction().commit();
208-
}
209-
}
210-
session2.beginTransaction();
211-
try {
212-
HibPerson p = new HibPerson();
213-
session2.persist( p );
214-
}
215-
finally {
216-
session2.getTransaction().commit();
217-
}
218-
session1.beginTransaction();
219-
try {
220-
HibPerson p = new HibPerson();
221-
session1.persist( p );
222-
}
223-
finally {
224-
session1.getTransaction().commit();
225-
}
226-
}
227-
catch (ConstraintViolationException cve) {
228-
fail( "ConstraintViolationException: " + cve.getMessage() );
229-
}
230-
finally {
231-
releaseResources( serviceRegistry, sessionFactory );
232-
}
66+
} );
67+
} );
68+
} );
23369
}
23470

235-
private void releaseResources(StandardServiceRegistry serviceRegistry, SessionFactoryImplementor sessionFactory) {
236-
if ( sessionFactory != null ) {
237-
try {
238-
sessionFactory.close();
239-
}
240-
catch (Exception e) {
241-
fail( "Unable to release SessionFactory : " + e.getMessage() );
242-
}
243-
}
244-
245-
if ( serviceRegistry != null ) {
246-
try {
247-
StandardServiceRegistryBuilder.destroy( serviceRegistry );
248-
}
249-
catch (Exception e) {
250-
fail( "Unable to release StandardServiceRegistry : " + e.getMessage() );
71+
private void inSessionFactory(boolean createSchema, Consumer<SessionFactoryImplementor> action) {
72+
try (var serviceRegistry = createServiceRegistry( createSchema )) {
73+
final MetadataImplementor metadata = (MetadataImplementor) new MetadataSources( serviceRegistry )
74+
.addAnnotatedClass( HibPerson.class )
75+
.buildMetadata();
76+
try (var sessionFactory = metadata.buildSessionFactory()) {
77+
action.accept( sessionFactory );
25178
}
25279
}
25380
}
25481

255-
@Override
256-
protected boolean createSchema() {
257-
return createSchema;
82+
private ServiceRegistry createServiceRegistry(boolean createSchema) {
83+
final StandardServiceRegistryBuilder ssrb = new StandardServiceRegistryBuilder();
84+
if ( createSchema ) {
85+
ssrb.applySetting( HBM2DDL_AUTO, Action.CREATE_DROP );
86+
}
87+
return ssrb.build();
25888
}
25989

26090
@Entity(name = "HibPerson")

0 commit comments

Comments
 (0)