Skip to content

Commit 55902c1

Browse files
committed
Polishing.
Remove static mocking to test actual behavior. Adapt tests. See #3346 Original pull request: #3347
1 parent 77e4a74 commit 55902c1

2 files changed

Lines changed: 18 additions & 32 deletions

File tree

src/main/java/org/springframework/data/redis/connection/lettuce/LettuceConnection.java

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -594,9 +594,7 @@ public void openPipeline() {
594594
try {
595595

596596
boolean done = LettuceFutures.awaitAll(timeout, TimeUnit.MILLISECONDS, futures.toArray(new RedisFuture[0]));
597-
598597
List<Object> results = new ArrayList<>(futures.size());
599-
600598
Exception problem = null;
601599

602600
if (done) {
@@ -645,9 +643,7 @@ public void openPipeline() {
645643

646644
if (problem != null) {
647645
throw new RedisPipelineException(problem, results);
648-
}
649-
650-
if (done) {
646+
} else if (done) {
651647
return results;
652648
}
653649

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

Lines changed: 17 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@
3939
import java.util.List;
4040
import java.util.Map;
4141
import java.util.Map.Entry;
42-
import java.util.concurrent.TimeUnit;
4342

4443
import org.junit.jupiter.api.BeforeEach;
4544
import org.junit.jupiter.api.Disabled;
@@ -49,7 +48,6 @@
4948
import org.mockito.MockedStatic;
5049
import org.mockito.Mockito;
5150

52-
import org.springframework.dao.InvalidDataAccessApiUsageException;
5351
import org.springframework.dao.InvalidDataAccessResourceUsageException;
5452
import org.springframework.dao.QueryTimeoutException;
5553
import org.springframework.data.redis.connection.AbstractConnectionUnitTestBase;
@@ -66,6 +64,8 @@
6664
import org.springframework.test.util.ReflectionTestUtils;
6765

6866
/**
67+
* Unit tests for {@link LettuceConnection}.
68+
*
6969
* @author Christoph Strobl
7070
* @author Mark Paluch
7171
*/
@@ -101,7 +101,7 @@ public void setUp() throws InvocationTargetException, IllegalAccessException {
101101

102102
when(statefulConnectionMock.async()).thenReturn(asyncCommandsMock);
103103
when(statefulConnectionMock.sync()).thenReturn(commandsMock);
104-
connection = new LettuceConnection(0, clientMock);
104+
connection = new LettuceConnection(1, clientMock);
105105
}
106106

107107
@Nested
@@ -433,21 +433,25 @@ protected void setOutput(ByteBuffer bytes) {
433433
}
434434

435435
@Nested
436-
class ClosePipelineUnitTests {
436+
class LettucePipelineConnectionUnitTests extends BasicUnitTests {
437+
438+
@BeforeEach
439+
public void setUp() throws InvocationTargetException, IllegalAccessException {
440+
connection.openPipeline();
441+
}
437442

438443
@Test // GH-3346
439444
void closePipelineShouldNotDoubleWrapTimeoutException() {
440445

446+
Command<?, ?, ?> cmd = new Command<>(CommandType.SET, new StatusOutput<>(ByteArrayCodec.INSTANCE));
447+
AsyncCommand<?, ?, ?> future = new AsyncCommand<>(cmd);
448+
449+
when(asyncCommandsMock.set(any(byte[].class), any(byte[].class))).thenReturn((RedisFuture) future);
441450
connection.openPipeline();
442451
connection.set("foo".getBytes(), "bar".getBytes());
443452

444-
try (MockedStatic<LettuceFutures> lf = Mockito.mockStatic(LettuceFutures.class)) {
445-
lf.when(() -> LettuceFutures.awaitAll(anyLong(), any(TimeUnit.class), any())).thenReturn(false);
446-
447-
assertThatThrownBy(() -> connection.closePipeline())
448-
.isInstanceOf(RedisPipelineException.class)
449-
.hasCauseInstanceOf(QueryTimeoutException.class);
450-
}
453+
assertThatThrownBy(() -> connection.closePipeline()).isInstanceOf(RedisPipelineException.class)
454+
.hasCauseInstanceOf(QueryTimeoutException.class);
451455
}
452456

453457
@SuppressWarnings({ "rawtypes", "unchecked" })
@@ -463,22 +467,8 @@ void closePipelineShouldNotDoubleWrapCommandException() {
463467
connection.openPipeline();
464468
connection.set("foo".getBytes(), "bar".getBytes());
465469

466-
try (MockedStatic<LettuceFutures> lf = Mockito.mockStatic(LettuceFutures.class)) {
467-
lf.when(() -> LettuceFutures.awaitAll(anyLong(), any(TimeUnit.class), any())).thenReturn(true);
468-
469-
assertThatThrownBy(() -> connection.closePipeline())
470-
.isInstanceOf(RedisPipelineException.class)
471-
.hasCauseInstanceOf(InvalidDataAccessApiUsageException.class);
472-
}
473-
}
474-
}
475-
476-
@Nested
477-
class LettucePipelineConnectionUnitTests extends BasicUnitTests {
478-
479-
@BeforeEach
480-
public void setUp() throws InvocationTargetException, IllegalAccessException {
481-
connection.openPipeline();
470+
assertThatThrownBy(() -> connection.closePipeline()).isInstanceOf(RedisPipelineException.class)
471+
.hasCauseExactlyInstanceOf(RedisException.class);
482472
}
483473

484474
@Test // DATAREDIS-528

0 commit comments

Comments
 (0)