forked from fppt/jedis-mock
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Iван
committed
Dec 23, 2023
1 parent
5b4b7ea
commit 2145348
Showing
3 changed files
with
201 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
156 changes: 156 additions & 0 deletions
156
src/test/java/com/github/fppt/jedismock/comparisontests/streams/XReadTests.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,156 @@ | ||
package com.github.fppt.jedismock.comparisontests.streams; | ||
|
||
import com.github.fppt.jedismock.comparisontests.ComparisonBase; | ||
import org.junit.jupiter.api.AfterEach; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.TestTemplate; | ||
import org.junit.jupiter.api.extension.ExtendWith; | ||
import org.testcontainers.shaded.com.google.common.collect.ImmutableMap; | ||
import redis.clients.jedis.HostAndPort; | ||
import redis.clients.jedis.Jedis; | ||
import redis.clients.jedis.StreamEntryID; | ||
import redis.clients.jedis.exceptions.JedisDataException; | ||
import redis.clients.jedis.params.XAddParams; | ||
import redis.clients.jedis.params.XReadParams; | ||
import redis.clients.jedis.resps.StreamEntry; | ||
|
||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.concurrent.ExecutionException; | ||
import java.util.concurrent.ExecutorService; | ||
import java.util.concurrent.Executors; | ||
import java.util.concurrent.Future; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.assertj.core.api.Assertions.assertThatThrownBy; | ||
|
||
@ExtendWith(ComparisonBase.class) | ||
public class XReadTests { | ||
private ExecutorService blockingThread; | ||
private Jedis blockedClient; | ||
|
||
@BeforeEach | ||
public void setUp(Jedis jedis, HostAndPort hostAndPort) { | ||
jedis.flushAll(); | ||
blockedClient = new Jedis(hostAndPort.getHost(), hostAndPort.getPort()); | ||
blockingThread = Executors.newSingleThreadExecutor(); | ||
} | ||
|
||
@AfterEach | ||
public void tearDown() { | ||
blockedClient.close(); | ||
blockingThread.shutdownNow(); | ||
} | ||
|
||
@TestTemplate | ||
void blockingXREADforStreamThatRanDry(Jedis jedis) throws ExecutionException, InterruptedException { | ||
jedis.xadd("s", XAddParams.xAddParams().id("666"), ImmutableMap.of("a", "b")); | ||
jedis.xdel("s", new StreamEntryID(666)); | ||
|
||
jedis.xread(XReadParams.xReadParams().block(10),ImmutableMap.of("s", new StreamEntryID(665))); | ||
|
||
assertThatThrownBy( | ||
() -> jedis.xadd("s", XAddParams.xAddParams().id("665"), ImmutableMap.of("a", "b")) | ||
) | ||
.isInstanceOf(JedisDataException.class) | ||
.hasMessageMatching("ERR.*equal.*smaller.*"); | ||
|
||
assertThatThrownBy( | ||
() -> jedis.xadd("s", XAddParams.xAddParams().id("665"), ImmutableMap.of("a", "b")) | ||
) | ||
.isInstanceOf(JedisDataException.class) | ||
.hasMessageMatching("ERR.*equal.*smaller.*"); | ||
|
||
|
||
Future<?> future = blockingThread.submit(() -> { | ||
List<Map.Entry<String, List<StreamEntry>>> data = blockedClient.xread( | ||
XReadParams.xReadParams().block(0), | ||
ImmutableMap.of("s", new StreamEntryID(665)) | ||
); | ||
|
||
assertThat(data) | ||
.hasSize(1) | ||
.first() | ||
.extracting(Map.Entry::getValue) | ||
.asList() | ||
.hasSize(1) | ||
.first() | ||
.usingRecursiveComparison() | ||
.isEqualTo( | ||
new StreamEntry( | ||
new StreamEntryID(667), | ||
ImmutableMap.of("a", "b") | ||
) | ||
); | ||
}); | ||
|
||
jedis.xadd("s", XAddParams.xAddParams().id("667"), ImmutableMap.of("a", "b")); | ||
|
||
future.get(); | ||
} | ||
|
||
@TestTemplate | ||
void xaddWithDelShouldNotAwakeClient(Jedis jedis) throws ExecutionException, InterruptedException { | ||
Future<?> future = blockingThread.submit(() -> { | ||
List<Map.Entry<String, List<StreamEntry>>> data = blockedClient.xread( | ||
XReadParams.xReadParams().block(3000), | ||
ImmutableMap.of("s", StreamEntryID.LAST_ENTRY) | ||
); | ||
|
||
assertThat(data) | ||
.hasSize(1) | ||
.first() | ||
.extracting(Map.Entry::getValue) | ||
.asList() | ||
.hasSize(1) | ||
.first() | ||
.usingRecursiveComparison() | ||
.isEqualTo( | ||
new StreamEntry( | ||
new StreamEntryID(12), | ||
ImmutableMap.of("new", "123") | ||
) | ||
); | ||
}); | ||
|
||
jedis.xadd("s", XAddParams.xAddParams().id("11"), ImmutableMap.of("old", "123")); | ||
jedis.del("s"); | ||
jedis.xadd("s", XAddParams.xAddParams().id("12"), ImmutableMap.of("new", "123")); | ||
|
||
future.get(); | ||
} | ||
|
||
|
||
@TestTemplate | ||
void xaddWithDelAndLpushShouldNotAwakeClient(Jedis jedis) throws ExecutionException, InterruptedException { | ||
Future<?> future = blockingThread.submit(() -> { | ||
List<Map.Entry<String, List<StreamEntry>>> data = blockedClient.xread( | ||
XReadParams.xReadParams().block(7000), | ||
ImmutableMap.of("s", StreamEntryID.LAST_ENTRY) | ||
); | ||
|
||
assertThat(data) | ||
.hasSize(1) | ||
.first() | ||
.extracting(Map.Entry::getValue) | ||
.asList() | ||
.hasSize(1) | ||
.first() | ||
.usingRecursiveComparison() | ||
.isEqualTo( | ||
new StreamEntry( | ||
new StreamEntryID(12), | ||
ImmutableMap.of("new", "123") | ||
) | ||
); | ||
}); | ||
|
||
jedis.xadd("s", XAddParams.xAddParams().id("11"), ImmutableMap.of("old", "123")); | ||
jedis.del("s"); | ||
jedis.lpush("s", "foo", "bar"); | ||
jedis.del("s"); | ||
jedis.xadd("s", XAddParams.xAddParams().id("12"), ImmutableMap.of("new", "123")); | ||
|
||
future.get(); | ||
} | ||
} |