Search in sources :

Example 1 with Command

use of io.lettuce.core.protocol.Command in project easeagent by megaease.

the class LettuceMetricInterceptorTest method getKey.

@Test
public void getKey() {
    LettuceMetricInterceptor lettuceMetricInterceptor = new LettuceMetricInterceptor();
    MethodInfo methodInfo = MethodInfo.builder().invoker("tttt").args(new Object[] { new Command(CommandKeyword.ADDR, null) }).build();
    String key = lettuceMetricInterceptor.getKey(methodInfo, null);
    assertEquals(CommandKeyword.ADDR.name(), key);
    methodInfo = MethodInfo.builder().invoker("tttt").args(new Object[] { Arrays.asList(new Command(CommandKeyword.ADDR, null), new Command(CommandKeyword.ADDR, null)) }).build();
    key = lettuceMetricInterceptor.getKey(methodInfo, null);
    assertEquals("[" + CommandKeyword.ADDR.name() + "," + CommandKeyword.ADDR.name() + "]", key);
}
Also used : RedisCommand(io.lettuce.core.protocol.RedisCommand) Command(io.lettuce.core.protocol.Command) MethodInfo(com.megaease.easeagent.plugin.interceptor.MethodInfo) Test(org.junit.Test)

Example 2 with Command

use of io.lettuce.core.protocol.Command in project lettuce-core by lettuce-io.

the class PubSubCommandHandlerUnitTests method shouldCompleteUnsubscribe.

@Test
void shouldCompleteUnsubscribe() throws Exception {
    Command<String, String, String> subCmd = new Command<>(CommandType.SUBSCRIBE, new PubSubOutput<>(StringCodec.UTF8), null);
    Command<String, String, String> unSubCmd = new Command<>(CommandType.UNSUBSCRIBE, new PubSubOutput<>(StringCodec.UTF8), null);
    doAnswer((Answer<PubSubEndpoint<String, String>>) inv -> {
        PubSubOutput<String, String> out = inv.getArgument(0);
        if (out.type() == PubSubOutput.Type.message) {
            throw new NullPointerException("Expected exception");
        }
        return endpoint;
    }).when(endpoint).notifyMessage(any());
    sut.channelRegistered(context);
    sut.channelActive(context);
    stack.add(subCmd);
    stack.add(unSubCmd);
    ByteBuf buf = responseBytes("*3\r\n$9\r\nsubscribe\r\n$10\r\ntest_sub_0\r\n:1\r\n" + "*3\r\n$7\r\nmessage\r\n$10\r\ntest_sub_0\r\n$3\r\nabc\r\n" + "*3\r\n$11\r\nunsubscribe\r\n$10\r\ntest_sub_0\r\n:0\r\n");
    sut.channelRead(context, buf);
    sut.channelRead(context, responseBytes("*3\r\n$7\r\nmessage\r\n$10\r\ntest_sub_1\r\n$3\r\nabc\r\n"));
    assertThat(unSubCmd.isDone()).isTrue();
}
Also used : Strictness(org.mockito.quality.Strictness) ClientResources(io.lettuce.core.resource.ClientResources) BeforeEach(org.junit.jupiter.api.BeforeEach) ArgumentMatchers(org.mockito.ArgumentMatchers) MockitoSettings(org.mockito.junit.jupiter.MockitoSettings) StringCodec(io.lettuce.core.codec.StringCodec) CommandType(io.lettuce.core.protocol.CommandType) Mock(org.mockito.Mock) ByteBufAllocator(io.netty.buffer.ByteBufAllocator) Command(io.lettuce.core.protocol.Command) Unpooled(io.netty.buffer.Unpooled) Answer(org.mockito.stubbing.Answer) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) StatusOutput(io.lettuce.core.output.StatusOutput) ArgumentCaptor(org.mockito.ArgumentCaptor) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) ByteBuf(io.netty.buffer.ByteBuf) ClientOptions(io.lettuce.core.ClientOptions) DefaultCommandLatencyCollectorOptions(io.lettuce.core.metrics.DefaultCommandLatencyCollectorOptions) Assertions(org.assertj.core.api.Assertions) DefaultCommandLatencyCollector(io.lettuce.core.metrics.DefaultCommandLatencyCollector) Tracing(io.lettuce.core.tracing.Tracing) ReflectionTestUtils(io.lettuce.test.ReflectionTestUtils) MockitoExtension(org.mockito.junit.jupiter.MockitoExtension) ChannelPipeline(io.netty.channel.ChannelPipeline) EventLoop(io.netty.channel.EventLoop) Test(org.junit.jupiter.api.Test) Channel(io.netty.channel.Channel) Mockito(org.mockito.Mockito) RedisCommand(io.lettuce.core.protocol.RedisCommand) ChannelConfig(io.netty.channel.ChannelConfig) Queue(java.util.Queue) Command(io.lettuce.core.protocol.Command) RedisCommand(io.lettuce.core.protocol.RedisCommand) ByteBuf(io.netty.buffer.ByteBuf) Test(org.junit.jupiter.api.Test)

Example 3 with Command

use of io.lettuce.core.protocol.Command in project lettuce-core by lettuce-io.

the class CliParser method parse.

/**
 * Parse a CLI command string into a {@link Command}.
 *
 * @param command
 * @return
 */
public static Command<String, String, List<Object>> parse(String command) {
    String[] parts = command.split(" ");
    boolean quoted = false;
    ProtocolKeyword type = null;
    CommandArgs<String, String> args = new CommandArgs<>(StringCodec.UTF8);
    StringBuilder buffer = new StringBuilder();
    for (int i = 0; i < parts.length; i++) {
        String part = parts[i];
        if (quoted && part.endsWith("\"")) {
            buffer.append(part, 0, part.length() - 1);
        } else if (part.startsWith("\"")) {
            quoted = true;
            buffer.append(buffer.append(part.substring(1)));
        } else {
            buffer.append(part);
        }
        if (quoted) {
            continue;
        }
        if (type == null) {
            String typeName = buffer.toString();
            type = new ProtocolKeyword() {

                @Override
                public byte[] getBytes() {
                    return name().getBytes(StandardCharsets.UTF_8);
                }

                @Override
                public String name() {
                    return typeName;
                }
            };
        } else {
            args.addKey(buffer.toString());
        }
        buffer.setLength(0);
    }
    return new Command<>(type, new ArrayOutput<>(StringCodec.UTF8), args);
}
Also used : CommandArgs(io.lettuce.core.protocol.CommandArgs) Command(io.lettuce.core.protocol.Command) ProtocolKeyword(io.lettuce.core.protocol.ProtocolKeyword)

Example 4 with Command

use of io.lettuce.core.protocol.Command in project lettuce-core by lettuce-io.

the class RedisCommandBuilder method configGet.

Command<K, V, Map<String, String>> configGet(String... parameters) {
    LettuceAssert.notNull(parameters, "Parameters " + MUST_NOT_BE_NULL);
    LettuceAssert.notEmpty(parameters, "Parameters " + MUST_NOT_BE_EMPTY);
    CommandArgs<String, String> args = new CommandArgs<>(StringCodec.UTF8).add(GET);
    for (String parameter : parameters) {
        args.add(parameter);
    }
    return Command.class.cast(new Command(CONFIG, new MapOutput<>(StringCodec.UTF8), args));
}
Also used : Command(io.lettuce.core.protocol.Command) RedisCommand(io.lettuce.core.protocol.RedisCommand)

Example 5 with Command

use of io.lettuce.core.protocol.Command in project lettuce-core by lettuce-io.

the class CustomCommandIntegrationTests method standaloneAsyncBatchTransaction.

@Test
void standaloneAsyncBatchTransaction() {
    RedisCommand<String, String, String> multi = new Command<>(CommandType.MULTI, new StatusOutput<>(StringCodec.UTF8));
    RedisCommand<String, String, String> set = new Command<>(CommandType.SET, new StatusOutput<>(StringCodec.UTF8), new CommandArgs<>(StringCodec.UTF8).addKey("key").add("value"));
    RedisCommand<String, String, TransactionResult> exec = new Command<>(CommandType.EXEC, null);
    AsyncCommand<String, String, String> async1 = new AsyncCommand<>(multi);
    AsyncCommand<String, String, String> async2 = new AsyncCommand<>(set);
    AsyncCommand<String, String, TransactionResult> async3 = new AsyncCommand<>(exec);
    getStandaloneConnection().dispatch(Arrays.asList(async1, async2, async3));
    assertThat(TestFutures.getOrTimeout(async1.toCompletableFuture())).isEqualTo("OK");
    assertThat(TestFutures.getOrTimeout(async2.toCompletableFuture())).isEqualTo("OK");
    TransactionResult transactionResult = TestFutures.getOrTimeout(async3.toCompletableFuture());
    assertThat(transactionResult.wasDiscarded()).isFalse();
    assertThat(transactionResult.<String>get(0)).isEqualTo("OK");
}
Also used : TransactionResult(io.lettuce.core.TransactionResult) Command(io.lettuce.core.protocol.Command) AsyncCommand(io.lettuce.core.protocol.AsyncCommand) RedisCommand(io.lettuce.core.protocol.RedisCommand) AsyncCommand(io.lettuce.core.protocol.AsyncCommand) Test(org.junit.jupiter.api.Test)

Aggregations

Command (io.lettuce.core.protocol.Command)9 RedisCommand (io.lettuce.core.protocol.RedisCommand)7 Test (org.junit.jupiter.api.Test)5 CommandType (io.lettuce.core.protocol.CommandType)4 BeforeEach (org.junit.jupiter.api.BeforeEach)4 StringCodec (io.lettuce.core.codec.StringCodec)3 ClientResources (io.lettuce.core.resource.ClientResources)3 Tracing (io.lettuce.core.tracing.Tracing)3 Unpooled (io.netty.buffer.Unpooled)3 List (java.util.List)3 Assertions (org.assertj.core.api.Assertions)3 ExtendWith (org.junit.jupiter.api.extension.ExtendWith)3 ArgumentMatchers (org.mockito.ArgumentMatchers)3 Mock (org.mockito.Mock)3 Mockito (org.mockito.Mockito)3 MockitoExtension (org.mockito.junit.jupiter.MockitoExtension)3 StatefulConnection (io.lettuce.core.api.StatefulConnection)2 CommandLatencyCollector (io.lettuce.core.metrics.CommandLatencyCollector)2 ValueListOutput (io.lettuce.core.output.ValueListOutput)2 AsyncCommand (io.lettuce.core.protocol.AsyncCommand)2