Skip to content

Commit

Permalink
Faster IndexingAcceptanceTest
Browse files Browse the repository at this point in the history
by using static db and separate labels
  • Loading branch information
tinwelint committed Sep 19, 2016
1 parent 7bf58e4 commit c5f0d5c
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 49 deletions.
Expand Up @@ -19,12 +19,13 @@
*/
package org.neo4j.graphdb;

import org.junit.Before;
import org.junit.ClassRule;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TestName;

import java.util.Map;
import java.util.concurrent.TimeUnit;

import org.neo4j.collection.primitive.Primitive;
import org.neo4j.collection.primitive.PrimitiveLongIterator;
import org.neo4j.collection.primitive.PrimitiveLongSet;
Expand All @@ -51,7 +52,6 @@
import static org.neo4j.test.mockito.matcher.Neo4jMatchers.hasProperty;
import static org.neo4j.test.mockito.matcher.Neo4jMatchers.inTx;
import static org.neo4j.test.mockito.matcher.Neo4jMatchers.isEmpty;
import static org.neo4j.test.mockito.matcher.Neo4jMatchers.waitForIndex;

public class IndexingAcceptanceTest
{
Expand Down Expand Up @@ -86,16 +86,8 @@ public void shouldInterpretPropertyAsChangedEvenIfPropertyMovesFromOneRecordToAn
tx.success();
}
}
{
IndexDefinition indexDefinition;
try ( Transaction tx = beansAPI.beginTx() )
{
indexDefinition = beansAPI.schema().indexFor( LABEL1 ).on( "key" ).create();

tx.success();
}
waitForIndex( beansAPI, indexDefinition );
}
Neo4jMatchers.createIndex( beansAPI, LABEL1, "key" );

// WHEN
try ( Transaction tx = beansAPI.beginTx() )
Expand Down Expand Up @@ -129,16 +121,8 @@ public void shouldUseDynamicPropertiesToIndexANodeWhenAddedAlongsideExistingProp
tx.success();
}
}
{
IndexDefinition indexDefinition;
try ( Transaction tx = beansAPI.beginTx() )
{
indexDefinition = beansAPI.schema().indexFor( LABEL1 ).on( "key2" ).create();

tx.success();
}
waitForIndex( beansAPI, indexDefinition );
}
Neo4jMatchers.createIndex( beansAPI, LABEL1, "key2" );
Node myNode;
{
try ( Transaction tx = beansAPI.beginTx() )
Expand Down Expand Up @@ -445,8 +429,9 @@ public void shouldAddIndexedPropertyToNodeWithDynamicLabels()

for ( int i = 0; i < indexesCount; i++ )
{
Neo4jMatchers.createIndex( db, Label.label( labelPrefix + i ), propertyKeyPrefix + i );
Neo4jMatchers.createIndexNoWait( db, Label.label( labelPrefix + i ), propertyKeyPrefix + i );
}
Neo4jMatchers.waitForIndexes( db );

// When
long nodeId;
Expand Down Expand Up @@ -489,7 +474,7 @@ public void shouldSupportIndexSeekByPrefix()
{
// GIVEN
GraphDatabaseService db = dbRule.getGraphDatabaseAPI();
IndexDefinition index = createIndex( db, LABEL1, "name" );
IndexDefinition index = Neo4jMatchers.createIndex( db, LABEL1, "name" );
createNodes( db, LABEL1, "name", "Mattias", "Mats", "Carla" );
PrimitiveLongSet expected = createNodes( db, LABEL1, "name", "Karl", "Karlsson" );

Expand All @@ -513,7 +498,7 @@ public void shouldIncludeNodesCreatedInSameTxInIndexSeekByPrefix()
{
// GIVEN
GraphDatabaseService db = dbRule.getGraphDatabaseAPI();
IndexDefinition index = createIndex( db, LABEL1, "name" );
IndexDefinition index = Neo4jMatchers.createIndex( db, LABEL1, "name" );
createNodes( db, LABEL1, "name", "Mattias", "Mats" );
PrimitiveLongSet expected = createNodes( db, LABEL1, "name", "Carl", "Carlsson" );
// WHEN
Expand All @@ -537,7 +522,7 @@ public void shouldNotIncludeNodesDeletedInSameTxInIndexSeekByPrefix()
{
// GIVEN
GraphDatabaseService db = dbRule.getGraphDatabaseAPI();
IndexDefinition index = createIndex( db, LABEL1, "name" );
IndexDefinition index = Neo4jMatchers.createIndex( db, LABEL1, "name" );
createNodes( db, LABEL1, "name", "Mattias" );
PrimitiveLongSet toDelete = createNodes( db, LABEL1, "name", "Karlsson", "Mats" );
PrimitiveLongSet expected = createNodes( db, LABEL1, "name", "Karl" );
Expand Down Expand Up @@ -567,7 +552,7 @@ public void shouldConsiderNodesChangedInSameTxInIndexPrefixSearch()
{
// GIVEN
GraphDatabaseService db = dbRule.getGraphDatabaseAPI();
IndexDefinition index = createIndex( db, LABEL1, "name" );
IndexDefinition index = Neo4jMatchers.createIndex( db, LABEL1, "name" );
createNodes( db, LABEL1, "name", "Mattias" );
PrimitiveLongSet toChangeToMatch = createNodes( db, LABEL1, "name", "Mats" );
PrimitiveLongSet toChangeToNotMatch = createNodes( db, LABEL1, "name", "Karlsson" );
Expand Down Expand Up @@ -600,23 +585,6 @@ public void shouldConsiderNodesChangedInSameTxInIndexPrefixSearch()
assertThat( found, equalTo( expected ) );
}

private IndexDefinition createIndex( GraphDatabaseService db, Label label, String propertyKey )
{
IndexDefinition index = null;
try ( Transaction tx = db.beginTx() )
{
// create index
index = db.schema().indexFor( label ).on( propertyKey ).create();
tx.success();
}
try ( Transaction tx = db.beginTx() )
{
db.schema().awaitIndexOnline( index, 10, TimeUnit.SECONDS );
tx.success();
}
return index;
}

private PrimitiveLongSet createNodes( GraphDatabaseService db, Label label, String propertyKey, String... propertyValues )
{
PrimitiveLongSet expected = Primitive.longSet();
Expand Down Expand Up @@ -661,12 +629,22 @@ private void assertCanCreateAndFind( GraphDatabaseService db, Label label, Strin

public static final String LONG_STRING = "a long string that has to be stored in dynamic records";

@ClassRule
public static ImpermanentDatabaseRule dbRule = new ImpermanentDatabaseRule();
@Rule
public ImpermanentDatabaseRule dbRule = new ImpermanentDatabaseRule();
public final TestName testName = new TestName();

private Label LABEL1 = Label.label( "LABEL1" );
private Label LABEL2 = Label.label( "LABEL2" );
private Label LABEL3 = Label.label( "LABEL3" );
private Label LABEL1;
private Label LABEL2;
private Label LABEL3;

@Before
public void setupLabels()
{
LABEL1 = Label.label( "LABEL1-" + testName.getMethodName() );
LABEL2 = Label.label( "LABEL2-" + testName.getMethodName() );
LABEL3 = Label.label( "LABEL3-" + testName.getMethodName() );
}

private Node createNode( GraphDatabaseService beansAPI, Map<String, Object> properties, Label... labels )
{
Expand Down
Expand Up @@ -619,15 +619,21 @@ public void describeTo( Description description )
}

public static IndexDefinition createIndex( GraphDatabaseService beansAPI, Label label, String property )
{
IndexDefinition indexDef = createIndexNoWait( beansAPI, label, property );

waitForIndex( beansAPI, indexDef );
return indexDef;
}

public static IndexDefinition createIndexNoWait( GraphDatabaseService beansAPI, Label label, String property )
{
IndexDefinition indexDef;
try ( Transaction tx = beansAPI.beginTx() )
{
indexDef = beansAPI.schema().indexFor( label ).on( property ).create();
tx.success();
}

waitForIndex( beansAPI, indexDef );
return indexDef;
}

Expand All @@ -639,6 +645,14 @@ public static void waitForIndex( GraphDatabaseService beansAPI, IndexDefinition
}
}

public static void waitForIndexes( GraphDatabaseService beansAPI )
{
try ( Transaction ignored = beansAPI.beginTx() )
{
beansAPI.schema().awaitIndexesOnline( 10, SECONDS );
}
}

public static Object getIndexState( GraphDatabaseService beansAPI, IndexDefinition indexDef )
{
try ( Transaction ignored = beansAPI.beginTx() )
Expand Down

0 comments on commit c5f0d5c

Please sign in to comment.