Skip to content

Commit b9212df

Browse files
committed
Added unit tests
1 parent 5b6a7dc commit b9212df

File tree

10 files changed

+1401
-410
lines changed

10 files changed

+1401
-410
lines changed

driver/src/main/java/org/neo4j/driver/internal/ClusterDriver.java

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,21 +23,21 @@
2323
import java.util.Set;
2424

2525
import org.neo4j.driver.internal.net.BoltServerAddress;
26-
import org.neo4j.driver.internal.net.pooling.PoolSettings;
27-
import org.neo4j.driver.internal.net.pooling.SocketConnectionPool;
2826
import org.neo4j.driver.internal.security.SecurityPlan;
2927
import org.neo4j.driver.internal.spi.Connection;
3028
import org.neo4j.driver.internal.spi.ConnectionPool;
3129
import org.neo4j.driver.internal.util.ConcurrentRoundRobinSet;
3230
import org.neo4j.driver.internal.util.Consumer;
3331
import org.neo4j.driver.v1.AccessMode;
32+
import org.neo4j.driver.v1.Logger;
3433
import org.neo4j.driver.v1.Logging;
3534
import org.neo4j.driver.v1.Record;
3635
import org.neo4j.driver.v1.Session;
3736
import org.neo4j.driver.v1.StatementResult;
3837
import org.neo4j.driver.v1.exceptions.ClientException;
3938
import org.neo4j.driver.v1.exceptions.ConnectionFailureException;
4039
import org.neo4j.driver.v1.exceptions.ServiceUnavailableException;
40+
import org.neo4j.driver.v1.util.BiFunction;
4141

4242
import static java.lang.String.format;
4343

@@ -59,28 +59,30 @@ public int compare( BoltServerAddress o1, BoltServerAddress o2 )
5959
}
6060
};
6161
private static final int MIN_SERVERS = 2;
62-
63-
protected final ConnectionPool connections;
62+
private final ConnectionPool connections;
63+
private final BiFunction<Connection,Logger, Session> sessionProvider;
6464

6565
private final ConcurrentRoundRobinSet<BoltServerAddress> routingServers = new ConcurrentRoundRobinSet<>(COMPARATOR);
6666
private final ConcurrentRoundRobinSet<BoltServerAddress> readServers = new ConcurrentRoundRobinSet<>(COMPARATOR);
6767
private final ConcurrentRoundRobinSet<BoltServerAddress> writeServers = new ConcurrentRoundRobinSet<>(COMPARATOR);
6868

69-
public ClusterDriver( BoltServerAddress seedAddress, ConnectionSettings connectionSettings,
69+
public ClusterDriver( BoltServerAddress seedAddress,
70+
ConnectionPool connections,
7071
SecurityPlan securityPlan,
71-
PoolSettings poolSettings, Logging logging )
72+
BiFunction<Connection,Logger, Session> sessionProvider,
73+
Logging logging )
7274
{
7375
super( securityPlan, logging );
7476
routingServers.add( seedAddress );
75-
this.connections = new SocketConnectionPool( connectionSettings, securityPlan, poolSettings, logging );
77+
this.connections = connections;
78+
this.sessionProvider = sessionProvider;
7679
checkServers();
7780
}
7881

7982
private void checkServers()
8083
{
8184
synchronized ( routingServers )
8285
{
83-
//todo remove setting hardcode to 2
8486
if ( routingServers.size() < MIN_SERVERS ||
8587
readServers.isEmpty() ||
8688
writeServers.isEmpty())
@@ -152,7 +154,7 @@ private boolean call( BoltServerAddress address, String procedureName, Consumer<
152154
try
153155
{
154156
acquire = connections.acquire(address);
155-
session = new NetworkSession( acquire, log );
157+
session = sessionProvider.apply( acquire, log );
156158

157159
StatementResult records = session.run( format( "CALL %s", procedureName ) );
158160
while ( records.hasNext() )

driver/src/main/java/org/neo4j/driver/internal/DirectDriver.java

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,9 @@
1616
* See the License for the specific language governing permissions and
1717
* limitations under the License.
1818
*/
19-
2019
package org.neo4j.driver.internal;
2120

2221
import org.neo4j.driver.internal.net.BoltServerAddress;
23-
import org.neo4j.driver.internal.net.pooling.PoolSettings;
24-
import org.neo4j.driver.internal.net.pooling.SocketConnectionPool;
2522
import org.neo4j.driver.internal.security.SecurityPlan;
2623
import org.neo4j.driver.internal.spi.ConnectionPool;
2724
import org.neo4j.driver.v1.AccessMode;
@@ -35,18 +32,18 @@ public class DirectDriver extends BaseDriver
3532
protected final ConnectionPool connections;
3633
private final BoltServerAddress address;
3734

38-
public DirectDriver( BoltServerAddress address, ConnectionSettings connectionSettings, SecurityPlan securityPlan,
39-
PoolSettings poolSettings, Logging logging )
35+
public DirectDriver( BoltServerAddress address, ConnectionPool connections, SecurityPlan securityPlan,
36+
Logging logging )
4037
{
41-
super(securityPlan, logging );
42-
this.connections = new SocketConnectionPool( connectionSettings, securityPlan, poolSettings, logging );
38+
super( securityPlan, logging );
39+
this.connections = connections;
4340
this.address = address;
4441
}
4542

4643
@Override
4744
public Session session()
4845
{
49-
return new NetworkSession( connections.acquire(address), log );
46+
return new NetworkSession( connections.acquire( address ), log );
5047
}
5148

5249
@Override

driver/src/main/java/org/neo4j/driver/internal/NetworkSession.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ public void run()
6060
private ExplicitTransaction currentTransaction;
6161
private AtomicBoolean isOpen = new AtomicBoolean( true );
6262

63-
NetworkSession( Connection connection, Logger logger )
63+
public NetworkSession( Connection connection, Logger logger )
6464
{
6565
this.connection = connection;
6666
this.logger = logger;

driver/src/main/java/org/neo4j/driver/v1/GraphDatabase.java

Lines changed: 29 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,15 @@
2525
import org.neo4j.driver.internal.ClusterDriver;
2626
import org.neo4j.driver.internal.ConnectionSettings;
2727
import org.neo4j.driver.internal.DirectDriver;
28+
import org.neo4j.driver.internal.NetworkSession;
2829
import org.neo4j.driver.internal.net.BoltServerAddress;
2930
import org.neo4j.driver.internal.net.pooling.PoolSettings;
31+
import org.neo4j.driver.internal.net.pooling.SocketConnectionPool;
3032
import org.neo4j.driver.internal.security.SecurityPlan;
33+
import org.neo4j.driver.internal.spi.Connection;
34+
import org.neo4j.driver.internal.spi.ConnectionPool;
3135
import org.neo4j.driver.v1.exceptions.ClientException;
36+
import org.neo4j.driver.v1.util.BiFunction;
3237

3338
import static java.lang.String.format;
3439
import static org.neo4j.driver.internal.security.SecurityPlan.insecure;
@@ -42,6 +47,17 @@
4247
*/
4348
public class GraphDatabase
4449
{
50+
51+
private static final BiFunction<Connection,Logger,Session>
52+
SESSION_PROVIDER = new BiFunction<Connection,Logger,Session>()
53+
{
54+
@Override
55+
public Session apply( Connection connection, Logger logger )
56+
{
57+
return new NetworkSession( connection, logger );
58+
}
59+
};
60+
4561
/**
4662
* Return a driver for a Neo4j instance with the default configuration settings
4763
*
@@ -144,7 +160,7 @@ public static Driver driver( URI uri, AuthToken authToken, Config config )
144160
new ConnectionSettings( authToken == null ? AuthTokens.none() : authToken );
145161

146162
// Make sure we have some configuration to play with
147-
if (config == null)
163+
if ( config == null )
148164
{
149165
config = Config.defaultConfig();
150166
}
@@ -166,12 +182,14 @@ public static Driver driver( URI uri, AuthToken authToken, Config config )
166182
config.idleTimeBeforeConnectionTest() );
167183

168184
// And finally, construct the driver proper
185+
ConnectionPool connectionPool =
186+
new SocketConnectionPool( connectionSettings, securityPlan, poolSettings, config.logging() );
169187
switch ( scheme.toLowerCase() )
170188
{
171189
case "bolt":
172-
return new DirectDriver( address, connectionSettings, securityPlan, poolSettings, config.logging() );
190+
return new DirectDriver( address, connectionPool, securityPlan, config.logging() );
173191
case "bolt+routing":
174-
return new ClusterDriver( address, connectionSettings, securityPlan, poolSettings, config.logging() );
192+
return new ClusterDriver( address, connectionPool, securityPlan, SESSION_PROVIDER, config.logging() );
175193
default:
176194
throw new ClientException( format( "Unsupported URI scheme: %s", scheme ) );
177195
}
@@ -186,24 +204,27 @@ private static SecurityPlan createSecurityPlan( BoltServerAddress address, Confi
186204
{
187205
Config.EncryptionLevel encryptionLevel = config.encryptionLevel();
188206
boolean requiresEncryption = encryptionLevel.equals( REQUIRED ) ||
189-
(encryptionLevel.equals( REQUIRED_NON_LOCAL ) && !address.isLocal() );
207+
(encryptionLevel.equals( REQUIRED_NON_LOCAL ) && !address.isLocal());
190208

191209
if ( requiresEncryption )
192210
{
193211
Logger logger = config.logging().getLog( "session" );
194212
switch ( config.trustStrategy().strategy() )
195213
{
196214
case TRUST_SIGNED_CERTIFICATES:
197-
logger.warn( "Option `TRUST_SIGNED_CERTIFICATE` has been deprecated and will be removed in a future version " +
198-
"of the driver. Please switch to use `TRUST_CUSTOM_CA_SIGNED_CERTIFICATES` instead." );
199-
//intentional fallthrough
215+
logger.warn(
216+
"Option `TRUST_SIGNED_CERTIFICATE` has been deprecated and will be removed in a future " +
217+
"version " +
218+
"of the driver. Please switch to use `TRUST_CUSTOM_CA_SIGNED_CERTIFICATES` instead." );
219+
//intentional fallthrough
200220
case TRUST_CUSTOM_CA_SIGNED_CERTIFICATES:
201221
return SecurityPlan.forSignedCertificates( config.trustStrategy().certFile() );
202222
case TRUST_ON_FIRST_USE:
203223
return SecurityPlan.forTrustOnFirstUse( config.trustStrategy().certFile(),
204224
address, logger );
205225
default:
206-
throw new ClientException( "Unknown TLS authentication strategy: " + config.trustStrategy().strategy().name() );
226+
throw new ClientException(
227+
"Unknown TLS authentication strategy: " + config.trustStrategy().strategy().name() );
207228
}
208229
}
209230
else
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/**
2+
* Copyright (c) 2002-2016 "Neo Technology,"
3+
* Network Engine for Objects in Lund AB [http://neotechnology.com]
4+
*
5+
* This file is part of Neo4j.
6+
*
7+
* Licensed under the Apache License, Version 2.0 (the "License");
8+
* you may not use this file except in compliance with the License.
9+
* You may obtain a copy of the License at
10+
*
11+
* http://www.apache.org/licenses/LICENSE-2.0
12+
*
13+
* Unless required by applicable law or agreed to in writing, software
14+
* distributed under the License is distributed on an "AS IS" BASIS,
15+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
* See the License for the specific language governing permissions and
17+
* limitations under the License.
18+
*/
19+
package org.neo4j.driver.v1.util;
20+
21+
/**
22+
* Same as {@link java.util.function.BiFunction}, but defined here to work in versions older than java 8.
23+
*
24+
* @param <T> the type of the first argument to the function
25+
* @param <U> the type of the second argument to the function
26+
* @param <R> the type of the result of the function
27+
*
28+
*/
29+
public interface BiFunction<T, U, R> {
30+
31+
/**
32+
* Applies this function to the given arguments.
33+
*
34+
* @param t the first function argument
35+
* @param u the second function argument
36+
* @return the function result
37+
*/
38+
R apply(T t, U u);
39+
}

0 commit comments

Comments
 (0)