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

Add Connection#getSocketString() method #3137

Closed
wants to merge 1 commit into from
Closed
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
12 changes: 12 additions & 0 deletions src/main/java/redis/clients/jedis/Connection.java
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,18 @@ public void close() {
}
}

/**
* This method simply returns {@link java.net.InetAddress} and port.
*
* @return 'address:port'
* @see Socket#getInetAddress()
* @see Socket#getPort()
*/
public String getSocketString() {
connect();
return "" + this.socket.getInetAddress() + ':' + this.socket.getPort();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
return "" + this.socket.getInetAddress() + ':' + this.socket.getPort();
return this.socket.getInetAddress() + ':' + this.socket.getPort();

}

public void disconnect() {
if (isConnected()) {
try {
Expand Down
10 changes: 10 additions & 0 deletions src/test/java/redis/clients/jedis/ConnectionTest.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package redis.clients.jedis;

import org.hamcrest.CoreMatchers;
import org.hamcrest.MatcherAssert;
import org.junit.After;
import org.junit.Test;

Expand Down Expand Up @@ -40,4 +42,12 @@ public void checkCloseable() {
client.connect();
client.close();
}

@Test
public void socketString() {
client = new Connection("127.0.0.1", 6379);
String socketString = client.getSocketString();
MatcherAssert.assertThat(socketString, CoreMatchers.containsString("127.0.0.1"));
MatcherAssert.assertThat(socketString, CoreMatchers.endsWith(":6379"));
}
}