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