Skip to content

Commit eec0364

Browse files
committed
[#487] Fix MultipleContextTest
* Make sure test actually runs * Remove unecessary local variables * Less strict check on the exception location
1 parent 347c31f commit eec0364

File tree

1 file changed

+59
-52
lines changed

1 file changed

+59
-52
lines changed

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

Lines changed: 59 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -5,30 +5,45 @@
55
*/
66
package org.hibernate.reactive;
77

8-
import java.util.concurrent.CompletionException;
8+
import java.util.concurrent.CompletableFuture;
99
import javax.persistence.Entity;
1010
import javax.persistence.Id;
1111

1212
import org.hibernate.cfg.Configuration;
1313
import org.hibernate.reactive.mutiny.Mutiny;
1414
import org.hibernate.reactive.stage.Stage;
15+
import org.hibernate.reactive.testing.DatabaseSelectionRule;
1516

17+
import org.junit.Rule;
1618
import org.junit.Test;
1719

1820
import io.smallrye.mutiny.Uni;
1921
import io.vertx.core.Context;
2022
import io.vertx.core.Vertx;
23+
import io.vertx.ext.unit.Async;
2124
import io.vertx.ext.unit.TestContext;
2225
import org.assertj.core.api.Assertions;
2326

27+
import static org.hibernate.reactive.containers.DatabaseConfiguration.DBType.POSTGRESQL;
28+
import static org.hibernate.reactive.testing.DatabaseSelectionRule.runOnlyFor;
29+
2430
/**
2531
* It's currently considered an error to share a Session between multiple reactive streams,
2632
* so we should detect that condition and throw an exception.
33+
* <p>
34+
* WARNING: Because we are running the code to test inside a function, we must create the {@link Async}
35+
* in advance. Otherwise the test will end successfully because the async has not been created yet.
36+
* </p>
2737
*/
2838
public class MultipleContextTest extends BaseReactiveTest {
2939

3040
private static final String ERROR_MESSAGE = "Detected use of the reactive Session from a different Thread";
3141

42+
// These tests will fail before touching the database, so there is no reason
43+
// to run them on all databases
44+
@Rule
45+
public DatabaseSelectionRule rule = runOnlyFor( POSTGRESQL );
46+
3247
@Override
3348
protected Configuration constructConfiguration() {
3449
Configuration configuration = super.constructConfiguration();
@@ -37,100 +52,92 @@ protected Configuration constructConfiguration() {
3752
}
3853

3954
@Test
40-
public void testPersistWithStage(TestContext testContext) throws Exception {
55+
public void testPersistWithStage(TestContext testContext) {
56+
Async async = testContext.async();
4157
Stage.Session session = openSession();
4258
Context testVertxContext = Vertx.currentContext();
4359

4460
// Create a different new context
45-
Vertx vertx = Vertx.vertx();
46-
Context newContext = vertx.getOrCreateContext();
61+
Context newContext = Vertx.vertx().getOrCreateContext();
4762
Assertions.assertThat( testVertxContext ).isNotEqualTo( newContext );
4863

4964
// Run test in the new context
50-
newContext.runOnContext( event ->
51-
test( testContext, session
52-
.persist( new Competition( "Cheese Rolling" ) )
53-
.handle( (v, e) -> {
54-
testContext.assertNotNull( e );
55-
testContext.assertEquals( CompletionException.class, e.getClass() );
56-
testContext.assertEquals( IllegalStateException.class, e.getCause().getClass() );
57-
testContext.assertTrue( e.getMessage().contains( ERROR_MESSAGE ) );
58-
return null;
59-
} ) )
65+
newContext.runOnContext( event -> test( async, testContext, session
66+
.withTransaction( transaction -> session.persist( new Competition( "Cheese Rolling" ) )
67+
.handle( (v, e) -> assertExceptionThrown( e ).join() ) ) )
6068
);
6169
}
6270

63-
6471
@Test
65-
public void testFindWithStage(TestContext testContext) throws Exception {
72+
public void testFindWithStage(TestContext testContext) {
73+
Async async = testContext.async();
6674
Stage.Session session = openSession();
6775
Context testVertxContext = Vertx.currentContext();
6876

6977
// Create a different new context
70-
Vertx vertx = Vertx.vertx();
71-
Context newContext = vertx.getOrCreateContext();
78+
Context newContext = Vertx.vertx().getOrCreateContext();
7279
Assertions.assertThat( testVertxContext ).isNotEqualTo( newContext );
7380

7481
// Run test in the new context
75-
newContext.runOnContext( event ->
76-
test( testContext, session
77-
.find( Competition.class, "Chess boxing" )
78-
.handle( (v, e) -> {
79-
testContext.assertNotNull( e );
80-
testContext.assertEquals( CompletionException.class, e.getClass() );
81-
testContext.assertEquals( IllegalStateException.class, e.getCause().getClass() );
82-
testContext.assertTrue( e.getMessage().contains( ERROR_MESSAGE ) );
83-
return null;
84-
} ) )
82+
newContext.runOnContext( event -> test( async, testContext, session
83+
.find( Competition.class, "Chess boxing" )
84+
.handle( (v, e) -> assertExceptionThrown( e ).join() ) )
8585
);
8686
}
8787

8888
@Test
89-
public void testOnPersistWithMutiny(TestContext testContext) throws Exception {
89+
public void testOnPersistWithMutiny(TestContext testContext) {
90+
Async async = testContext.async();
9091
Mutiny.Session session = openMutinySession();
9192
Context testVertxContext = Vertx.currentContext();
9293

9394
// Create a different new context
94-
Vertx vertx = Vertx.vertx();
95-
Context newContext = vertx.getOrCreateContext();
95+
Context newContext = Vertx.vertx().getOrCreateContext();
9696
Assertions.assertThat( testVertxContext ).isNotEqualTo( newContext );
9797

9898
// Run test in the new context
99-
newContext.runOnContext( event ->
100-
test( testContext, session
101-
.persist( new Competition( "Cheese Rolling" ) )
102-
.onItem().invoke( v -> testContext.fail( "We were expecting an exception" ) )
103-
.onFailure().recoverWithUni( e -> {
104-
testContext.assertEquals( IllegalStateException.class, e.getClass() );
105-
testContext.assertTrue( e.getMessage().contains( ERROR_MESSAGE ) );
106-
return Uni.createFrom().voidItem();
107-
} ) )
99+
newContext.runOnContext( event -> test( async, testContext, session
100+
.persist( new Competition( "Cheese Rolling" ) )
101+
.call( session::flush )
102+
.onItemOrFailure()
103+
.transformToUni( (unused, e) -> Uni.createFrom().completionStage( assertExceptionThrown( e ) ) ) )
108104
);
109105
}
110106

111107
@Test
112-
public void testFindWithMutiny(TestContext testContext) throws Exception {
108+
public void testFindWithMutiny(TestContext testContext) {
109+
Async async = testContext.async();
113110
Mutiny.Session session = openMutinySession();
114111
Context testVertxContext = Vertx.currentContext();
115112

116113
// Create a different new context
117-
Vertx vertx = Vertx.vertx();
118-
Context newContext = vertx.getOrCreateContext();
114+
Context newContext = Vertx.vertx().getOrCreateContext();
119115
Assertions.assertThat( testVertxContext ).isNotEqualTo( newContext );
120116

121117
// Run test in the new context
122-
newContext.runOnContext(event ->
123-
test( testContext, session
124-
.find( Competition.class, "Chess boxing" )
125-
.onItem().invoke( v -> testContext.fail( "We were expecting an exception" ) )
126-
.onFailure().recoverWithUni( e -> {
127-
testContext.assertEquals( IllegalStateException.class, e.getClass() );
128-
testContext.assertTrue( e.getMessage().contains( ERROR_MESSAGE ) );
129-
return Uni.createFrom().nullItem();
130-
} ) )
118+
newContext.runOnContext( event -> test( async, testContext, session
119+
.find( Competition.class, "Chess boxing" )
120+
.onItemOrFailure()
121+
.transformToUni( (unused, e) -> Uni.createFrom().completionStage( assertExceptionThrown( e ) ) ) )
131122
);
132123
}
133124

125+
// Check that at least one exception has the expected message
126+
private static CompletableFuture<Void> assertExceptionThrown(Throwable e) {
127+
CompletableFuture<Void> result = new CompletableFuture<>();
128+
Throwable t = e;
129+
while ( t != null ) {
130+
if ( t.getClass().equals( IllegalStateException.class )
131+
&& t.getMessage().contains( ERROR_MESSAGE ) ) {
132+
result.complete( null );
133+
return result;
134+
}
135+
t = t.getCause();
136+
}
137+
result.completeExceptionally( new AssertionError( "Expected exception not thrown. Exception thrown: " + e ) );
138+
return result;
139+
}
140+
134141
@Entity
135142
static class Competition {
136143
@Id

0 commit comments

Comments
 (0)