Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Modify Connection.toIdentityString and test #3931

Merged
merged 1 commit into from
Aug 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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:"));
}
}
Loading