Skip to content

Commit 1325ff8

Browse files
authored
Merge pull request #382 from lutovich/1.4-bolt-address-resolution
Do not cache resolved inet address
2 parents 89c842f + b88ccaf commit 1325ff8

File tree

2 files changed

+25
-9
lines changed

2 files changed

+25
-9
lines changed

driver/src/main/java/org/neo4j/driver/internal/net/BoltServerAddress.java

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -103,20 +103,24 @@ public String toString()
103103
return format( "%s:%d", host, port );
104104
}
105105

106+
/**
107+
* Create a {@link SocketAddress} from this bolt address. This method always attempts to resolve the hostname into
108+
* an {@link InetAddress}.
109+
*
110+
* @return new socket address.
111+
* @see InetSocketAddress
112+
*/
106113
public SocketAddress toSocketAddress()
107114
{
108-
if (socketAddress == null)
109-
{
110-
socketAddress = new InetSocketAddress( host, port );
111-
}
112-
return socketAddress;
115+
return new InetSocketAddress( host, port );
113116
}
114117

115118
/**
116119
* Resolve the host name down to an IP address, if not already resolved.
117120
*
118121
* @return this instance if already resolved, otherwise a new address instance
119-
* @throws UnknownHostException
122+
* @throws UnknownHostException if no IP address for the host could be found
123+
* @see InetAddress#getByName(String)
120124
*/
121125
public BoltServerAddress resolve() throws UnknownHostException
122126
{

driver/src/test/java/org/neo4j/driver/internal/net/BoltServerAddressTest.java

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,12 @@
1919
package org.neo4j.driver.internal.net;
2020

2121
import org.junit.Test;
22-
import org.neo4j.driver.internal.net.BoltServerAddress;
22+
23+
import java.net.SocketAddress;
2324

2425
import static org.hamcrest.CoreMatchers.equalTo;
25-
import static org.junit.Assert.*;
26+
import static org.junit.Assert.assertNotSame;
27+
import static org.junit.Assert.assertThat;
2628

2729
public class BoltServerAddressTest
2830
{
@@ -38,4 +40,14 @@ public void portShouldUseDefaultIfNotSupplied()
3840
assertThat( new BoltServerAddress( "localhost" ).port(), equalTo( BoltServerAddress.DEFAULT_PORT ) );
3941
}
4042

41-
}
43+
@Test
44+
public void shouldAlwaysResolveAddress()
45+
{
46+
BoltServerAddress boltAddress = new BoltServerAddress( "localhost" );
47+
48+
SocketAddress socketAddress1 = boltAddress.toSocketAddress();
49+
SocketAddress socketAddress2 = boltAddress.toSocketAddress();
50+
51+
assertNotSame( socketAddress1, socketAddress2 );
52+
}
53+
}

0 commit comments

Comments
 (0)