Skip to content

Commit fbdfd00

Browse files
authored
Merge pull request #304 from zhenlineo/1.1-tls-connection-error
Tls connections throw ServiceUnavaliable after restart of server
2 parents a1f737a + 637be40 commit fbdfd00

File tree

2 files changed

+27
-8
lines changed

2 files changed

+27
-8
lines changed

driver/src/main/java/org/neo4j/driver/internal/security/TLSSocketChannel.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import org.neo4j.driver.v1.Logger;
3131
import org.neo4j.driver.internal.util.BytePrinter;
3232
import org.neo4j.driver.v1.exceptions.ClientException;
33+
import org.neo4j.driver.v1.exceptions.ServiceUnavailableException;
3334

3435
import static javax.net.ssl.SSLEngineResult.HandshakeStatus.FINISHED;
3536
import static javax.net.ssl.SSLEngineResult.HandshakeStatus.NOT_HANDSHAKING;
@@ -163,7 +164,8 @@ private HandshakeStatus unwrap( ByteBuffer buffer ) throws IOException
163164
*/
164165
if ( channel.read( cipherIn ) < 0 )
165166
{
166-
throw new ClientException( "SSL Connection terminated while receiving data. " +
167+
throw new ServiceUnavailableException(
168+
"SSL Connection terminated while receiving data. " +
167169
"This can happen due to network instabilities, or due to restarts of the database." );
168170
}
169171
cipherIn.flip();

driver/src/test/java/org/neo4j/driver/v1/integration/ServerKilledIT.java

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,12 @@
2020

2121
import org.junit.Rule;
2222
import org.junit.Test;
23+
import org.junit.runner.RunWith;
24+
import org.junit.runners.Parameterized;
25+
import org.junit.runners.Parameterized.Parameters;
2326

27+
import java.util.Arrays;
28+
import java.util.Collection;
2429
import java.util.List;
2530
import java.util.concurrent.TimeUnit;
2631

@@ -43,19 +48,33 @@
4348
* Mainly concerned about the connection pool - we want to make sure that bad connections are evacuated from the
4449
* pool properly if the server dies, or all connections are lost for some other reason.
4550
*/
51+
@RunWith(Parameterized.class)
4652
public class ServerKilledIT
4753
{
4854
@Rule
4955
public TestNeo4j neo4j = new TestNeo4j();
5056

57+
@Parameters(name = "{0} connections")
58+
public static Collection<Object[]> data() {
59+
return Arrays.asList(new Object[][] {
60+
{ "plaintext", Config.EncryptionLevel.NONE },
61+
{ "tls encrypted", Config.EncryptionLevel.REQUIRED }
62+
});
63+
}
64+
65+
private Config.EncryptionLevel encryptionLevel;
66+
67+
public ServerKilledIT( String testName, Config.EncryptionLevel encryptionLevel )
68+
{
69+
this.encryptionLevel = encryptionLevel;
70+
}
71+
5172
@Test
5273
public void shouldRecoverFromServerRestart() throws Throwable
5374
{
5475
// Given
5576
// config with sessionLivenessCheckTimeout not set, i.e. turned off
56-
Config config = Config.build()
57-
.withEncryptionLevel( Config.EncryptionLevel.NONE )
58-
.toConfig();
77+
Config config = Config.build().withEncryptionLevel( encryptionLevel ).toConfig();
5978

6079
try ( Driver driver = GraphDatabase.driver( Neo4jRunner.DEFAULT_URI, config ) )
6180
{
@@ -77,8 +96,7 @@ public void shouldRecoverFromServerRestart() throws Throwable
7796
if ( toleratedFailures-- == 0 )
7897
{
7998
fail( "Expected (for now) at most four failures, one for each old connection, but now I've " +
80-
"gotten " +
81-
"five: " + e.getMessage() );
99+
"gotten " + "five: " + e.getMessage() );
82100
}
83101
}
84102
}
@@ -95,9 +113,8 @@ public void shouldDropBrokenOldSessions() throws Throwable
95113
{
96114
// config with set liveness check timeout
97115
int livenessCheckTimeoutMinutes = 10;
98-
Config config = Config.build()
116+
Config config = Config.build().withEncryptionLevel( encryptionLevel )
99117
.withConnectionLivenessCheckTimeout( livenessCheckTimeoutMinutes, TimeUnit.MINUTES )
100-
.withEncryptionLevel( Config.EncryptionLevel.NONE )
101118
.toConfig();
102119

103120
FakeClock clock = new FakeClock();

0 commit comments

Comments
 (0)