Skip to content

Commit 6b33c5a

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 6b33c5a

File tree

1 file changed

+46
-48
lines changed

1 file changed

+46
-48
lines changed

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

Lines changed: 46 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
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

@@ -18,12 +18,17 @@
1818
import io.smallrye.mutiny.Uni;
1919
import io.vertx.core.Context;
2020
import io.vertx.core.Vertx;
21+
import io.vertx.ext.unit.Async;
2122
import io.vertx.ext.unit.TestContext;
2223
import org.assertj.core.api.Assertions;
2324

2425
/**
2526
* It's currently considered an error to share a Session between multiple reactive streams,
2627
* so we should detect that condition and throw an exception.
28+
* <p>
29+
* WARNING: Because we are running the code to test inside a function, we must create the {@link Async}
30+
* in advance. Otherwise the test will end successfully because the async has not been created yet.
31+
* </p>
2732
*/
2833
public class MultipleContextTest extends BaseReactiveTest {
2934

@@ -38,99 +43,92 @@ protected Configuration constructConfiguration() {
3843

3944
@Test
4045
public void testPersistWithStage(TestContext testContext) throws Exception {
46+
Async async = testContext.async();
4147
Stage.Session session = openSession();
4248
Context testVertxContext = Vertx.currentContext();
4349

4450
// Create a different new context
45-
Vertx vertx = Vertx.vertx();
46-
Context newContext = vertx.getOrCreateContext();
51+
Context newContext = Vertx.vertx().getOrCreateContext();
4752
Assertions.assertThat( testVertxContext ).isNotEqualTo( newContext );
4853

4954
// 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-
} ) )
55+
newContext.runOnContext( event -> test( async, testContext, session
56+
.persist( new Competition( "Cheese Rolling" ) )
57+
.thenCompose( v -> session.flush() )
58+
.handle( (v, e) -> assertExceptionThrown( e ).join() ) )
6059
);
6160
}
6261

63-
6462
@Test
6563
public void testFindWithStage(TestContext testContext) throws Exception {
64+
Async async = testContext.async();
6665
Stage.Session session = openSession();
6766
Context testVertxContext = Vertx.currentContext();
6867

6968
// Create a different new context
70-
Vertx vertx = Vertx.vertx();
71-
Context newContext = vertx.getOrCreateContext();
69+
Context newContext = Vertx.vertx().getOrCreateContext();
7270
Assertions.assertThat( testVertxContext ).isNotEqualTo( newContext );
7371

7472
// 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-
} ) )
73+
newContext.runOnContext( event -> test( async, testContext, session
74+
.find( Competition.class, "Chess boxing" )
75+
.handle( (v, e) -> assertExceptionThrown( e ).join() ) )
8576
);
8677
}
8778

8879
@Test
8980
public void testOnPersistWithMutiny(TestContext testContext) throws Exception {
81+
Async async = testContext.async();
9082
Mutiny.Session session = openMutinySession();
9183
Context testVertxContext = Vertx.currentContext();
9284

9385
// Create a different new context
94-
Vertx vertx = Vertx.vertx();
95-
Context newContext = vertx.getOrCreateContext();
86+
Context newContext = Vertx.vertx().getOrCreateContext();
9687
Assertions.assertThat( testVertxContext ).isNotEqualTo( newContext );
9788

9889
// 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-
} ) )
90+
newContext.runOnContext( event -> test( async, testContext, session
91+
.persist( new Competition( "Cheese Rolling" ) )
92+
.call( session::flush )
93+
.onItemOrFailure()
94+
.transformToUni( (unused, e) -> Uni.createFrom().completionStage( assertExceptionThrown( e ) ) ) )
10895
);
10996
}
11097

11198
@Test
11299
public void testFindWithMutiny(TestContext testContext) throws Exception {
100+
Async async = testContext.async();
113101
Mutiny.Session session = openMutinySession();
114102
Context testVertxContext = Vertx.currentContext();
115103

116104
// Create a different new context
117-
Vertx vertx = Vertx.vertx();
118-
Context newContext = vertx.getOrCreateContext();
105+
Context newContext = Vertx.vertx().getOrCreateContext();
119106
Assertions.assertThat( testVertxContext ).isNotEqualTo( newContext );
120107

121108
// 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-
} ) )
109+
newContext.runOnContext( event -> test( async, testContext, session
110+
.find( Competition.class, "Chess boxing" )
111+
.onItemOrFailure()
112+
.transformToUni( (unused, e) -> Uni.createFrom().completionStage( assertExceptionThrown( e ) ) ) )
131113
);
132114
}
133115

116+
// Check that at least one exception has the expected message
117+
private static CompletableFuture<Void> assertExceptionThrown(Throwable e) {
118+
CompletableFuture<Void> result = new CompletableFuture<>();
119+
Throwable t = e;
120+
while ( t != null ) {
121+
if ( t.getClass().equals( IllegalStateException.class )
122+
&& t.getMessage().contains( ERROR_MESSAGE ) ) {
123+
result.complete( null );
124+
return result;
125+
}
126+
t = t.getCause();
127+
}
128+
result.completeExceptionally( new AssertionError( "Expected exception not thrown. Exception thrown: " + e ) );
129+
return result;
130+
}
131+
134132
@Entity
135133
static class Competition {
136134
@Id

0 commit comments

Comments
 (0)