Skip to content

Commit

Permalink
Add test utilities
Browse files Browse the repository at this point in the history
Make it easier to run tests with custom config.
Make it easier to run tests with multiple identical threads.
  • Loading branch information
thobe committed Apr 11, 2017
1 parent 4e4590d commit 9941381
Show file tree
Hide file tree
Showing 14 changed files with 221 additions and 119 deletions.
Expand Up @@ -32,7 +32,6 @@
import org.neo4j.graphdb.Label;
import org.neo4j.graphdb.Result;
import org.neo4j.graphdb.Transaction;
import org.neo4j.graphdb.factory.GraphDatabaseBuilder;
import org.neo4j.graphdb.factory.GraphDatabaseSettings;
import org.neo4j.kernel.configuration.Config;
import org.neo4j.kernel.monitoring.Monitors;
Expand All @@ -51,15 +50,8 @@ public class QueryInvalidationIT

@Rule
public final DatabaseRule db = new ImpermanentDatabaseRule()
{
@Override
protected void configure( GraphDatabaseBuilder builder )
{
super.configure( builder );
builder.setConfig( GraphDatabaseSettings.query_statistics_divergence_threshold, "0.5" );
builder.setConfig( GraphDatabaseSettings.cypher_min_replan_interval, "1s" );
}
};
.withSetting( GraphDatabaseSettings.query_statistics_divergence_threshold, "0.5" )
.withSetting( GraphDatabaseSettings.cypher_min_replan_interval, "1s" );

@Test
public void shouldRePlanAfterDataChangesFromAnEmptyDatabase() throws Exception
Expand Down
Expand Up @@ -27,7 +27,6 @@

import org.neo4j.graphdb.Node;
import org.neo4j.graphdb.Transaction;
import org.neo4j.graphdb.factory.GraphDatabaseBuilder;
import org.neo4j.graphdb.factory.GraphDatabaseSettings;
import org.neo4j.graphdb.index.Index;
import org.neo4j.graphdb.index.IndexManager;
Expand All @@ -53,13 +52,7 @@ public class TransactionRepresentationCommitProcessIT

@Rule
public final DatabaseRule db = new ImpermanentDatabaseRule()
{
@Override
protected void configure( GraphDatabaseBuilder builder )
{
builder.setConfig( GraphDatabaseSettings.check_point_interval_time, "10ms" );
}
};
.withSetting( GraphDatabaseSettings.check_point_interval_time, "10ms" );

@Test( timeout = 15000 )
public void commitDuringContinuousCheckpointing() throws Exception
Expand Down
Expand Up @@ -43,7 +43,6 @@
import org.neo4j.graphdb.DependencyResolver;
import org.neo4j.graphdb.GraphDatabaseService;
import org.neo4j.graphdb.Transaction;
import org.neo4j.graphdb.factory.GraphDatabaseBuilder;
import org.neo4j.graphdb.factory.GraphDatabaseSettings;
import org.neo4j.kernel.api.Statement;
import org.neo4j.kernel.api.exceptions.EntityNotFoundException;
Expand Down Expand Up @@ -86,17 +85,9 @@ public class IndexStatisticsTest

@Rule
public DatabaseRule dbRule = new EmbeddedDatabaseRule()
{
@Override
protected void configure( GraphDatabaseBuilder builder )
{
super.configure( builder );
// make sure we don't sample in these tests
builder.setConfig( GraphDatabaseSettings.index_background_sampling_enabled, "false" );
builder.setConfig( GraphDatabaseSettings.multi_threaded_schema_index_population_enabled,
.withSetting( GraphDatabaseSettings.index_background_sampling_enabled, "false" )
.withSetting( GraphDatabaseSettings.multi_threaded_schema_index_population_enabled,
multiThreadedPopulationEnabled + "" );
}
};

private GraphDatabaseService db;
private ThreadToStatementContextBridge bridge;
Expand Down
Expand Up @@ -28,7 +28,6 @@
import org.neo4j.graphdb.Relationship;
import org.neo4j.graphdb.RelationshipType;
import org.neo4j.graphdb.Transaction;
import org.neo4j.graphdb.factory.GraphDatabaseBuilder;
import org.neo4j.graphdb.factory.GraphDatabaseSettings;
import org.neo4j.helpers.Exceptions;
import org.neo4j.test.Race;
Expand All @@ -42,14 +41,7 @@ public class NeoStoresIT
{
@ClassRule
public static final DatabaseRule db = new EmbeddedDatabaseRule()
{
@Override
protected void configure( GraphDatabaseBuilder builder )
{
super.configure( builder );
builder.setConfig( GraphDatabaseSettings.dense_node_threshold, "1");
}
};
.withSetting( GraphDatabaseSettings.dense_node_threshold, "1");

private static final RelationshipType FRIEND = RelationshipType.withName( "FRIEND" );

Expand Down
Expand Up @@ -27,7 +27,6 @@
import org.neo4j.graphdb.Node;
import org.neo4j.graphdb.Relationship;
import org.neo4j.graphdb.Transaction;
import org.neo4j.graphdb.factory.GraphDatabaseBuilder;
import org.neo4j.graphdb.factory.GraphDatabaseSettings;
import org.neo4j.kernel.impl.MyRelTypes;
import org.neo4j.test.rule.DatabaseRule;
Expand Down Expand Up @@ -55,13 +54,7 @@ public class RelationshipChainPointerChasingTest

@Rule
public final DatabaseRule db = new ImpermanentDatabaseRule()
{
@Override
protected void configure( GraphDatabaseBuilder builder )
{
builder.setConfig( GraphDatabaseSettings.dense_node_threshold, String.valueOf( THRESHOLD ) );
}
};
.withSetting( GraphDatabaseSettings.dense_node_threshold, String.valueOf( THRESHOLD ) );

@Test
public void shouldChaseTheLivingRelationships() throws Exception
Expand Down
Expand Up @@ -25,7 +25,6 @@
import org.neo4j.graphdb.Node;
import org.neo4j.graphdb.RelationshipType;
import org.neo4j.graphdb.Transaction;
import org.neo4j.graphdb.factory.GraphDatabaseBuilder;
import org.neo4j.graphdb.factory.GraphDatabaseSettings;
import org.neo4j.kernel.impl.storageengine.impl.recordstorage.RecordStorageEngine;
import org.neo4j.kernel.internal.GraphDatabaseAPI;
Expand All @@ -42,13 +41,7 @@ public class RelationshipGroupStoreIT

@Rule
public final DatabaseRule db = new ImpermanentDatabaseRule()
{
@Override
protected void configure( GraphDatabaseBuilder builder )
{
builder.setConfig( GraphDatabaseSettings.dense_node_threshold, "1" );
}
};
.withSetting( GraphDatabaseSettings.dense_node_threshold, "1" );

@Test
public void shouldCreateAllTheseRelationshipTypes() throws Exception
Expand Down
Expand Up @@ -29,7 +29,6 @@
import org.neo4j.graphdb.GraphDatabaseService;
import org.neo4j.graphdb.Node;
import org.neo4j.graphdb.Transaction;
import org.neo4j.graphdb.factory.GraphDatabaseBuilder;
import org.neo4j.graphdb.factory.GraphDatabaseSettings;
import org.neo4j.kernel.impl.MyRelTypes;
import org.neo4j.kernel.impl.locking.NoOpClient;
Expand Down Expand Up @@ -64,13 +63,7 @@ public class RelationshipCreatorTest
private static final int DENSE_NODE_THRESHOLD = 5;
@Rule
public final DatabaseRule dbRule = new ImpermanentDatabaseRule()
{
@Override
protected void configure( GraphDatabaseBuilder builder )
{
builder.setConfig( GraphDatabaseSettings.dense_node_threshold, String.valueOf( DENSE_NODE_THRESHOLD ) );
}
};
.withSetting( GraphDatabaseSettings.dense_node_threshold, String.valueOf( DENSE_NODE_THRESHOLD ) );
private IdGeneratorFactory idGeneratorFactory;

@Before
Expand Down
106 changes: 106 additions & 0 deletions community/kernel/src/test/java/org/neo4j/test/ConfigBuilder.java
@@ -0,0 +1,106 @@
/*
* Copyright (c) 2002-2017 "Neo Technology,"
* Network Engine for Objects in Lund AB [http://neotechnology.com]
*
* This file is part of Neo4j.
*
* Neo4j is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.neo4j.test;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

import org.neo4j.graphdb.config.Setting;

/**
* Convenience for building config for a test database.
* In particular this is class is useful when parameterizing a test with different configurations.
* <p>
* Usage:
* <pre><code>
* import static org.neo4j.test.ConfigBuilder.configure;
*
* {@literal @}{@link org.junit.runner.RunWith RunWith}({@link org.junit.runners.Parameterized Parameterized.class})
* public class SomeTest
* {
* {@literal @}{@link org.junit.runners.Parameterized.Parameters Parameterized.Parameters}( name = "{0}" )
* public static Iterable&lt;Object[]&gt; configurations()
* {
* return Arrays.asList(
* // First set of configuration
* {@link #configure(Setting, String) configure}( {@link
* org.neo4j.graphdb.factory.GraphDatabaseSettings#query_cache_size
* GraphDatabaseSettings.query_cache_size}, "42" ).{@link #asParameters() asParameters}(),
* // Second set of configuration
* {@link #configure(Setting, String) configure}( {@link
* org.neo4j.graphdb.factory.GraphDatabaseSettings#query_cache_size
* GraphDatabaseSettings.query_cache_size}, "12" )
* .{@link #and(Setting, String) and}( {@link
* org.neo4j.graphdb.factory.GraphDatabaseSettings#cypher_min_replan_interval
* GraphDatabaseSettings.cypher_min_replan_interval}, "5000" ).{@link #asParameters() asParameters}()
* );
* }
*
* public final{@literal @}Rule {@link org.neo4j.test.rule.DatabaseRule DatabaseRule} db;
*
* public SomeTest( ConfigBuilder config )
* {
* this.db = new {@link org.neo4j.test.rule.ImpermanentDatabaseRule
* ImpermanentDatabaseRule}().{@link org.neo4j.test.rule.DatabaseRule#withConfiguration(Map)
* withConfiguration}( config.{@link #configuration() configuration}() );
* }
* }
* </code></pre>
*/
public final class ConfigBuilder
{
public static ConfigBuilder configure( Setting<?> key, String value )
{
Map<Setting<?>,String> config = new HashMap<>();
config.put( key, value );
return new ConfigBuilder( config );
}

private final Map<Setting<?>,String> config;

private ConfigBuilder( Map<Setting<?>,String> config )
{
this.config = config;
}

public Map<Setting<?>,String> configuration()
{
return Collections.unmodifiableMap( config );
}

public ConfigBuilder and( Setting<?> key, String value )
{
Map<Setting<?>,String> config = new HashMap<>( this.config );
config.put( key, value );
return new ConfigBuilder( config );
}

public Object[] asParameters()
{
return new Object[] {this};
}

@Override
public String toString()
{
return config.toString();
}
}
Expand Up @@ -22,6 +22,7 @@
import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import java.util.function.Consumer;
Expand Down Expand Up @@ -67,6 +68,7 @@ public abstract class DatabaseRule extends ExternalResource implements GraphData
private String storeDir;
private Supplier<Statement> statementSupplier;
private boolean startEagerly = true;
private Map<Setting<?>, String> config;

/**
* Means the database will be started on first {@link #getGraphDatabaseAPI()}}
Expand Down Expand Up @@ -296,6 +298,13 @@ protected void configure( GraphDatabaseBuilder builder )
// Override to configure the database

// Adjusted defaults for testing
if ( config != null )
{
for ( Map.Entry<Setting<?>,String> setting : config.entrySet() )
{
builder.setConfig( setting.getKey(), setting.getValue() );
}
}
}

public GraphDatabaseBuilder setConfig( Setting<?> setting, String value )
Expand Down Expand Up @@ -328,6 +337,26 @@ public synchronized void ensureStarted()
}
}

public DatabaseRule withSetting( Setting<?> key, String value )
{
if ( this.config == null )
{
this.config = new HashMap<>();
}
this.config.put( key, value );
return this;
}

public DatabaseRule withConfiguration( Map<Setting<?>,String> configuration )
{
if ( this.config == null )
{
this.config = new HashMap<>();
}
this.config.putAll( configuration );
return this;
}

public interface RestartAction
{
void run( FileSystemAbstraction fs, File storeDirectory ) throws IOException;
Expand Down

0 comments on commit 9941381

Please sign in to comment.