Skip to content

Commit

Permalink
Add some more unit tests. Polishing.
Browse files Browse the repository at this point in the history
  • Loading branch information
tishun committed Aug 5, 2024
1 parent 5a931c4 commit 843f8fd
Show file tree
Hide file tree
Showing 5 changed files with 103 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ public enum TrackingFlag {
* @return the resulting {@link TrackingFlag} or {@link IllegalArgumentException} if unrecognized
*/
public static TrackingFlag from(String flag) {
switch (flag) {
switch (flag.toLowerCase()) {
case "off":
return OFF;
case "on":
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package io.lettuce.core.cluster.models.tracking;

import io.lettuce.core.protocol.CommandKeyword;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
Expand Down Expand Up @@ -58,8 +60,9 @@ private static void verifyStructure(List<?> trackinginfoOutput) {
throw new IllegalArgumentException("trackinginfoOutput has wrong number of elements");
}

if (!trackinginfoOutput.get(0).equals("flags") || !trackinginfoOutput.get(2).equals("redirect")
|| !trackinginfoOutput.get(4).equals("prefixes")) {
if (!CommandKeyword.FLAGS.toString().equalsIgnoreCase(trackinginfoOutput.get(0).toString())
|| !CommandKeyword.REDIRECT.toString().equalsIgnoreCase(trackinginfoOutput.get(2).toString())
|| !CommandKeyword.PREFIXES.toString().equalsIgnoreCase(trackinginfoOutput.get(4).toString())) {
throw new IllegalArgumentException("trackinginfoOutput has unsupported argument order");
}

Expand Down
6 changes: 3 additions & 3 deletions src/main/java/io/lettuce/core/protocol/CommandKeyword.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,13 @@ public enum CommandKeyword implements ProtocolKeyword {

BY, BYLEX, BYSCORE, CACHING, CAT, CH, CHANNELS, COPY, COUNT, COUNTKEYSINSLOT, CONSUMERS, CREATE, DB, DELSLOTS, DELSLOTSRANGE, DELUSER, DESC, DRYRUN, SOFT, HARD, ENCODING,

FAILOVER, FORGET, FIELDS, FLUSH, FORCE, FREQ, FLUSHSLOTS, GENPASS, GETNAME, GETUSER, GETKEYSINSLOT, GETREDIR, GROUP, GROUPS, HTSTATS, ID, IDLE, INFO,
FAILOVER, FORGET, FIELDS, FLAGS, FLUSH, FORCE, FREQ, FLUSHSLOTS, GENPASS, GETNAME, GETUSER, GETKEYSINSLOT, GETREDIR, GROUP, GROUPS, HTSTATS, ID, IDLE, INFO,

IDLETIME, JUSTID, KILL, KEYSLOT, LEFT, LEN, LIMIT, LIST, LOAD, LOG, MATCH,

MAX, MAXLEN, MEET, MIN, MINID, MOVED, NO, NOACK, NOCOMMANDS, NODE, NODES, NOMKSTREAM, NOPASS, NOSAVE, NOT, NOVALUES, NUMSUB, SHARDCHANNELS, SHARDNUMSUB, NUMPAT, NX, OFF, ON, ONE, OR, PAUSE,
MAX, MAXLEN, MEET, MIN, MINID, MOVED, NO, NOACK, NOCOMMANDS, NODE, NODES, NOMKSTREAM, NOPASS, NOSAVE, NOT, NOVALUES, NUMSUB, SHARDCHANNELS, SHARDNUMSUB, NUMPAT, NX, OFF, ON, ONE, OR, PAUSE, PREFIXES,

REFCOUNT, REMOVE, RELOAD, REPLACE, REPLICATE, REPLICAS, REV, RESET, RESETCHANNELS, RESETKEYS, RESETPASS,
REFCOUNT, REMOVE, RELOAD, REPLACE, REDIRECT, REPLICATE, REPLICAS, REV, RESET, RESETCHANNELS, RESETKEYS, RESETPASS,

RESETSTAT, RESTART, RETRYCOUNT, REWRITE, RIGHT, SAVECONFIG, SDSLEN, SETINFO, SETNAME, SETSLOT, SHARDS, SLOTS, STABLE,

Expand Down
11 changes: 11 additions & 0 deletions src/test/java/io/lettuce/core/RedisCommandBuilderUnitTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -146,4 +146,15 @@ void shouldCorrectlyConstructHpttl() {
+ "3\r\n" + "$7\r\n" + "hField1\r\n" + "$7\r\n" + "hField2\r\n" + "$7\r\n" + "hField3\r\n");
}

@Test
void shouldCorrectlyConstructClientTrackinginfo() {

Command<String, String, ?> command = sut.clientTrackinginfo();
ByteBuf buf = Unpooled.directBuffer();
command.encode(buf);

assertThat(buf.toString(StandardCharsets.UTF_8))
.isEqualTo("*2\r\n" + "$6\r\n" + "CLIENT\r\n" + "$12\r\n" + "TRACKINGINFO\r\n");
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
package io.lettuce.core.cluster.models.tracking;

import io.lettuce.core.protocol.CommandKeyword;
import org.junit.jupiter.api.Test;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertThrows;

class TrackingInfoParserTest {

@Test
void parse() {
List<Object> input = new ArrayList<>();
input.add(CommandKeyword.FLAGS.toString());
input.add(Arrays.asList(TrackingInfo.TrackingFlag.ON.toString(), TrackingInfo.TrackingFlag.OPTIN.toString()));
input.add(CommandKeyword.REDIRECT.toString());
input.add(0L);
input.add(CommandKeyword.PREFIXES.toString());
input.add(new ArrayList<>());

TrackingInfo info = TrackingInfoParser.parse(input);

assertThat(info.getFlags()).contains(TrackingInfo.TrackingFlag.ON, TrackingInfo.TrackingFlag.OPTIN);
assertThat(info.getRedirect()).isEqualTo(0L);
assertThat(info.getPrefixes()).isEmpty();
}

@Test
void parseFailEmpty() {
List<Object> input = new ArrayList<>();

Exception exception = assertThrows(IllegalArgumentException.class, () -> {
TrackingInfo info = TrackingInfoParser.parse(input);
});
}

@Test
void parseFailNumberOfElements() {
List<Object> input = new ArrayList<>();
input.add(CommandKeyword.FLAGS.toString());
input.add(Arrays.asList(TrackingInfo.TrackingFlag.ON.toString(), TrackingInfo.TrackingFlag.OPTIN.toString()));
input.add(CommandKeyword.REDIRECT.toString());

Exception exception = assertThrows(IllegalArgumentException.class, () -> {
TrackingInfo info = TrackingInfoParser.parse(input);
});
}

@Test
void parseFailOrder() {
List<Object> input = new ArrayList<>();
input.add(CommandKeyword.FLAGS.toString());
input.add(Arrays.asList(TrackingInfo.TrackingFlag.ON.toString(), TrackingInfo.TrackingFlag.OPTIN.toString()));
input.add(CommandKeyword.PREFIXES.toString());
input.add(new ArrayList<>());
input.add(CommandKeyword.REDIRECT.toString());
input.add(0L);

Exception exception = assertThrows(IllegalArgumentException.class, () -> {
TrackingInfo info = TrackingInfoParser.parse(input);
});
}

@Test
void parseFailTypes() {
List<Object> input = new ArrayList<>();
input.add(CommandKeyword.FLAGS.toString());
input.add(Arrays.asList(TrackingInfo.TrackingFlag.ON.toString(), TrackingInfo.TrackingFlag.OPTIN.toString()));
input.add(CommandKeyword.REDIRECT.toString());
input.add(Boolean.FALSE);
input.add(CommandKeyword.PREFIXES.toString());
input.add(new ArrayList<>());

Exception exception = assertThrows(IllegalArgumentException.class, () -> {
TrackingInfo info = TrackingInfoParser.parse(input);
});
}

}

0 comments on commit 843f8fd

Please sign in to comment.