Skip to content

Commit

Permalink
Merge pull request #382 from lutovich/1.4-bolt-address-resolution
Browse files Browse the repository at this point in the history
Do not cache resolved inet address
  • Loading branch information
zhenlineo authored Jun 22, 2017
2 parents 89c842f + b88ccaf commit 1325ff8
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 9 deletions.
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 );
}
}

0 comments on commit 1325ff8

Please sign in to comment.