Skip to content

Commit 49c6637

Browse files
committed
Fix broken unit tests.
Closes #2720
1 parent ff0503c commit 49c6637

File tree

1 file changed

+40
-50
lines changed

1 file changed

+40
-50
lines changed

src/test/java/org/springframework/data/redis/connection/lettuce/LettuceConnectionUnitTests.java

+40-50
Original file line numberDiff line numberDiff line change
@@ -24,22 +24,24 @@
2424
import io.lettuce.core.XClaimArgs;
2525
import io.lettuce.core.api.StatefulRedisConnection;
2626
import io.lettuce.core.api.async.RedisAsyncCommands;
27-
import io.lettuce.core.api.sync.RedisCommands;
2827
import io.lettuce.core.codec.ByteArrayCodec;
2928
import io.lettuce.core.codec.RedisCodec;
29+
import io.lettuce.core.codec.StringCodec;
3030
import io.lettuce.core.output.StatusOutput;
3131
import io.lettuce.core.protocol.AsyncCommand;
3232
import io.lettuce.core.protocol.Command;
3333
import io.lettuce.core.protocol.CommandArgs;
34+
import io.lettuce.core.protocol.CommandType;
3435

3536
import java.lang.reflect.InvocationTargetException;
3637
import java.time.Duration;
3738
import java.util.Collections;
3839

3940
import org.junit.jupiter.api.BeforeEach;
41+
import org.junit.jupiter.api.Disabled;
4042
import org.junit.jupiter.api.Test;
4143
import org.mockito.ArgumentCaptor;
42-
44+
import org.mockito.Mockito;
4345
import org.springframework.dao.InvalidDataAccessResourceUsageException;
4446
import org.springframework.data.redis.connection.AbstractConnectionUnitTestBase;
4547
import org.springframework.data.redis.connection.RedisServerCommands.ShutdownOption;
@@ -61,7 +63,6 @@ public static class BasicUnitTests extends AbstractConnectionUnitTestBase<RedisA
6163
private RedisClient clientMock;
6264
StatefulRedisConnection<byte[], byte[]> statefulConnectionMock;
6365
RedisAsyncCommands<byte[], byte[]> asyncCommandsMock;
64-
RedisCommands syncCommandsMock;
6566

6667
@SuppressWarnings({ "unchecked" })
6768
@BeforeEach
@@ -71,48 +72,57 @@ public void setUp() throws InvocationTargetException, IllegalAccessException {
7172
statefulConnectionMock = mock(StatefulRedisConnection.class);
7273
when(clientMock.connect((RedisCodec) any())).thenReturn(statefulConnectionMock);
7374

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+
});
7687

77-
when(statefulConnectionMock.async()).thenReturn(getNativeRedisConnectionMock());
78-
when(statefulConnectionMock.sync()).thenReturn(syncCommandsMock);
88+
when(statefulConnectionMock.async()).thenReturn(asyncCommandsMock);
7989
connection = new LettuceConnection(0, clientMock);
8090
}
8191

8292
@Test // DATAREDIS-184
8393
public void shutdownWithNullOptionsIsCalledCorrectly() {
8494

8595
connection.shutdown(null);
86-
verify(syncCommandsMock, times(1)).shutdown(true);
96+
verify(asyncCommandsMock).shutdown(true);
8797
}
8898

8999
@Test // DATAREDIS-184
90100
public void shutdownWithNosaveOptionIsCalledCorrectly() {
91101

92102
connection.shutdown(ShutdownOption.NOSAVE);
93-
verify(syncCommandsMock, times(1)).shutdown(false);
103+
verify(asyncCommandsMock).shutdown(false);
94104
}
95105

96106
@Test // DATAREDIS-184
97107
public void shutdownWithSaveOptionIsCalledCorrectly() {
98108

99109
connection.shutdown(ShutdownOption.SAVE);
100-
verify(syncCommandsMock, times(1)).shutdown(true);
110+
verify(asyncCommandsMock).shutdown(true);
101111
}
102112

103113
@Test // DATAREDIS-267
104114
public void killClientShouldDelegateCallCorrectly() {
105115

106116
String ipPort = "127.0.0.1:1001";
107117
connection.killClient("127.0.0.1", 1001);
108-
verify(syncCommandsMock, times(1)).clientKill(eq(ipPort));
118+
verify(asyncCommandsMock).clientKill(eq(ipPort));
109119
}
110120

111121
@Test // DATAREDIS-270
112122
public void getClientNameShouldSendRequestCorrectly() {
113123

114124
connection.getClientName();
115-
verify(syncCommandsMock, times(1)).clientGetname();
125+
verify(asyncCommandsMock).clientGetname();
116126
}
117127

118128
@Test // DATAREDIS-277
@@ -124,14 +134,14 @@ void replicaOfShouldThrowExectpionWhenCalledForNullHost() {
124134
public void replicaOfShouldBeSentCorrectly() {
125135

126136
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));
128138
}
129139

130140
@Test // DATAREDIS-277
131141
public void replicaOfNoOneShouldBeSentCorrectly() {
132142

133143
connection.replicaOfNoOne();
134-
verify(syncCommandsMock, times(1)).slaveofNoOne();
144+
verify(asyncCommandsMock).slaveofNoOne();
135145
}
136146

137147
@Test // DATAREDIS-348
@@ -147,32 +157,31 @@ void dbIndexShouldBeSetWhenObtainingConnection() {
147157
connection.select(1);
148158
connection.getNativeConnection();
149159

150-
verify(syncCommandsMock, times(1)).select(1);
160+
verify(asyncCommandsMock).dispatch(eq(CommandType.SELECT), any(), any());
151161
}
152162

153163
@Test // DATAREDIS-603
154164
void translatesUnknownExceptions() {
155165

156166
IllegalArgumentException exception = new IllegalArgumentException("Aw, snap");
157167

158-
when(syncCommandsMock.set(any(), any())).thenThrow(exception);
168+
when(asyncCommandsMock.set(any(), any())).thenThrow(exception);
159169
connection = new LettuceConnection(null, 0, clientMock, 1);
160170

161171
assertThatThrownBy(() -> connection.set("foo".getBytes(), "bar".getBytes()))
162-
.hasMessageContaining(exception.getMessage()).hasRootCause(exception);
172+
.hasRootCause(exception);
163173
}
164174

165175
@Test // DATAREDIS-603
166-
void translatesPipelineUnknownExceptions() throws Exception {
176+
void translatesPipelineUnknownExceptions() {
167177

168178
IllegalArgumentException exception = new IllegalArgumentException("Aw, snap");
169179

170180
when(asyncCommandsMock.set(any(byte[].class), any(byte[].class))).thenThrow(exception);
171181
connection = new LettuceConnection(null, 0, clientMock, 1);
172182
connection.openPipeline();
173183

174-
assertThatThrownBy(() -> connection.set("foo".getBytes(), "bar".getBytes()))
175-
.hasMessageContaining(exception.getMessage()).hasRootCause(exception);
184+
assertThatThrownBy(() -> connection.set("foo".getBytes(), "bar".getBytes())).hasRootCause(exception);
176185
}
177186

178187
@Test // DATAREDIS-1122
@@ -182,11 +191,7 @@ void xaddShouldHonorMaxlen() {
182191

183192
connection.streamCommands().xAdd(record, XAddOptions.maxlen(100));
184193
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());
190195

191196
assertThat(args.getValue()).extracting("maxlen").isEqualTo(100L);
192197
}
@@ -198,11 +203,7 @@ void xClaimShouldNotAddJustIdFlagToArgs() {
198203
XClaimOptions.minIdle(Duration.ofMillis(100)).ids("1-1"));
199204
ArgumentCaptor<XClaimArgs> args = ArgumentCaptor.forClass(XClaimArgs.class);
200205

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());
206207

207208
assertThat(ReflectionTestUtils.getField(args.getValue(), "justid")).isEqualTo(false);
208209
}
@@ -214,11 +215,7 @@ void xClaimJustIdShouldAddJustIdFlagToArgs() {
214215
XClaimOptions.minIdle(Duration.ofMillis(100)).ids("1-1"));
215216
ArgumentCaptor<XClaimArgs> args = ArgumentCaptor.forClass(XClaimArgs.class);
216217

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());
222219

223220
assertThat(ReflectionTestUtils.getField(args.getValue(), "justid")).isEqualTo(true);
224221
}
@@ -245,11 +242,7 @@ void xaddShouldHonorNoMkStream() {
245242

246243
connection.streamCommands().xAdd(record, XAddOptions.makeNoStream());
247244
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());
253246

254247
assertThat(args.getValue()).extracting("nomkstream").isEqualTo(true);
255248
}
@@ -266,53 +259,50 @@ public void setUp() throws InvocationTargetException, IllegalAccessException {
266259
}
267260

268261
@Test // DATAREDIS-528
262+
@Disabled("SHUTDOWN not supported in pipeline")
269263
public void shutdownWithSaveOptionIsCalledCorrectly() {
270264

271-
connection.shutdown(ShutdownOption.SAVE);
272-
verify(asyncCommandsMock, times(1)).shutdown(true);
273265
}
274266

275267
@Test // DATAREDIS-528
268+
@Disabled("SHUTDOWN not supported in pipeline")
276269
public void shutdownWithNosaveOptionIsCalledCorrectly() {
277270

278-
connection.shutdown(ShutdownOption.NOSAVE);
279-
verify(asyncCommandsMock, times(1)).shutdown(false);
280271
}
281272

282273
@Test // DATAREDIS-528
283274
public void replicaOfShouldBeSentCorrectly() {
284275

285276
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));
287278
}
288279

289280
@Test // DATAREDIS-528
281+
@Disabled("SHUTDOWN not supported in pipeline")
290282
public void shutdownWithNullOptionsIsCalledCorrectly() {
291283

292-
connection.shutdown(null);
293-
verify(asyncCommandsMock, times(1)).shutdown(true);
294284
}
295285

296286
@Test // DATAREDIS-528
297287
public void killClientShouldDelegateCallCorrectly() {
298288

299289
String ipPort = "127.0.0.1:1001";
300290
connection.killClient("127.0.0.1", 1001);
301-
verify(asyncCommandsMock, times(1)).clientKill(eq(ipPort));
291+
verify(asyncCommandsMock).clientKill(eq(ipPort));
302292
}
303293

304294
@Test // DATAREDIS-528
305295
public void replicaOfNoOneShouldBeSentCorrectly() {
306296

307297
connection.replicaOfNoOne();
308-
verify(asyncCommandsMock, times(1)).slaveofNoOne();
298+
verify(asyncCommandsMock).slaveofNoOne();
309299
}
310300

311301
@Test // DATAREDIS-528
312302
public void getClientNameShouldSendRequestCorrectly() {
313303

314304
connection.getClientName();
315-
verify(asyncCommandsMock, times(1)).clientGetname();
305+
verify(asyncCommandsMock).clientGetname();
316306
}
317307
}
318308
}

0 commit comments

Comments
 (0)