Skip to content

Commit 9fe36dc

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

File tree

1 file changed

+44
-48
lines changed

1 file changed

+44
-48
lines changed

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

Lines changed: 44 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
*/
66
package org.hibernate.reactive;
77

8-
import java.util.concurrent.CompletionException;
98
import javax.persistence.Entity;
109
import javax.persistence.Id;
1110

@@ -18,12 +17,17 @@
1817
import io.smallrye.mutiny.Uni;
1918
import io.vertx.core.Context;
2019
import io.vertx.core.Vertx;
20+
import io.vertx.ext.unit.Async;
2121
import io.vertx.ext.unit.TestContext;
2222
import org.assertj.core.api.Assertions;
2323

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

@@ -38,99 +42,91 @@ protected Configuration constructConfiguration() {
3842

3943
@Test
4044
public void testPersistWithStage(TestContext testContext) throws Exception {
45+
Async async = testContext.async();
4146
Stage.Session session = openSession();
4247
Context testVertxContext = Vertx.currentContext();
4348

4449
// Create a different new context
45-
Vertx vertx = Vertx.vertx();
46-
Context newContext = vertx.getOrCreateContext();
50+
Context newContext = Vertx.vertx().getOrCreateContext();
4751
Assertions.assertThat( testVertxContext ).isNotEqualTo( newContext );
4852

4953
// 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-
} ) )
54+
newContext.runOnContext( event -> test( async, testContext, session
55+
.persist( new Competition( "Cheese Rolling" ) )
56+
.thenCompose( v -> session.flush() )
57+
.handle( (v, e) -> assertExceptionThrown( e ) ) )
6058
);
6159
}
6260

63-
6461
@Test
6562
public void testFindWithStage(TestContext testContext) throws Exception {
63+
Async async = testContext.async();
6664
Stage.Session session = openSession();
6765
Context testVertxContext = Vertx.currentContext();
6866

6967
// Create a different new context
70-
Vertx vertx = Vertx.vertx();
71-
Context newContext = vertx.getOrCreateContext();
68+
Context newContext = Vertx.vertx().getOrCreateContext();
7269
Assertions.assertThat( testVertxContext ).isNotEqualTo( newContext );
7370

7471
// 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-
} ) )
72+
newContext.runOnContext( event -> test( async, testContext, session
73+
.find( Competition.class, "Chess boxing" )
74+
.handle( (v, e) -> assertExceptionThrown( e ) ) )
8575
);
8676
}
8777

8878
@Test
8979
public void testOnPersistWithMutiny(TestContext testContext) throws Exception {
80+
Async async = testContext.async();
9081
Mutiny.Session session = openMutinySession();
9182
Context testVertxContext = Vertx.currentContext();
9283

9384
// Create a different new context
94-
Vertx vertx = Vertx.vertx();
95-
Context newContext = vertx.getOrCreateContext();
85+
Context newContext = Vertx.vertx().getOrCreateContext();
9686
Assertions.assertThat( testVertxContext ).isNotEqualTo( newContext );
9787

9888
// 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-
} ) )
89+
newContext.runOnContext( event -> test( async, testContext, session
90+
.persist( new Competition( "Cheese Rolling" ) )
91+
.call( session::flush )
92+
.onItem().invoke( v -> testContext.fail( "We were expecting an exception" ) )
93+
.onFailure().recoverWithUni( e -> Uni.createFrom().voidItem()
94+
.invoke( () -> 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+
.onItem().invoke( v -> testContext.fail( "We were expecting an exception" ) )
112+
.onFailure().recoverWithUni( e -> Uni.createFrom().voidItem()
113+
.replaceWith( () -> assertExceptionThrown( e ) ) ) )
131114
);
132115
}
133116

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

0 commit comments

Comments
 (0)