Skip to content

Commit

Permalink
Cleanup combined index tests
Browse files Browse the repository at this point in the history
  • Loading branch information
burqen authored and tinwelint committed Aug 4, 2017
1 parent 8edafd1 commit 4ac2027
Show file tree
Hide file tree
Showing 4 changed files with 169 additions and 119 deletions.
Expand Up @@ -71,11 +71,13 @@ public void process( IndexEntryUpdate update ) throws IOException, IndexEntryCon
{ {
from.process( IndexEntryUpdate.remove( from.process( IndexEntryUpdate.remove(
update.getEntityId(), update.indexKey(), update.beforeValues() ) ); update.getEntityId(), update.indexKey(), update.beforeValues() ) );
from.process( IndexEntryUpdate.add( to.process( IndexEntryUpdate.add(
update.getEntityId(), update.indexKey(), update.values() ) ); update.getEntityId(), update.indexKey(), update.values() ) );
} }
break;
case REMOVED: case REMOVED:
select( update.values(), boostUpdater, fallbackUpdater ).process( update ); select( update.values(), boostUpdater, fallbackUpdater ).process( update );
break;
default: default:
throw new IllegalArgumentException( "Unknown update mode" ); throw new IllegalArgumentException( "Unknown update mode" );
} }
Expand All @@ -84,7 +86,13 @@ public void process( IndexEntryUpdate update ) throws IOException, IndexEntryCon
@Override @Override
public void close() throws IOException, IndexEntryConflictException public void close() throws IOException, IndexEntryConflictException
{ {
boostUpdater.close(); try
fallbackUpdater.close(); {
boostUpdater.close();
}
finally
{
fallbackUpdater.close();
}
} }
} }
Expand Up @@ -19,8 +19,8 @@
*/ */
package org.neo4j.kernel.impl.index.schema.combined; package org.neo4j.kernel.impl.index.schema.combined;


import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import org.mockito.Mockito;


import java.io.IOException; import java.io.IOException;


Expand All @@ -35,17 +35,29 @@
import static org.mockito.Mockito.mock; import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify; import static org.mockito.Mockito.verify;
import static org.mockito.internal.verification.VerificationModeFactory.times; import static org.mockito.internal.verification.VerificationModeFactory.times;
import static org.neo4j.kernel.impl.index.schema.combined.CombinedIndexTestHelp.verifyCombinedThrowIfBothThrow;
import static org.neo4j.kernel.impl.index.schema.combined.CombinedIndexTestHelp.verifyFailOnSingleCloseFailure;
import static org.neo4j.kernel.impl.index.schema.combined.CombinedIndexTestHelp.verifyOtherIsClosedOnSingleThrow;


public class CombinedIndexAccessorTest public class CombinedIndexAccessorTest
{ {
private IndexAccessor boostAccessor;
private IndexAccessor fallbackAccessor;
private CombinedIndexAccessor combinedIndexAccessor;

@Before
public void setup()
{
boostAccessor = mock( IndexAccessor.class );
fallbackAccessor = mock( IndexAccessor.class );
combinedIndexAccessor = new CombinedIndexAccessor( boostAccessor, fallbackAccessor );
}

/* drop */

@Test @Test
public void dropMustDropBoostAndFallback() throws Exception public void dropMustDropBoostAndFallback() throws Exception
{ {
// given
IndexAccessor boostAccessor = mock( IndexAccessor.class );
IndexAccessor fallbackAccessor = mock( IndexAccessor.class );
CombinedIndexAccessor combinedIndexAccessor = new CombinedIndexAccessor( boostAccessor, fallbackAccessor );

// when // when
// ... both drop successful // ... both drop successful
combinedIndexAccessor.drop(); combinedIndexAccessor.drop();
Expand All @@ -57,23 +69,13 @@ public void dropMustDropBoostAndFallback() throws Exception
@Test @Test
public void dropMustThrowIfDropBoostFail() throws Exception public void dropMustThrowIfDropBoostFail() throws Exception
{ {
// given
IndexAccessor boostAccessor = mock( IndexAccessor.class );
IndexAccessor fallbackAccessor = mock( IndexAccessor.class );
CombinedIndexAccessor combinedIndexAccessor = new CombinedIndexAccessor( boostAccessor, fallbackAccessor );

// when // when
verifyFailOnSingleDropFailure( boostAccessor, combinedIndexAccessor ); verifyFailOnSingleDropFailure( boostAccessor, combinedIndexAccessor );
} }


@Test @Test
public void dropMustThrowIfDropFallbackFail() throws Exception public void dropMustThrowIfDropFallbackFail() throws Exception
{ {
// given
IndexAccessor boostAccessor = mock( IndexAccessor.class );
IndexAccessor fallbackAccessor = mock( IndexAccessor.class );
CombinedIndexAccessor combinedIndexAccessor = new CombinedIndexAccessor( boostAccessor, fallbackAccessor );

// when // when
verifyFailOnSingleDropFailure( fallbackAccessor, combinedIndexAccessor ); verifyFailOnSingleDropFailure( fallbackAccessor, combinedIndexAccessor );
} }
Expand All @@ -98,9 +100,6 @@ private void verifyFailOnSingleDropFailure( IndexAccessor failingAccessor, Combi
public void dropMustThrowIfBothFail() throws Exception public void dropMustThrowIfBothFail() throws Exception
{ {
// given // given
IndexAccessor boostAccessor = mock( IndexAccessor.class );
IndexAccessor fallbackAccessor = mock( IndexAccessor.class );
CombinedIndexAccessor combinedIndexAccessor = new CombinedIndexAccessor( boostAccessor, fallbackAccessor );
IOException boostFailure = new IOException( "boost" ); IOException boostFailure = new IOException( "boost" );
IOException fallbackFailure = new IOException( "fallback" ); IOException fallbackFailure = new IOException( "fallback" );
doThrow( boostFailure ).when( boostAccessor ).drop(); doThrow( boostFailure ).when( boostAccessor ).drop();
Expand All @@ -119,17 +118,15 @@ public void dropMustThrowIfBothFail() throws Exception
} }
} }


/* close */

@Test @Test
public void closeMustCloseBoostAndFallback() throws Exception public void closeMustCloseBoostAndFallback() throws Exception
{ {
// given
IndexAccessor boostAccessor = mock( IndexAccessor.class );
IndexAccessor fallbackAccessor = mock( IndexAccessor.class );
CombinedIndexAccessor combinedIndexAccessor = new CombinedIndexAccessor( boostAccessor, fallbackAccessor );

// when // when
// ... both drop successful // ... both drop successful
combinedIndexAccessor.close(); combinedIndexAccessor.close();

// then // then
verify( boostAccessor, times( 1 ) ).close(); verify( boostAccessor, times( 1 ) ).close();
verify( fallbackAccessor, times( 1 ) ).close(); verify( fallbackAccessor, times( 1 ) ).close();
Expand All @@ -138,104 +135,30 @@ public void closeMustCloseBoostAndFallback() throws Exception
@Test @Test
public void closeMustThrowIfFallbackThrow() throws Exception public void closeMustThrowIfFallbackThrow() throws Exception
{ {
// given
IndexAccessor boostAccessor = mock( IndexAccessor.class );
IndexAccessor fallbackAccessor = mock( IndexAccessor.class );
CombinedIndexAccessor combinedIndexAccessor = new CombinedIndexAccessor( boostAccessor, fallbackAccessor );

verifyFailOnSingleCloseFailure( fallbackAccessor, combinedIndexAccessor ); verifyFailOnSingleCloseFailure( fallbackAccessor, combinedIndexAccessor );
} }


@Test @Test
public void closeMustThrowIfBoostThrow() throws Exception public void closeMustThrowIfBoostThrow() throws Exception
{ {
// given
IndexAccessor boostAccessor = mock( IndexAccessor.class );
IndexAccessor fallbackAccessor = mock( IndexAccessor.class );
CombinedIndexAccessor combinedIndexAccessor = new CombinedIndexAccessor( boostAccessor, fallbackAccessor );

verifyFailOnSingleCloseFailure( boostAccessor, combinedIndexAccessor ); verifyFailOnSingleCloseFailure( boostAccessor, combinedIndexAccessor );
} }


@Test @Test
public void closeMustCloseBoostIfFallbackThrow() throws Exception public void closeMustCloseBoostIfFallbackThrow() throws Exception
{ {
// given
IndexAccessor boostAccessor = mock( IndexAccessor.class );
IndexAccessor fallbackAccessor = mock( IndexAccessor.class );
CombinedIndexAccessor combinedIndexAccessor = new CombinedIndexAccessor( boostAccessor, fallbackAccessor );

verifyOtherIsClosedOnSingleThrow( fallbackAccessor, boostAccessor, combinedIndexAccessor ); verifyOtherIsClosedOnSingleThrow( fallbackAccessor, boostAccessor, combinedIndexAccessor );
} }


@Test @Test
public void closeMustCloseFallbackIfBoostThrow() throws Exception public void closeMustCloseFallbackIfBoostThrow() throws Exception
{ {
// given
IndexAccessor boostAccessor = mock( IndexAccessor.class );
IndexAccessor fallbackAccessor = mock( IndexAccessor.class );
CombinedIndexAccessor combinedIndexAccessor = new CombinedIndexAccessor( boostAccessor, fallbackAccessor );

verifyOtherIsClosedOnSingleThrow( boostAccessor, fallbackAccessor, combinedIndexAccessor ); verifyOtherIsClosedOnSingleThrow( boostAccessor, fallbackAccessor, combinedIndexAccessor );
} }


private void verifyOtherIsClosedOnSingleThrow( IndexAccessor failingAccessor, IndexAccessor successfulAccessor,
CombinedIndexAccessor combinedIndexAccessor ) throws IOException
{
IOException failure = new IOException( "fail" );
doThrow( failure ).when( failingAccessor ).close();

// when
try
{
combinedIndexAccessor.close();
}
catch ( IOException ignore )
{
}

// then
verify( successfulAccessor, Mockito.times( 1 ) ).close();
}

private void verifyFailOnSingleCloseFailure( IndexAccessor failingAccessor, CombinedIndexAccessor combinedIndexAccessor )
throws IOException
{
IOException expectedFailure = new IOException( "fail" );
doThrow( expectedFailure ).when( failingAccessor ).close();
try
{
combinedIndexAccessor.close();
fail( "Should have failed" );
}
catch ( IOException e )
{
assertSame( expectedFailure, e );
}
}

@Test @Test
public void closeMustThrowIfBothFail() throws Exception public void closeMustThrowIfBothFail() throws Exception
{ {
// given verifyCombinedThrowIfBothThrow( boostAccessor, fallbackAccessor, combinedIndexAccessor );
IndexAccessor boostAccessor = mock( IndexAccessor.class );
IndexAccessor fallbackAccessor = mock( IndexAccessor.class );
CombinedIndexAccessor combinedIndexAccessor = new CombinedIndexAccessor( boostAccessor, fallbackAccessor );
IOException boostFailure = new IOException( "boost" );
IOException fallbackFailure = new IOException( "fallback" );
doThrow( boostFailure ).when( boostAccessor ).close();
doThrow( fallbackFailure ).when( fallbackAccessor ).close();

try
{
// when
combinedIndexAccessor.close();
fail( "Should have failed" );
}
catch ( IOException e )
{
// then0
assertThat( e, anyOf( sameInstance( boostFailure ), sameInstance( fallbackFailure ) ) );
}
} }
} }
Expand Up @@ -28,25 +28,27 @@
import org.neo4j.kernel.api.index.IndexEntryUpdate; import org.neo4j.kernel.api.index.IndexEntryUpdate;
import org.neo4j.kernel.api.index.IndexPopulator; import org.neo4j.kernel.api.index.IndexPopulator;
import org.neo4j.kernel.api.schema.LabelSchemaDescriptor; import org.neo4j.kernel.api.schema.LabelSchemaDescriptor;
import org.neo4j.kernel.api.schema.SchemaDescriptorFactory;
import org.neo4j.values.storable.Value; import org.neo4j.values.storable.Value;


import static org.hamcrest.Matchers.sameInstance;
import static org.hamcrest.core.AnyOf.anyOf;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.fail;
import static org.mockito.Matchers.any; import static org.mockito.Matchers.any;
import static org.mockito.Matchers.anyBoolean; import static org.mockito.Matchers.anyBoolean;
import static org.mockito.Matchers.anyString; import static org.mockito.Matchers.anyString;
import static org.mockito.Mockito.doThrow; import static org.mockito.Mockito.doThrow;
import static org.mockito.Mockito.mock; import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.times; import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify; import static org.mockito.Mockito.verify;
import static org.neo4j.kernel.impl.index.schema.combined.CombinedIndexTestHelp.add;
import static org.neo4j.kernel.impl.index.schema.combined.CombinedIndexTestHelp.verifyCallFail; import static org.neo4j.kernel.impl.index.schema.combined.CombinedIndexTestHelp.verifyCallFail;


public class CombinedIndexPopulatorTest public class CombinedIndexPopulatorTest
{ {
private IndexPopulator boostPopulator; private IndexPopulator boostPopulator;
private IndexPopulator fallbackPopulator; private IndexPopulator fallbackPopulator;
private CombinedIndexPopulator combinedIndexPopulator; private CombinedIndexPopulator combinedIndexPopulator;
private LabelSchemaDescriptor indexKey = SchemaDescriptorFactory.forLabel( 0, 0 );
private LabelSchemaDescriptor compositeIndexKey = SchemaDescriptorFactory.forLabel( 0, 0, 1 );


@Before @Before
public void mockComponents() public void mockComponents()
Expand Down Expand Up @@ -173,25 +175,12 @@ public void addMustSelectCorrectPopulator() throws Exception
private void verifyAddWithCorrectPopulator( IndexPopulator correctPopulator, IndexPopulator wrongPopulator, Value... numberValues ) private void verifyAddWithCorrectPopulator( IndexPopulator correctPopulator, IndexPopulator wrongPopulator, Value... numberValues )
throws IndexEntryConflictException, IOException throws IndexEntryConflictException, IOException
{ {
IndexEntryUpdate<LabelSchemaDescriptor> update = indexEntryUpdate( numberValues ); IndexEntryUpdate<LabelSchemaDescriptor> update = add( numberValues );
combinedIndexPopulator.add( update ); combinedIndexPopulator.add( update );
verify( correctPopulator, times( 1 ) ).add( update ); verify( correctPopulator, times( 1 ) ).add( update );
verify( wrongPopulator, times( 0 ) ).add( update ); verify( wrongPopulator, times( 0 ) ).add( update );
} }


private IndexEntryUpdate<LabelSchemaDescriptor> indexEntryUpdate( Value... value )
{
switch ( value.length )
{
case 1:
return IndexEntryUpdate.add( 0, indexKey, value );
case 2:
return IndexEntryUpdate.add( 0, compositeIndexKey, value );
default:
return null;
}
}

/* verifyDeferredConstraints */ /* verifyDeferredConstraints */


@Test @Test
Expand Down Expand Up @@ -287,6 +276,7 @@ public void closeMustCloseBoostIfFallbackThrow() throws Exception
try try
{ {
combinedIndexPopulator.close( true ); combinedIndexPopulator.close( true );
fail( "Should have failed" );
} }
catch ( IOException ignore ) catch ( IOException ignore )
{ {
Expand All @@ -307,14 +297,36 @@ public void closeMustCloseFallbackIfBoostThrow() throws Exception
try try
{ {
combinedIndexPopulator.close( true ); combinedIndexPopulator.close( true );
fail( "Should have failed" );
} }
catch ( IOException ignore ) catch ( IOException ignore )
{ {
} }


// then // then
verify( fallbackPopulator, times( 1 ) ).close( true ); verify( fallbackPopulator, times( 1 ) ).close( true );
}


@Test
public void closeMustThrowIfBothThrow() throws Exception
{
// given
IOException boostFailure = new IOException( "boost" );
IOException fallbackFailure = new IOException( "fallback" );
doThrow( boostFailure ).when( boostPopulator ).close( anyBoolean() );
doThrow( fallbackFailure ).when( fallbackPopulator).close( anyBoolean() );

try
{
// when
combinedIndexPopulator.close( anyBoolean() );
fail( "Should have failed" );
}
catch ( IOException e )
{
// then
assertThat( e, anyOf( sameInstance( boostFailure ), sameInstance( fallbackFailure ) ) );
}
} }


/* markAsFailed */ /* markAsFailed */
Expand Down

0 comments on commit 4ac2027

Please sign in to comment.