Skip to content

Commit

Permalink
Modify Connection.toIdentityString and test (#3931)
Browse files Browse the repository at this point in the history
  • Loading branch information
sazzad16 authored Aug 22, 2024
1 parent a8983db commit 2a43ab5
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 47 deletions.
56 changes: 13 additions & 43 deletions src/main/java/redis/clients/jedis/Connection.java
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public Connection(final JedisSocketFactory socketFactory, JedisClientConfig clie

@Override
public String toString() {
return "Connection{" + socketFactory + "}";
return getClass().getSimpleName() + "{" + socketFactory + "}";
}

@Experimental
Expand All @@ -82,52 +82,22 @@ public String toIdentityString() {
return strVal;
}

String className = getClass().getSimpleName();
int id = hashCode();
String classInfo = getClass().toString();

if (socket == null) {
StringBuilder buf = new StringBuilder(56)
.append("[")
.append(classInfo)
.append(", id: 0x")
.append(id)
.append(']');
return buf.toString();
return String.format("%s{id: 0x%X}", className, id);
}

SocketAddress remoteAddr = socket.getRemoteSocketAddress();
SocketAddress localAddr = socket.getLocalSocketAddress();
if (remoteAddr != null) {
StringBuilder buf = new StringBuilder(101)
.append("[")
.append(classInfo)
.append(", id: 0x")
.append(id)
.append(", L:")
.append(localAddr)
.append(broken? " ! " : " - ")
.append("R:")
.append(remoteAddr)
.append(']');
strVal = buf.toString();
strVal = String.format("%s{id: 0x%X, L:%s %c R:%s}", className, id,
localAddr, (broken ? '!' : '-'), remoteAddr);
} else if (localAddr != null) {
StringBuilder buf = new StringBuilder(64)
.append("[")
.append(classInfo)
.append(", id: 0x")
.append(id)
.append(", L:")
.append(localAddr)
.append(']');
strVal = buf.toString();
strVal = String.format("%s{id: 0x%X, L:%s}", className, id, localAddr);
} else {
StringBuilder buf = new StringBuilder(56)
.append("[")
.append(classInfo)
.append(", id: 0x")
.append(id)
.append(']');
strVal = buf.toString();
strVal = String.format("%s{id: 0x%X}", className, id);
}

strValActive = broken;
Expand Down Expand Up @@ -156,7 +126,7 @@ public void setSoTimeout(int soTimeout) {
try {
this.socket.setSoTimeout(soTimeout);
} catch (SocketException ex) {
broken = true;
setBroken();
throw new JedisConnectionException(ex);
}
}
Expand All @@ -169,7 +139,7 @@ public void setTimeoutInfinite() {
}
socket.setSoTimeout(infiniteSoTimeout);
} catch (SocketException ex) {
broken = true;
setBroken();
throw new JedisConnectionException(ex);
}
}
Expand All @@ -178,7 +148,7 @@ public void rollbackTimeout() {
try {
socket.setSoTimeout(this.soTimeout);
} catch (SocketException ex) {
broken = true;
setBroken();
throw new JedisConnectionException(ex);
}
}
Expand Down Expand Up @@ -245,7 +215,7 @@ public void sendCommand(final CommandArguments args) {
*/
}
// Any other exceptions related to connection?
broken = true;
setBroken();
throw ex;
}
}
Expand Down Expand Up @@ -398,7 +368,7 @@ protected void flush() {
try {
outputStream.flush();
} catch (IOException ex) {
broken = true;
setBroken();
throw new JedisConnectionException(ex);
}
}
Expand All @@ -414,7 +384,7 @@ protected Object readProtocolWithCheckingBroken() {
// System.out.println(redis.clients.jedis.util.SafeEncoder.encodeObject(read));
// return read;
} catch (JedisConnectionException exc) {
broken = true;
setBroken();
throw exc;
}
}
Expand Down
28 changes: 24 additions & 4 deletions src/test/java/redis/clients/jedis/ConnectionTest.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package redis.clients.jedis;

import static org.hamcrest.MatcherAssert.assertThat;

import org.hamcrest.Matchers;
import org.junit.After;
import org.junit.Assert;
import org.junit.Test;

import redis.clients.jedis.exceptions.JedisConnectionException;
Expand Down Expand Up @@ -45,10 +47,28 @@ public void checkCloseable() {
@Test
public void checkIdentityString() {
client = new Connection("localhost", 6379);
Assert.assertFalse(client.toIdentityString().contains("6379"));

String idString = "id: 0x" + Integer.toHexString(client.hashCode()).toUpperCase();

String identityString = client.toIdentityString();
assertThat(identityString, Matchers.startsWith("Connection{"));
assertThat(identityString, Matchers.endsWith("}"));
assertThat(identityString, Matchers.containsString(idString));

client.connect();
Assert.assertTrue(client.toIdentityString().contains("6379"));
identityString = client.toIdentityString();
assertThat(identityString, Matchers.startsWith("Connection{"));
assertThat(identityString, Matchers.endsWith("}"));
assertThat(identityString, Matchers.containsString(idString));
assertThat(identityString, Matchers.containsString(", L:"));
assertThat(identityString, Matchers.containsString(" - R:"));

client.close();
Assert.assertTrue(client.toIdentityString().contains("6379"));
identityString = client.toIdentityString();
assertThat(identityString, Matchers.startsWith("Connection{"));
assertThat(identityString, Matchers.endsWith("}"));
assertThat(identityString, Matchers.containsString(idString));
assertThat(identityString, Matchers.containsString(", L:"));
assertThat(identityString, Matchers.containsString(" ! R:"));
}
}

0 comments on commit 2a43ab5

Please sign in to comment.