5
5
*/
6
6
package org .hibernate .reactive ;
7
7
8
- import java .util .concurrent .CompletionException ;
9
8
import javax .persistence .Entity ;
10
9
import javax .persistence .Id ;
11
10
18
17
import io .smallrye .mutiny .Uni ;
19
18
import io .vertx .core .Context ;
20
19
import io .vertx .core .Vertx ;
20
+ import io .vertx .ext .unit .Async ;
21
21
import io .vertx .ext .unit .TestContext ;
22
22
import org .assertj .core .api .Assertions ;
23
23
24
24
/**
25
25
* It's currently considered an error to share a Session between multiple reactive streams,
26
26
* 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>
27
31
*/
28
32
public class MultipleContextTest extends BaseReactiveTest {
29
33
@@ -38,99 +42,91 @@ protected Configuration constructConfiguration() {
38
42
39
43
@ Test
40
44
public void testPersistWithStage (TestContext testContext ) throws Exception {
45
+ Async async = testContext .async ();
41
46
Stage .Session session = openSession ();
42
47
Context testVertxContext = Vertx .currentContext ();
43
48
44
49
// Create a different new context
45
- Vertx vertx = Vertx .vertx ();
46
- Context newContext = vertx .getOrCreateContext ();
50
+ Context newContext = Vertx .vertx ().getOrCreateContext ();
47
51
Assertions .assertThat ( testVertxContext ).isNotEqualTo ( newContext );
48
52
49
53
// 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 ) ) )
60
58
);
61
59
}
62
60
63
-
64
61
@ Test
65
62
public void testFindWithStage (TestContext testContext ) throws Exception {
63
+ Async async = testContext .async ();
66
64
Stage .Session session = openSession ();
67
65
Context testVertxContext = Vertx .currentContext ();
68
66
69
67
// Create a different new context
70
- Vertx vertx = Vertx .vertx ();
71
- Context newContext = vertx .getOrCreateContext ();
68
+ Context newContext = Vertx .vertx ().getOrCreateContext ();
72
69
Assertions .assertThat ( testVertxContext ).isNotEqualTo ( newContext );
73
70
74
71
// 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 ) ) )
85
75
);
86
76
}
87
77
88
78
@ Test
89
79
public void testOnPersistWithMutiny (TestContext testContext ) throws Exception {
80
+ Async async = testContext .async ();
90
81
Mutiny .Session session = openMutinySession ();
91
82
Context testVertxContext = Vertx .currentContext ();
92
83
93
84
// Create a different new context
94
- Vertx vertx = Vertx .vertx ();
95
- Context newContext = vertx .getOrCreateContext ();
85
+ Context newContext = Vertx .vertx ().getOrCreateContext ();
96
86
Assertions .assertThat ( testVertxContext ).isNotEqualTo ( newContext );
97
87
98
88
// 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 ) ) ) )
108
95
);
109
96
}
110
97
111
98
@ Test
112
99
public void testFindWithMutiny (TestContext testContext ) throws Exception {
100
+ Async async = testContext .async ();
113
101
Mutiny .Session session = openMutinySession ();
114
102
Context testVertxContext = Vertx .currentContext ();
115
103
116
104
// Create a different new context
117
- Vertx vertx = Vertx .vertx ();
118
- Context newContext = vertx .getOrCreateContext ();
105
+ Context newContext = Vertx .vertx ().getOrCreateContext ();
119
106
Assertions .assertThat ( testVertxContext ).isNotEqualTo ( newContext );
120
107
121
108
// 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 ) ) ) )
131
114
);
132
115
}
133
116
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
+
134
130
@ Entity
135
131
static class Competition {
136
132
@ Id
0 commit comments