Skip to content

Commit

Permalink
Polishing #740
Browse files Browse the repository at this point in the history
Add test. Add author tag.

Original pull request: #741.
  • Loading branch information
mp911de committed Mar 24, 2018
1 parent bcf03ac commit 58f67db
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
* @author Mark Paluch
* @author Jongyeol Choi
* @author Grzegorz Szpak
* @author Nick Vollmar
*/
@ChannelHandler.Sharable
public class CommandHandler<K, V> extends ChannelDuplexHandler implements RedisChannelWriter<K, V> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,13 @@

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Fail.fail;
import static org.mockito.AdditionalMatchers.gt;
import static org.mockito.Matchers.any;
import static org.mockito.Mockito.*;

import java.io.IOException;
import java.net.Inet4Address;
import java.net.InetSocketAddress;
import java.util.*;
import java.util.concurrent.atomic.AtomicLong;

Expand All @@ -47,14 +50,14 @@
import com.lambdaworks.redis.RedisException;
import com.lambdaworks.redis.codec.StringCodec;
import com.lambdaworks.redis.codec.Utf8StringCodec;
import com.lambdaworks.redis.metrics.DefaultCommandLatencyCollector;
import com.lambdaworks.redis.metrics.DefaultCommandLatencyCollectorOptions;
import com.lambdaworks.redis.metrics.CommandLatencyCollector;
import com.lambdaworks.redis.output.StatusOutput;
import com.lambdaworks.redis.resource.ClientResources;

import edu.umd.cs.mtc.MultithreadedTestCase;
import edu.umd.cs.mtc.TestFramework;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.*;
import io.netty.handler.codec.EncoderException;
import io.netty.util.concurrent.Future;
Expand Down Expand Up @@ -93,6 +96,9 @@ public class CommandHandlerTest {
@Mock
private ChannelPromise promise;

@Mock
private CommandLatencyCollector latencyCollector;

@BeforeClass
public static void beforeClass() {
LoggerContext ctx = (LoggerContext) LogManager.getContext();
Expand All @@ -116,14 +122,16 @@ public void before() throws Exception {
when(context.channel()).thenReturn(channel);
when(channel.pipeline()).thenReturn(pipeline);
when(channel.eventLoop()).thenReturn(eventLoop);
when(channel.remoteAddress()).thenReturn(new InetSocketAddress(Inet4Address.getLocalHost(), 1234));
when(channel.localAddress()).thenReturn(new InetSocketAddress(Inet4Address.getLocalHost(), 1234));
when(eventLoop.submit(any(Runnable.class))).thenAnswer(invocation -> {
Runnable r = (Runnable) invocation.getArguments()[0];
r.run();
return null;
});

when(clientResources.commandLatencyCollector()).thenReturn(
new DefaultCommandLatencyCollector(DefaultCommandLatencyCollectorOptions.create()));
when(latencyCollector.isEnabled()).thenReturn(true);
when(clientResources.commandLatencyCollector()).thenReturn(latencyCollector);

when(channel.writeAndFlush(any())).thenAnswer(invocation -> {

Expand Down Expand Up @@ -575,6 +583,22 @@ public void shouldWriteActiveCommandsInMixedBatch() throws Exception {
.allMatch(o -> CommandWrapper.unwrap((RedisCommand) o) == command2);
}

@Test
public void shouldRecordCorrectFirstResponseLatency() throws Exception {

when(promise.isSuccess()).thenReturn(true);
sut.channelActive(context);

LatencyMeteredCommand<String, String, String> wrapped = new LatencyMeteredCommand<>(command);

sut.write(context, wrapped, promise);
Thread.sleep(10);

sut.channelRead(context, Unpooled.wrappedBuffer("*1\r\n+OK\r\n".getBytes()));

verify(latencyCollector).recordCommandLatency(any(), any(), eq(CommandType.APPEND), gt(0L), gt(0L));
}

@Test
public void shouldIgnoreNonReadableBuffers() throws Exception {

Expand Down

0 comments on commit 58f67db

Please sign in to comment.