Skip to content

Do not cache resolved inet address #382

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

Merged
merged 1 commit into from
Jun 22, 2017
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
Original file line number Diff line number Diff line change
Expand Up @@ -103,20 +103,24 @@ public String toString()
return format( "%s:%d", host, port );
}

/**
* Create a {@link SocketAddress} from this bolt address. This method always attempts to resolve the hostname into
* an {@link InetAddress}.
*
* @return new socket address.
* @see InetSocketAddress
*/
public SocketAddress toSocketAddress()
{
if (socketAddress == null)
{
socketAddress = new InetSocketAddress( host, port );
}
return socketAddress;
return new InetSocketAddress( host, port );
}

/**
* Resolve the host name down to an IP address, if not already resolved.
*
* @return this instance if already resolved, otherwise a new address instance
* @throws UnknownHostException
* @throws UnknownHostException if no IP address for the host could be found
* @see InetAddress#getByName(String)
*/
public BoltServerAddress resolve() throws UnknownHostException
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,12 @@
package org.neo4j.driver.internal.net;

import org.junit.Test;
import org.neo4j.driver.internal.net.BoltServerAddress;

import java.net.SocketAddress;

import static org.hamcrest.CoreMatchers.equalTo;
import static org.junit.Assert.*;
import static org.junit.Assert.assertNotSame;
import static org.junit.Assert.assertThat;

public class BoltServerAddressTest
{
Expand All @@ -38,4 +40,14 @@ public void portShouldUseDefaultIfNotSupplied()
assertThat( new BoltServerAddress( "localhost" ).port(), equalTo( BoltServerAddress.DEFAULT_PORT ) );
}

}
@Test
public void shouldAlwaysResolveAddress()
{
BoltServerAddress boltAddress = new BoltServerAddress( "localhost" );

SocketAddress socketAddress1 = boltAddress.toSocketAddress();
SocketAddress socketAddress2 = boltAddress.toSocketAddress();

assertNotSame( socketAddress1, socketAddress2 );
}
}