22
22
import java .sql .SQLException ;
23
23
import java .util .LinkedHashMap ;
24
24
import java .util .Map ;
25
- import java .util .Set ;
26
25
import java .util .stream .Stream ;
27
26
28
27
import javax .sql .DataSource ;
29
28
29
+ import org .assertj .core .api .InstanceOfAssertFactories ;
30
30
import org .junit .jupiter .api .AfterEach ;
31
31
import org .junit .jupiter .api .BeforeEach ;
32
32
import org .junit .jupiter .api .Test ;
@@ -68,6 +68,7 @@ class TransactionalStringRedisTemplateTests {
68
68
}
69
69
}
70
70
71
+ @ SuppressWarnings ({"rawtypes" , "unchecked" })
71
72
@ BeforeEach
72
73
void beforeEach () {
73
74
@@ -78,14 +79,14 @@ void beforeEach() {
78
79
stringTemplate .afterPropertiesSet ();
79
80
80
81
stringTemplate .execute ((RedisCallback ) con -> {
81
- con .flushDb ();
82
+ con .serverCommands (). flushDb ();
82
83
return null ;
83
84
});
84
85
}
85
86
86
87
@ AfterEach
87
88
void afterEach () {
88
- redisConnectionFactory .getConnection ().flushAll ();
89
+ redisConnectionFactory .getConnection ().serverCommands (). flushAll ();
89
90
}
90
91
91
92
@ Test // GH-3191
@@ -119,7 +120,6 @@ void visibilityDuringManagedTransaction() throws SQLException {
119
120
.containsEntry ("isMember(inside)" , false );
120
121
}
121
122
122
- @ SuppressWarnings ("unchecked" )
123
123
@ Test // GH-3187
124
124
void allRangeWithScoresMethodsShouldExecuteImmediatelyInTransaction () throws SQLException {
125
125
@@ -138,77 +138,55 @@ void allRangeWithScoresMethodsShouldExecuteImmediatelyInTransaction() throws SQL
138
138
Map <String , Object > ops = new LinkedHashMap <>();
139
139
140
140
// Query data added outside transaction (should execute immediately)
141
- ops .put ("rangeWithScores_before " ,
141
+ ops .put ("rangeWithScores_outside " ,
142
142
stringTemplate .opsForZSet ().rangeWithScores ("testzset" , 0 , -1 ));
143
- ops .put ("reverseRangeWithScores_before " ,
143
+ ops .put ("reverseRangeWithScores_outside " ,
144
144
stringTemplate .opsForZSet ().reverseRangeWithScores ("testzset" , 0 , -1 ));
145
- ops .put ("rangeByScoreWithScores_before " ,
145
+ ops .put ("rangeByScoreWithScores_outside " ,
146
146
stringTemplate .opsForZSet ().rangeByScoreWithScores ("testzset" , 1.0 , 2.0 ));
147
- ops .put ("reverseRangeByScoreWithScores_before " ,
147
+ ops .put ("reverseRangeByScoreWithScores_outside " ,
148
148
stringTemplate .opsForZSet ().reverseRangeByScoreWithScores ("testzset" , 1.0 , 2.0 ));
149
149
150
150
// Add inside transaction (goes into multi/exec queue)
151
- ops .put ("add_result " , stringTemplate .opsForZSet ().add ("testzset" , "inside" , 3.0 ));
151
+ ops .put ("add_inside " , stringTemplate .opsForZSet ().add ("testzset" , "inside" , 3.0 ));
152
152
153
153
// Changes made inside transaction should not be visible yet (read executes immediately)
154
- ops .put ("rangeWithScores_after " ,
154
+ ops .put ("rangeWithScores_inside " ,
155
155
stringTemplate .opsForZSet ().rangeWithScores ("testzset" , 0 , -1 ));
156
- ops .put ("reverseRangeWithScores_after " ,
156
+ ops .put ("reverseRangeWithScores_inside " ,
157
157
stringTemplate .opsForZSet ().reverseRangeWithScores ("testzset" , 0 , -1 ));
158
- ops .put ("rangeByScoreWithScores_after " ,
158
+ ops .put ("rangeByScoreWithScores_inside " ,
159
159
stringTemplate .opsForZSet ().rangeByScoreWithScores ("testzset" , 1.0 , 3.0 ));
160
- ops .put ("reverseRangeByScoreWithScores_after " ,
160
+ ops .put ("reverseRangeByScoreWithScores_inside " ,
161
161
stringTemplate .opsForZSet ().reverseRangeByScoreWithScores ("testzset" , 1.0 , 3.0 ));
162
162
163
163
return ops ;
164
164
});
165
165
166
166
// add result is null (no result until exec)
167
- assertThat (result ).containsEntry ("add_result" , null );
168
-
169
- // before: only data added outside transaction is visible
170
- assertThat ((Set <TypedTuple <String >>) result .get ("rangeWithScores_before" ))
171
- .hasSize (2 )
172
- .extracting (TypedTuple ::getValue )
173
- .containsExactly ("outside1" , "outside2" );
174
-
175
- assertThat ((Set <TypedTuple <String >>) result .get ("reverseRangeWithScores_before" ))
176
- .hasSize (2 )
177
- .extracting (TypedTuple ::getValue )
178
- .containsExactly ("outside2" , "outside1" );
179
-
180
- assertThat ((Set <TypedTuple <String >>) result .get ("rangeByScoreWithScores_before" ))
181
- .hasSize (2 )
182
- .extracting (TypedTuple ::getValue )
183
- .containsExactly ("outside1" , "outside2" );
184
-
185
- assertThat ((Set <TypedTuple <String >>) result .get ("reverseRangeByScoreWithScores_before" ))
186
- .hasSize (2 )
187
- .extracting (TypedTuple ::getValue )
188
- .containsExactly ("outside2" , "outside1" );
189
-
190
- // after: changes made inside transaction are still not visible
191
- assertThat ((Set <TypedTuple <String >>) result .get ("rangeWithScores_after" ))
192
- .hasSize (2 )
193
- .extracting (TypedTuple ::getValue )
194
- .containsExactly ("outside1" , "outside2" );
195
-
196
- assertThat ((Set <TypedTuple <String >>) result .get ("reverseRangeWithScores_after" ))
197
- .hasSize (2 )
198
- .extracting (TypedTuple ::getValue )
199
- .containsExactly ("outside2" , "outside1" );
200
-
201
- assertThat ((Set <TypedTuple <String >>) result .get ("rangeByScoreWithScores_after" ))
202
- .hasSize (2 )
203
- .extracting (TypedTuple ::getValue )
204
- .containsExactly ("outside1" , "outside2" );
205
-
206
- assertThat ((Set <TypedTuple <String >>) result .get ("reverseRangeByScoreWithScores_after" ))
207
- .hasSize (2 )
208
- .extracting (TypedTuple ::getValue )
209
- .containsExactly ("outside2" , "outside1" );
167
+ assertThat (result ).containsEntry ("add_inside" , null );
168
+
169
+ // changes made outside transaction are visible
170
+ assertThatResultForOperationContainsExactly (result , "rangeWithScores_outside" , "outside1" , "outside2" );
171
+ assertThatResultForOperationContainsExactly (result , "reverseRangeWithScores_outside" , "outside2" , "outside1" );
172
+ assertThatResultForOperationContainsExactly (result , "rangeByScoreWithScores_outside" , "outside1" , "outside2" );
173
+ assertThatResultForOperationContainsExactly (result , "reverseRangeByScoreWithScores_outside" , "outside2" , "outside1" );
174
+
175
+ // changes made inside transaction are not visible (i.e. a 3rd element was added but not detected in range op)
176
+ assertThatResultForOperationContainsExactly (result , "rangeWithScores_inside" , "outside1" , "outside2" );
177
+ assertThatResultForOperationContainsExactly (result , "reverseRangeWithScores_inside" , "outside2" , "outside1" );
178
+ assertThatResultForOperationContainsExactly (result , "rangeByScoreWithScores_inside" , "outside1" , "outside2" );
179
+ assertThatResultForOperationContainsExactly (result , "reverseRangeByScoreWithScores_inside" , "outside2" , "outside1" );
210
180
}
211
181
182
+ private void assertThatResultForOperationContainsExactly (Map <String , Object > result , String operation , String ... expectedValues ) {
183
+ assertThat (result .get (operation ))
184
+ .asInstanceOf (InstanceOfAssertFactories .set (TypedTuple .class ))
185
+ .hasSize (expectedValues .length )
186
+ .extracting (TypedTuple ::getValue )
187
+ .containsExactly (expectedValues );
188
+ }
189
+
212
190
static Stream <Arguments > argumentsStream () {
213
191
214
192
LettuceConnectionFactory lcf = new LettuceConnectionFactory (SettingsUtils .standaloneConfiguration ());
0 commit comments