24
24
import io .lettuce .core .XClaimArgs ;
25
25
import io .lettuce .core .api .StatefulRedisConnection ;
26
26
import io .lettuce .core .api .async .RedisAsyncCommands ;
27
- import io .lettuce .core .api .sync .RedisCommands ;
28
27
import io .lettuce .core .codec .ByteArrayCodec ;
29
28
import io .lettuce .core .codec .RedisCodec ;
29
+ import io .lettuce .core .codec .StringCodec ;
30
30
import io .lettuce .core .output .StatusOutput ;
31
31
import io .lettuce .core .protocol .AsyncCommand ;
32
32
import io .lettuce .core .protocol .Command ;
33
33
import io .lettuce .core .protocol .CommandArgs ;
34
+ import io .lettuce .core .protocol .CommandType ;
34
35
35
36
import java .lang .reflect .InvocationTargetException ;
36
37
import java .time .Duration ;
37
38
import java .util .Collections ;
38
39
39
40
import org .junit .jupiter .api .BeforeEach ;
41
+ import org .junit .jupiter .api .Disabled ;
40
42
import org .junit .jupiter .api .Test ;
41
43
import org .mockito .ArgumentCaptor ;
42
-
44
+ import org . mockito . Mockito ;
43
45
import org .springframework .dao .InvalidDataAccessResourceUsageException ;
44
46
import org .springframework .data .redis .connection .AbstractConnectionUnitTestBase ;
45
47
import org .springframework .data .redis .connection .RedisServerCommands .ShutdownOption ;
@@ -61,7 +63,6 @@ public static class BasicUnitTests extends AbstractConnectionUnitTestBase<RedisA
61
63
private RedisClient clientMock ;
62
64
StatefulRedisConnection <byte [], byte []> statefulConnectionMock ;
63
65
RedisAsyncCommands <byte [], byte []> asyncCommandsMock ;
64
- RedisCommands syncCommandsMock ;
65
66
66
67
@ SuppressWarnings ({ "unchecked" })
67
68
@ BeforeEach
@@ -71,48 +72,57 @@ public void setUp() throws InvocationTargetException, IllegalAccessException {
71
72
statefulConnectionMock = mock (StatefulRedisConnection .class );
72
73
when (clientMock .connect ((RedisCodec ) any ())).thenReturn (statefulConnectionMock );
73
74
74
- asyncCommandsMock = getNativeRedisConnectionMock ();
75
- syncCommandsMock = mock (RedisCommands .class );
75
+ asyncCommandsMock = Mockito .mock (RedisAsyncCommands .class , invocation -> {
76
+
77
+ if (invocation .getMethod ().getReturnType ().equals (RedisFuture .class )) {
78
+
79
+ Command <?, ?, ?> cmd = new Command <>(CommandType .PING , new StatusOutput <>(StringCodec .UTF8 ));
80
+ AsyncCommand <?, ?, ?> async = new AsyncCommand <>(cmd );
81
+ async .complete ();
82
+
83
+ return async ;
84
+ }
85
+ return null ;
86
+ });
76
87
77
- when (statefulConnectionMock .async ()).thenReturn (getNativeRedisConnectionMock ());
78
- when (statefulConnectionMock .sync ()).thenReturn (syncCommandsMock );
88
+ when (statefulConnectionMock .async ()).thenReturn (asyncCommandsMock );
79
89
connection = new LettuceConnection (0 , clientMock );
80
90
}
81
91
82
92
@ Test // DATAREDIS-184
83
93
public void shutdownWithNullOptionsIsCalledCorrectly () {
84
94
85
95
connection .shutdown (null );
86
- verify (syncCommandsMock , times ( 1 ) ).shutdown (true );
96
+ verify (asyncCommandsMock ).shutdown (true );
87
97
}
88
98
89
99
@ Test // DATAREDIS-184
90
100
public void shutdownWithNosaveOptionIsCalledCorrectly () {
91
101
92
102
connection .shutdown (ShutdownOption .NOSAVE );
93
- verify (syncCommandsMock , times ( 1 ) ).shutdown (false );
103
+ verify (asyncCommandsMock ).shutdown (false );
94
104
}
95
105
96
106
@ Test // DATAREDIS-184
97
107
public void shutdownWithSaveOptionIsCalledCorrectly () {
98
108
99
109
connection .shutdown (ShutdownOption .SAVE );
100
- verify (syncCommandsMock , times ( 1 ) ).shutdown (true );
110
+ verify (asyncCommandsMock ).shutdown (true );
101
111
}
102
112
103
113
@ Test // DATAREDIS-267
104
114
public void killClientShouldDelegateCallCorrectly () {
105
115
106
116
String ipPort = "127.0.0.1:1001" ;
107
117
connection .killClient ("127.0.0.1" , 1001 );
108
- verify (syncCommandsMock , times ( 1 ) ).clientKill (eq (ipPort ));
118
+ verify (asyncCommandsMock ).clientKill (eq (ipPort ));
109
119
}
110
120
111
121
@ Test // DATAREDIS-270
112
122
public void getClientNameShouldSendRequestCorrectly () {
113
123
114
124
connection .getClientName ();
115
- verify (syncCommandsMock , times ( 1 ) ).clientGetname ();
125
+ verify (asyncCommandsMock ).clientGetname ();
116
126
}
117
127
118
128
@ Test // DATAREDIS-277
@@ -124,14 +134,14 @@ void replicaOfShouldThrowExectpionWhenCalledForNullHost() {
124
134
public void replicaOfShouldBeSentCorrectly () {
125
135
126
136
connection .replicaOf ("127.0.0.1" , 1001 );
127
- verify (syncCommandsMock , times ( 1 ) ).slaveof (eq ("127.0.0.1" ), eq (1001 ));
137
+ verify (asyncCommandsMock ).slaveof (eq ("127.0.0.1" ), eq (1001 ));
128
138
}
129
139
130
140
@ Test // DATAREDIS-277
131
141
public void replicaOfNoOneShouldBeSentCorrectly () {
132
142
133
143
connection .replicaOfNoOne ();
134
- verify (syncCommandsMock , times ( 1 ) ).slaveofNoOne ();
144
+ verify (asyncCommandsMock ).slaveofNoOne ();
135
145
}
136
146
137
147
@ Test // DATAREDIS-348
@@ -147,32 +157,31 @@ void dbIndexShouldBeSetWhenObtainingConnection() {
147
157
connection .select (1 );
148
158
connection .getNativeConnection ();
149
159
150
- verify (syncCommandsMock , times ( 1 )). select ( 1 );
160
+ verify (asyncCommandsMock ). dispatch ( eq ( CommandType . SELECT ), any (), any () );
151
161
}
152
162
153
163
@ Test // DATAREDIS-603
154
164
void translatesUnknownExceptions () {
155
165
156
166
IllegalArgumentException exception = new IllegalArgumentException ("Aw, snap" );
157
167
158
- when (syncCommandsMock .set (any (), any ())).thenThrow (exception );
168
+ when (asyncCommandsMock .set (any (), any ())).thenThrow (exception );
159
169
connection = new LettuceConnection (null , 0 , clientMock , 1 );
160
170
161
171
assertThatThrownBy (() -> connection .set ("foo" .getBytes (), "bar" .getBytes ()))
162
- .hasMessageContaining ( exception . getMessage ()). hasRootCause (exception );
172
+ .hasRootCause (exception );
163
173
}
164
174
165
175
@ Test // DATAREDIS-603
166
- void translatesPipelineUnknownExceptions () throws Exception {
176
+ void translatesPipelineUnknownExceptions () {
167
177
168
178
IllegalArgumentException exception = new IllegalArgumentException ("Aw, snap" );
169
179
170
180
when (asyncCommandsMock .set (any (byte [].class ), any (byte [].class ))).thenThrow (exception );
171
181
connection = new LettuceConnection (null , 0 , clientMock , 1 );
172
182
connection .openPipeline ();
173
183
174
- assertThatThrownBy (() -> connection .set ("foo" .getBytes (), "bar" .getBytes ()))
175
- .hasMessageContaining (exception .getMessage ()).hasRootCause (exception );
184
+ assertThatThrownBy (() -> connection .set ("foo" .getBytes (), "bar" .getBytes ())).hasRootCause (exception );
176
185
}
177
186
178
187
@ Test // DATAREDIS-1122
@@ -182,11 +191,7 @@ void xaddShouldHonorMaxlen() {
182
191
183
192
connection .streamCommands ().xAdd (record , XAddOptions .maxlen (100 ));
184
193
ArgumentCaptor <XAddArgs > args = ArgumentCaptor .forClass (XAddArgs .class );
185
- if (connection .isPipelined ()) {
186
- verify (asyncCommandsMock , times (1 )).xadd (any (), args .capture (), anyMap ());
187
- } else {
188
- verify (syncCommandsMock , times (1 )).xadd (any (), args .capture (), anyMap ());
189
- }
194
+ verify (asyncCommandsMock ).xadd (any (), args .capture (), anyMap ());
190
195
191
196
assertThat (args .getValue ()).extracting ("maxlen" ).isEqualTo (100L );
192
197
}
@@ -198,11 +203,7 @@ void xClaimShouldNotAddJustIdFlagToArgs() {
198
203
XClaimOptions .minIdle (Duration .ofMillis (100 )).ids ("1-1" ));
199
204
ArgumentCaptor <XClaimArgs > args = ArgumentCaptor .forClass (XClaimArgs .class );
200
205
201
- if (connection .isPipelined ()) {
202
- verify (asyncCommandsMock ).xclaim (any (), any (), args .capture (), any ());
203
- } else {
204
- verify (syncCommandsMock ).xclaim (any (), any (), args .capture (), any ());
205
- }
206
+ verify (asyncCommandsMock ).xclaim (any (), any (), args .capture (), any ());
206
207
207
208
assertThat (ReflectionTestUtils .getField (args .getValue (), "justid" )).isEqualTo (false );
208
209
}
@@ -214,11 +215,7 @@ void xClaimJustIdShouldAddJustIdFlagToArgs() {
214
215
XClaimOptions .minIdle (Duration .ofMillis (100 )).ids ("1-1" ));
215
216
ArgumentCaptor <XClaimArgs > args = ArgumentCaptor .forClass (XClaimArgs .class );
216
217
217
- if (connection .isPipelined ()) {
218
- verify (asyncCommandsMock ).xclaim (any (), any (), args .capture (), any ());
219
- } else {
220
- verify (syncCommandsMock ).xclaim (any (), any (), args .capture (), any ());
221
- }
218
+ verify (asyncCommandsMock ).xclaim (any (), any (), args .capture (), any ());
222
219
223
220
assertThat (ReflectionTestUtils .getField (args .getValue (), "justid" )).isEqualTo (true );
224
221
}
@@ -245,11 +242,7 @@ void xaddShouldHonorNoMkStream() {
245
242
246
243
connection .streamCommands ().xAdd (record , XAddOptions .makeNoStream ());
247
244
ArgumentCaptor <XAddArgs > args = ArgumentCaptor .forClass (XAddArgs .class );
248
- if (connection .isPipelined ()) {
249
- verify (asyncCommandsMock , times (1 )).xadd (any (), args .capture (), anyMap ());
250
- } else {
251
- verify (syncCommandsMock , times (1 )).xadd (any (), args .capture (), anyMap ());
252
- }
245
+ verify (asyncCommandsMock ).xadd (any (), args .capture (), anyMap ());
253
246
254
247
assertThat (args .getValue ()).extracting ("nomkstream" ).isEqualTo (true );
255
248
}
@@ -266,53 +259,50 @@ public void setUp() throws InvocationTargetException, IllegalAccessException {
266
259
}
267
260
268
261
@ Test // DATAREDIS-528
262
+ @ Disabled ("SHUTDOWN not supported in pipeline" )
269
263
public void shutdownWithSaveOptionIsCalledCorrectly () {
270
264
271
- connection .shutdown (ShutdownOption .SAVE );
272
- verify (asyncCommandsMock , times (1 )).shutdown (true );
273
265
}
274
266
275
267
@ Test // DATAREDIS-528
268
+ @ Disabled ("SHUTDOWN not supported in pipeline" )
276
269
public void shutdownWithNosaveOptionIsCalledCorrectly () {
277
270
278
- connection .shutdown (ShutdownOption .NOSAVE );
279
- verify (asyncCommandsMock , times (1 )).shutdown (false );
280
271
}
281
272
282
273
@ Test // DATAREDIS-528
283
274
public void replicaOfShouldBeSentCorrectly () {
284
275
285
276
connection .replicaOf ("127.0.0.1" , 1001 );
286
- verify (asyncCommandsMock , times ( 1 ) ).slaveof (eq ("127.0.0.1" ), eq (1001 ));
277
+ verify (asyncCommandsMock ).slaveof (eq ("127.0.0.1" ), eq (1001 ));
287
278
}
288
279
289
280
@ Test // DATAREDIS-528
281
+ @ Disabled ("SHUTDOWN not supported in pipeline" )
290
282
public void shutdownWithNullOptionsIsCalledCorrectly () {
291
283
292
- connection .shutdown (null );
293
- verify (asyncCommandsMock , times (1 )).shutdown (true );
294
284
}
295
285
296
286
@ Test // DATAREDIS-528
297
287
public void killClientShouldDelegateCallCorrectly () {
298
288
299
289
String ipPort = "127.0.0.1:1001" ;
300
290
connection .killClient ("127.0.0.1" , 1001 );
301
- verify (asyncCommandsMock , times ( 1 ) ).clientKill (eq (ipPort ));
291
+ verify (asyncCommandsMock ).clientKill (eq (ipPort ));
302
292
}
303
293
304
294
@ Test // DATAREDIS-528
305
295
public void replicaOfNoOneShouldBeSentCorrectly () {
306
296
307
297
connection .replicaOfNoOne ();
308
- verify (asyncCommandsMock , times ( 1 ) ).slaveofNoOne ();
298
+ verify (asyncCommandsMock ).slaveofNoOne ();
309
299
}
310
300
311
301
@ Test // DATAREDIS-528
312
302
public void getClientNameShouldSendRequestCorrectly () {
313
303
314
304
connection .getClientName ();
315
- verify (asyncCommandsMock , times ( 1 ) ).clientGetname ();
305
+ verify (asyncCommandsMock ).clientGetname ();
316
306
}
317
307
}
318
308
}
0 commit comments