Skip to content

Commit 0d3943e

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 0d3943e

File tree

1 file changed

+60
-52
lines changed

1 file changed

+60
-52
lines changed

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

Lines changed: 60 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,93 @@ 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+
.persist( new Competition( "Cheese Rolling" ) )
67+
.thenCompose( v -> session.flush() )
68+
.handle( (v, e) -> assertExceptionThrown( e ).join() ) )
6069
);
6170
}
6271

63-
6472
@Test
65-
public void testFindWithStage(TestContext testContext) throws Exception {
73+
public void testFindWithStage(TestContext testContext) {
74+
Async async = testContext.async();
6675
Stage.Session session = openSession();
6776
Context testVertxContext = Vertx.currentContext();
6877

6978
// Create a different new context
70-
Vertx vertx = Vertx.vertx();
71-
Context newContext = vertx.getOrCreateContext();
79+
Context newContext = Vertx.vertx().getOrCreateContext();
7280
Assertions.assertThat( testVertxContext ).isNotEqualTo( newContext );
7381

7482
// 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-
} ) )
83+
newContext.runOnContext( event -> test( async, testContext, session
84+
.find( Competition.class, "Chess boxing" )
85+
.handle( (v, e) -> assertExceptionThrown( e ).join() ) )
8586
);
8687
}
8788

8889
@Test
89-
public void testOnPersistWithMutiny(TestContext testContext) throws Exception {
90+
public void testOnPersistWithMutiny(TestContext testContext) {
91+
Async async = testContext.async();
9092
Mutiny.Session session = openMutinySession();
9193
Context testVertxContext = Vertx.currentContext();
9294

9395
// Create a different new context
94-
Vertx vertx = Vertx.vertx();
95-
Context newContext = vertx.getOrCreateContext();
96+
Context newContext = Vertx.vertx().getOrCreateContext();
9697
Assertions.assertThat( testVertxContext ).isNotEqualTo( newContext );
9798

9899
// 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-
} ) )
100+
newContext.runOnContext( event -> test( async, testContext, session
101+
.persist( new Competition( "Cheese Rolling" ) )
102+
.call( session::flush )
103+
.onItemOrFailure()
104+
.transformToUni( (unused, e) -> Uni.createFrom().completionStage( assertExceptionThrown( e ) ) ) )
108105
);
109106
}
110107

111108
@Test
112-
public void testFindWithMutiny(TestContext testContext) throws Exception {
109+
public void testFindWithMutiny(TestContext testContext) {
110+
Async async = testContext.async();
113111
Mutiny.Session session = openMutinySession();
114112
Context testVertxContext = Vertx.currentContext();
115113

116114
// Create a different new context
117-
Vertx vertx = Vertx.vertx();
118-
Context newContext = vertx.getOrCreateContext();
115+
Context newContext = Vertx.vertx().getOrCreateContext();
119116
Assertions.assertThat( testVertxContext ).isNotEqualTo( newContext );
120117

121118
// 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-
} ) )
119+
newContext.runOnContext( event -> test( async, testContext, session
120+
.find( Competition.class, "Chess boxing" )
121+
.onItemOrFailure()
122+
.transformToUni( (unused, e) -> Uni.createFrom().completionStage( assertExceptionThrown( e ) ) ) )
131123
);
132124
}
133125

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

0 commit comments

Comments
 (0)