Skip to content

Commit

Permalink
Use String command representation in CommandLatencyId.equals/hashCode #…
Browse files Browse the repository at this point in the history
…1210

We now use the String representation of a command for equality and hashCode computation.
  • Loading branch information
mp911de committed Jan 21, 2020
1 parent 120d57d commit df06111
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 3 deletions.
8 changes: 5 additions & 3 deletions src/main/java/io/lettuce/core/metrics/CommandLatencyId.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ public class CommandLatencyId implements Serializable, Comparable<CommandLatency
private final SocketAddress localAddress;
private final SocketAddress remoteAddress;
private final ProtocolKeyword commandType;
private final String commandName;

protected CommandLatencyId(SocketAddress localAddress, SocketAddress remoteAddress, ProtocolKeyword commandType) {
LettuceAssert.notNull(localAddress, "LocalAddress must not be null");
Expand All @@ -42,6 +43,7 @@ protected CommandLatencyId(SocketAddress localAddress, SocketAddress remoteAddre
this.localAddress = localAddress;
this.remoteAddress = remoteAddress;
this.commandType = commandType;
this.commandName = commandType.name();
}

/**
Expand Down Expand Up @@ -96,14 +98,14 @@ public boolean equals(Object o) {
return false;
if (!remoteAddress.equals(that.remoteAddress))
return false;
return commandType.equals(that.commandType);
return commandName.equals(that.commandName);
}

@Override
public int hashCode() {
int result = localAddress.hashCode();
result = 31 * result + remoteAddress.hashCode();
result = 31 * result + commandType.hashCode();
result = 31 * result + commandName.hashCode();
return result;
}

Expand All @@ -124,7 +126,7 @@ public int compareTo(CommandLatencyId o) {
return localResult;
}

return commandType.toString().compareTo(o.commandType.toString());
return commandName.compareTo(o.commandName);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,12 @@
import org.junit.jupiter.api.Test;

import io.lettuce.core.protocol.CommandKeyword;
import io.lettuce.core.protocol.ProtocolKeyword;
import io.netty.channel.local.LocalAddress;

/**
* Unit tests for {@link CommandLatencyId}.
*
* @author Mark Paluch
*/
class CommandLatencyIdUnitTests {
Expand All @@ -39,4 +42,35 @@ void testValues() {
assertThat(sut.localAddress()).isEqualTo(LocalAddress.ANY);
assertThat(sut.remoteAddress()).isEqualTo(new LocalAddress("me"));
}

@Test
void testEquality() {
assertThat(sut).isEqualTo(CommandLatencyId.create(LocalAddress.ANY, new LocalAddress("me"), new MyCommand("ADDR")));
assertThat(sut).isNotEqualTo(CommandLatencyId.create(LocalAddress.ANY, new LocalAddress("me"), new MyCommand("FOO")));
}

@Test
void testHashCode() {
assertThat(sut)
.hasSameHashCodeAs(CommandLatencyId.create(LocalAddress.ANY, new LocalAddress("me"), new MyCommand("ADDR")));
}

static class MyCommand implements ProtocolKeyword {

final String name;

public MyCommand(String name) {
this.name = name;
}

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

@Override
public String name() {
return name;
}
}
}

0 comments on commit df06111

Please sign in to comment.