Skip to content

Commit

Permalink
Fix comment
Browse files Browse the repository at this point in the history
Add test
  • Loading branch information
thachlp committed Jun 8, 2024
1 parent 10ebbf4 commit e3080fd
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 75 deletions.
72 changes: 0 additions & 72 deletions src/main/java/io/lettuce/core/masterreplica/ReadOnlyCommands.java

This file was deleted.

4 changes: 2 additions & 2 deletions src/main/java/io/lettuce/core/protocol/ReadOnlyCommands.java
Original file line number Diff line number Diff line change
Expand Up @@ -71,10 +71,10 @@ public static ReadOnlyPredicate asPredicate() {

enum CommandName {
ASKING, BITCOUNT, BITPOS, CLIENT, COMMAND, DUMP, ECHO, EVAL_RO, EVALSHA_RO, EXISTS, FCALL_RO, //
GEODIST, GEOPOS, GEORADIUS, GEORADIUS_RO, GEORADIUSBYMEMBER, GEORADIUSBYMEMBER_RO, GEOSEARCH, GEOHASH, GET, GETBIT, //
GEODIST, GEOPOS, GEORADIUS_RO, GEORADIUSBYMEMBER_RO, GEOSEARCH, GEOHASH, GET, GETBIT, //
GETRANGE, HEXISTS, HGET, HGETALL, HKEYS, HLEN, HMGET, HRANDFIELD, HSCAN, HSTRLEN, //
HVALS, INFO, KEYS, LINDEX, LLEN, LPOS, LRANGE, SORT_RO, MGET, PFCOUNT, PTTL, //
RANDOMKEY, READWRITE, SCAN, SCARD, SCRIPT, //
RANDOMKEY, SCAN, SCARD, SCRIPT, //
SDIFF, SINTER, SISMEMBER, SMISMEMBER, SMEMBERS, SRANDMEMBER, SSCAN, STRLEN, //
SUNION, TIME, TTL, TYPE, //
XINFO, XLEN, XPENDING, XRANGE, XREVRANGE, XREAD, //
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class ClusterReadOnlyCommandsUnitTests {

@Test
void testCount() {
assertThat(ClusterReadOnlyCommands.getReadOnlyCommands()).hasSize(86);
assertThat(ClusterReadOnlyCommands.getReadOnlyCommands()).hasSize(83);
}

@Test
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package io.lettuce.core.commands;

import io.lettuce.core.TestSupport;
import io.lettuce.core.api.sync.RedisCommands;
import io.lettuce.core.cluster.ClusterReadOnlyCommands;
import io.lettuce.core.cluster.ClusterTestUtil;
import io.lettuce.core.cluster.api.StatefulRedisClusterConnection;
import io.lettuce.core.cluster.api.sync.RedisClusterCommands;
import io.lettuce.core.protocol.CommandType;
import io.lettuce.core.protocol.ProtocolKeyword;
import io.lettuce.core.protocol.ReadOnlyCommands;
import io.lettuce.test.KeyValueStreamingAdapter;
import io.lettuce.test.LettuceExtension;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestInstance;
import org.junit.jupiter.api.extension.ExtendWith;

import javax.inject.Inject;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import static org.assertj.core.api.Assertions.assertThat;

/**
* @author Thach Le
*/
@ExtendWith(LettuceExtension.class)
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
class ReadOnlyCommandIntegrationTests extends TestSupport {

private final RedisCommands<String, String> redis;

@Inject
protected ReadOnlyCommandIntegrationTests(RedisCommands<String, String> redis) {
this.redis = redis;
}

@BeforeEach
void setUp() {
redis.flushall();
}

@Test
void testReadOnlyCommands() {
for (ProtocolKeyword readOnlyCommand : ClusterReadOnlyCommands.getReadOnlyCommands()) {
assertThat(isCommandReadOnly(readOnlyCommand.name())).isTrue();
}
}

private boolean isCommandReadOnly(String commandName) {
List<Object> commandInfo = redis.commandInfo(commandName);
if (commandInfo == null || commandInfo.isEmpty()) {
throw new IllegalArgumentException("Command not found: " + commandName);
}

List<Object> flags = (List<Object>) commandInfo.get(2); // Index 2 is the flags list
return flags.contains("readonly") && !flags.contains("write");
}
}

0 comments on commit e3080fd

Please sign in to comment.