Skip to content

Commit

Permalink
Added unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
pontusmelke committed Sep 16, 2016
1 parent 5b6a7dc commit f9f7b64
Show file tree
Hide file tree
Showing 10 changed files with 1,401 additions and 410 deletions.
20 changes: 11 additions & 9 deletions driver/src/main/java/org/neo4j/driver/internal/ClusterDriver.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,21 +23,21 @@
import java.util.Set;

import org.neo4j.driver.internal.net.BoltServerAddress;
import org.neo4j.driver.internal.net.pooling.PoolSettings;
import org.neo4j.driver.internal.net.pooling.SocketConnectionPool;
import org.neo4j.driver.internal.security.SecurityPlan;
import org.neo4j.driver.internal.spi.Connection;
import org.neo4j.driver.internal.spi.ConnectionPool;
import org.neo4j.driver.internal.util.ConcurrentRoundRobinSet;
import org.neo4j.driver.internal.util.Consumer;
import org.neo4j.driver.v1.AccessMode;
import org.neo4j.driver.v1.Logger;
import org.neo4j.driver.v1.Logging;
import org.neo4j.driver.v1.Record;
import org.neo4j.driver.v1.Session;
import org.neo4j.driver.v1.StatementResult;
import org.neo4j.driver.v1.exceptions.ClientException;
import org.neo4j.driver.v1.exceptions.ConnectionFailureException;
import org.neo4j.driver.v1.exceptions.ServiceUnavailableException;
import org.neo4j.driver.v1.util.BiFunction;

import static java.lang.String.format;

Expand All @@ -59,28 +59,30 @@ public int compare( BoltServerAddress o1, BoltServerAddress o2 )
}
};
private static final int MIN_SERVERS = 2;

protected final ConnectionPool connections;
private final ConnectionPool connections;
private final BiFunction<Connection,Logger, Session> sessionProvider;

private final ConcurrentRoundRobinSet<BoltServerAddress> routingServers = new ConcurrentRoundRobinSet<>(COMPARATOR);
private final ConcurrentRoundRobinSet<BoltServerAddress> readServers = new ConcurrentRoundRobinSet<>(COMPARATOR);
private final ConcurrentRoundRobinSet<BoltServerAddress> writeServers = new ConcurrentRoundRobinSet<>(COMPARATOR);

public ClusterDriver( BoltServerAddress seedAddress, ConnectionSettings connectionSettings,
public ClusterDriver( BoltServerAddress seedAddress,
ConnectionPool connections,
SecurityPlan securityPlan,
PoolSettings poolSettings, Logging logging )
BiFunction<Connection,Logger, Session> sessionProvider,
Logging logging )
{
super( securityPlan, logging );
routingServers.add( seedAddress );
this.connections = new SocketConnectionPool( connectionSettings, securityPlan, poolSettings, logging );
this.connections = connections;
this.sessionProvider = sessionProvider;
checkServers();
}

private void checkServers()
{
synchronized ( routingServers )
{
//todo remove setting hardcode to 2
if ( routingServers.size() < MIN_SERVERS ||
readServers.isEmpty() ||
writeServers.isEmpty())
Expand Down Expand Up @@ -152,7 +154,7 @@ private boolean call( BoltServerAddress address, String procedureName, Consumer<
try
{
acquire = connections.acquire(address);
session = new NetworkSession( acquire, log );
session = sessionProvider.apply( acquire, log );

StatementResult records = session.run( format( "CALL %s", procedureName ) );
while ( records.hasNext() )
Expand Down
13 changes: 5 additions & 8 deletions driver/src/main/java/org/neo4j/driver/internal/DirectDriver.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,9 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.neo4j.driver.internal;

import org.neo4j.driver.internal.net.BoltServerAddress;
import org.neo4j.driver.internal.net.pooling.PoolSettings;
import org.neo4j.driver.internal.net.pooling.SocketConnectionPool;
import org.neo4j.driver.internal.security.SecurityPlan;
import org.neo4j.driver.internal.spi.ConnectionPool;
import org.neo4j.driver.v1.AccessMode;
Expand All @@ -35,18 +32,18 @@ public class DirectDriver extends BaseDriver
protected final ConnectionPool connections;
private final BoltServerAddress address;

public DirectDriver( BoltServerAddress address, ConnectionSettings connectionSettings, SecurityPlan securityPlan,
PoolSettings poolSettings, Logging logging )
public DirectDriver( BoltServerAddress address, ConnectionPool connections, SecurityPlan securityPlan,
Logging logging )
{
super(securityPlan, logging );
this.connections = new SocketConnectionPool( connectionSettings, securityPlan, poolSettings, logging );
super( securityPlan, logging );
this.connections = connections;
this.address = address;
}

@Override
public Session session()
{
return new NetworkSession( connections.acquire(address), log );
return new NetworkSession( connections.acquire( address ), log );
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public void run()
private ExplicitTransaction currentTransaction;
private AtomicBoolean isOpen = new AtomicBoolean( true );

NetworkSession( Connection connection, Logger logger )
public NetworkSession( Connection connection, Logger logger )
{
this.connection = connection;
this.logger = logger;
Expand Down
37 changes: 29 additions & 8 deletions driver/src/main/java/org/neo4j/driver/v1/GraphDatabase.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,15 @@
import org.neo4j.driver.internal.ClusterDriver;
import org.neo4j.driver.internal.ConnectionSettings;
import org.neo4j.driver.internal.DirectDriver;
import org.neo4j.driver.internal.NetworkSession;
import org.neo4j.driver.internal.net.BoltServerAddress;
import org.neo4j.driver.internal.net.pooling.PoolSettings;
import org.neo4j.driver.internal.net.pooling.SocketConnectionPool;
import org.neo4j.driver.internal.security.SecurityPlan;
import org.neo4j.driver.internal.spi.Connection;
import org.neo4j.driver.internal.spi.ConnectionPool;
import org.neo4j.driver.v1.exceptions.ClientException;
import org.neo4j.driver.v1.util.BiFunction;

import static java.lang.String.format;
import static org.neo4j.driver.internal.security.SecurityPlan.insecure;
Expand All @@ -42,6 +47,17 @@
*/
public class GraphDatabase
{

private static final BiFunction<Connection,Logger,Session>
SESSION_PROVIDER = new BiFunction<Connection,Logger,Session>()
{
@Override
public Session apply( Connection connection, Logger logger )
{
return new NetworkSession( connection, logger );
}
};

/**
* Return a driver for a Neo4j instance with the default configuration settings
*
Expand Down Expand Up @@ -144,7 +160,7 @@ public static Driver driver( URI uri, AuthToken authToken, Config config )
new ConnectionSettings( authToken == null ? AuthTokens.none() : authToken );

// Make sure we have some configuration to play with
if (config == null)
if ( config == null )
{
config = Config.defaultConfig();
}
Expand All @@ -166,12 +182,14 @@ public static Driver driver( URI uri, AuthToken authToken, Config config )
config.idleTimeBeforeConnectionTest() );

// And finally, construct the driver proper
ConnectionPool connectionPool =
new SocketConnectionPool( connectionSettings, securityPlan, poolSettings, config.logging() );
switch ( scheme.toLowerCase() )
{
case "bolt":
return new DirectDriver( address, connectionSettings, securityPlan, poolSettings, config.logging() );
return new DirectDriver( address, connectionPool, securityPlan, config.logging() );
case "bolt+routing":
return new ClusterDriver( address, connectionSettings, securityPlan, poolSettings, config.logging() );
return new ClusterDriver( address, connectionPool, securityPlan, SESSION_PROVIDER, config.logging() );
default:
throw new ClientException( format( "Unsupported URI scheme: %s", scheme ) );
}
Expand All @@ -186,24 +204,27 @@ private static SecurityPlan createSecurityPlan( BoltServerAddress address, Confi
{
Config.EncryptionLevel encryptionLevel = config.encryptionLevel();
boolean requiresEncryption = encryptionLevel.equals( REQUIRED ) ||
(encryptionLevel.equals( REQUIRED_NON_LOCAL ) && !address.isLocal() );
(encryptionLevel.equals( REQUIRED_NON_LOCAL ) && !address.isLocal());

if ( requiresEncryption )
{
Logger logger = config.logging().getLog( "session" );
switch ( config.trustStrategy().strategy() )
{
case TRUST_SIGNED_CERTIFICATES:
logger.warn( "Option `TRUST_SIGNED_CERTIFICATE` has been deprecated and will be removed in a future version " +
"of the driver. Please switch to use `TRUST_CUSTOM_CA_SIGNED_CERTIFICATES` instead." );
//intentional fallthrough
logger.warn(
"Option `TRUST_SIGNED_CERTIFICATE` has been deprecated and will be removed in a future " +
"version " +
"of the driver. Please switch to use `TRUST_CUSTOM_CA_SIGNED_CERTIFICATES` instead." );
//intentional fallthrough
case TRUST_CUSTOM_CA_SIGNED_CERTIFICATES:
return SecurityPlan.forSignedCertificates( config.trustStrategy().certFile() );
case TRUST_ON_FIRST_USE:
return SecurityPlan.forTrustOnFirstUse( config.trustStrategy().certFile(),
address, logger );
default:
throw new ClientException( "Unknown TLS authentication strategy: " + config.trustStrategy().strategy().name() );
throw new ClientException(
"Unknown TLS authentication strategy: " + config.trustStrategy().strategy().name() );
}
}
else
Expand Down
39 changes: 39 additions & 0 deletions driver/src/main/java/org/neo4j/driver/v1/util/BiFunction.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/**
* Copyright (c) 2002-2016 "Neo Technology,"
* Network Engine for Objects in Lund AB [http://neotechnology.com]
*
* This file is part of Neo4j.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.neo4j.driver.v1.util;

/**
* Same as {@link java.util.function.BiFunction}, but defined here to work in versions older than java 8.
*
* @param <T> the type of the first argument to the function
* @param <U> the type of the second argument to the function
* @param <R> the type of the result of the function
*
*/
public interface BiFunction<T, U, R> {

/**
* Applies this function to the given arguments.
*
* @param t the first function argument
* @param u the second function argument
* @return the function result
*/
R apply(T t, U u);
}
Loading

0 comments on commit f9f7b64

Please sign in to comment.