Skip to content

Commit

Permalink
Migrate index module test to junit 5.
Browse files Browse the repository at this point in the history
Introduce random extension.
  • Loading branch information
MishaDemianenko committed Jul 4, 2018
1 parent 7cf4fbd commit e0a8092
Show file tree
Hide file tree
Showing 28 changed files with 547 additions and 736 deletions.
4 changes: 4 additions & 0 deletions community/index/pom.xml
Expand Up @@ -89,6 +89,10 @@ the relevant Commercial Agreement.
<groupId>commons-codec</groupId>
<artifactId>commons-codec</artifactId>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-params</artifactId>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-migrationsupport</artifactId>
Expand Down
Expand Up @@ -21,23 +21,24 @@

import org.apache.commons.lang3.mutable.MutableLong;
import org.eclipse.collections.impl.iterator.ImmutableEmptyLongIterator;
import org.junit.Test;
import org.junit.jupiter.api.Test;

import org.neo4j.io.pagecache.CursorException;
import org.neo4j.io.pagecache.PageCursor;

import static org.hamcrest.CoreMatchers.allOf;
import static org.hamcrest.CoreMatchers.containsString;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.fail;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.neo4j.index.internal.gbptree.ConsistencyChecker.assertNoCrashOrBrokenPointerInGSPP;
import static org.neo4j.index.internal.gbptree.GenerationSafePointer.MIN_GENERATION;
import static org.neo4j.index.internal.gbptree.PageCursorUtil.goTo;
import static org.neo4j.index.internal.gbptree.SimpleLongLayout.longLayout;

public class ConsistencyCheckerTest
class ConsistencyCheckerTest
{
@Test
public void shouldThrowDescriptiveExceptionOnBrokenGSPP() throws Exception
void shouldThrowDescriptiveExceptionOnBrokenGSPP() throws Exception
{
// GIVEN
int pageSize = 256;
Expand All @@ -53,26 +54,21 @@ public void shouldThrowDescriptiveExceptionOnBrokenGSPP() throws Exception
TreeNode.setSuccessor( cursor, pointer, stableGeneration, crashGeneration );

// WHEN
try
CursorException exception = assertThrows( CursorException.class, () ->
{
assertNoCrashOrBrokenPointerInGSPP( cursor, stableGeneration, unstableGeneration,
pointerFieldName, TreeNode.BYTE_POS_SUCCESSOR );
assertNoCrashOrBrokenPointerInGSPP( cursor, stableGeneration, unstableGeneration, pointerFieldName, TreeNode.BYTE_POS_SUCCESSOR );
cursor.checkAndClearCursorException();
fail( "Should have failed" );
}
catch ( CursorException e )
{
// THEN
assertThat( e.getMessage(), containsString( pointerFieldName ) );
assertThat( e.getMessage(), containsString( pointerFieldName ) );
assertThat( e.getMessage(), containsString( "state=CRASH" ) );
assertThat( e.getMessage(), containsString( "state=EMPTY" ) );
assertThat( e.getMessage(), containsString( String.valueOf( pointer ) ) );
}
} );

assertThat( exception.getMessage(), allOf( containsString( pointerFieldName ),
containsString( pointerFieldName ),
containsString( "state=CRASH" ),
containsString( "state=EMPTY" ),
containsString( String.valueOf( pointer ) ) ) );
}

@Test
public void shouldDetectUnusedPages() throws Exception
void shouldDetectUnusedPages() throws Exception
{
// GIVEN
int pageSize = 256;
Expand Down Expand Up @@ -120,15 +116,8 @@ public void shouldDetectUnusedPages() throws Exception
// WHEN
ConsistencyChecker<MutableLong> cc =
new ConsistencyChecker<>( node, layout, stableGeneration, unstableGeneration );
try
{
cc.checkSpace( cursor, idProvider.lastId(), ImmutableEmptyLongIterator.INSTANCE );
fail( "Should have failed" );
}
catch ( RuntimeException e )
{
// THEN good
assertThat( e.getMessage(), containsString( "unused pages" ) );
}
RuntimeException exception =
assertThrows( RuntimeException.class, () -> cc.checkSpace( cursor, idProvider.lastId(), ImmutableEmptyLongIterator.INSTANCE ) );
assertThat( exception.getMessage(), containsString( "unused pages" ) );
}
}
Expand Up @@ -19,21 +19,20 @@
*/
package org.neo4j.index.internal.gbptree;

import org.junit.Before;
import org.junit.Test;
import org.junit.runners.Parameterized.Parameter;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import org.neo4j.io.pagecache.ByteArrayPageCursor;
import org.neo4j.io.pagecache.PageCursor;

import static org.junit.Assert.assertEquals;

import static org.junit.Assert.fail;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.neo4j.index.internal.gbptree.DynamicSizeUtil.extractKeySize;
import static org.neo4j.index.internal.gbptree.DynamicSizeUtil.extractValueSize;
import static org.neo4j.index.internal.gbptree.DynamicSizeUtil.putKeyValueSize;
import static org.neo4j.index.internal.gbptree.DynamicSizeUtil.readKeyValueSize;

public class DynamicSizeUtilTest
class DynamicSizeUtilTest
{
private static final int KEY_ONE_BYTE_MAX = 0x1F;
private static final int KEY_TWO_BYTE_MIN = KEY_ONE_BYTE_MAX + 1;
Expand All @@ -45,21 +44,14 @@ public class DynamicSizeUtilTest

private PageCursor cursor;

@Parameter( 0 )
public int keySize;
@Parameter( 1 )
public int valueSize;
@Parameter( 2 )
public int expectedBytes;

@Before
public void setUp()
@BeforeEach
void setUp()
{
cursor = ByteArrayPageCursor.wrap( 8192 );
}

@Test
public void shouldPutAndGetKeyValueSize() throws Exception
void shouldPutAndGetKeyValueSize()
{
// KEY SIZE | VALUE SIZE | EXPECTED BYTES
shouldPutAndGetKeyValueSize( 0, 0, 1 );
Expand All @@ -85,7 +77,7 @@ public void shouldPutAndGetKeyValueSize() throws Exception
}

@Test
public void shouldPutAndGetKeySize() throws Exception
void shouldPutAndGetKeySize()
{
// KEY SIZE | EXPECTED BYTES
shouldPutAndGetKeySize( 0, 1 );
Expand All @@ -95,42 +87,26 @@ public void shouldPutAndGetKeySize() throws Exception
}

@Test
public void shouldPreventWritingKeyLargerThanMaxPossible() throws Exception
void shouldPreventWritingKeyLargerThanMaxPossible()
{
// given
int keySize = 0xFFF;

// when
try
{
putKeyValueSize( cursor, keySize + 1, 0 );
fail( "Expected failure" );
}
catch ( IllegalArgumentException e )
{
// then good
}
assertThrows( IllegalArgumentException.class, () -> putKeyValueSize( cursor, keySize + 1, 0 ) );

// whereas when size is one less than that
shouldPutAndGetKeyValueSize( keySize, 0, 2 );
}

@Test
public void shouldPreventWritingValueLargerThanMaxPossible() throws Exception
void shouldPreventWritingValueLargerThanMaxPossible()
{
// given
int valueSize = 0x7FFF;

// when
try
{
putKeyValueSize( cursor, 1, valueSize + 1 );
fail( "Expected failure" );
}
catch ( IllegalArgumentException e )
{
// then good
}
assertThrows( IllegalArgumentException.class, () -> putKeyValueSize( cursor, 1, valueSize + 1 ) );

// whereas when size is one less than that
shouldPutAndGetKeyValueSize( 1, valueSize, 3 );
Expand All @@ -153,7 +129,7 @@ private int putAndGetKey( int keySize )
return offsetAfter - offsetBefore;
}

private void shouldPutAndGetKeyValueSize( int keySize, int valueSize, int expectedBytes ) throws Exception
private void shouldPutAndGetKeyValueSize( int keySize, int valueSize, int expectedBytes )
{
int size = putAndGetKeyValue( keySize, valueSize );
assertEquals( expectedBytes, size );
Expand Down
Expand Up @@ -57,7 +57,6 @@
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import static org.neo4j.index.internal.gbptree.SimpleLongLayout.longLayout;
import static org.neo4j.test.rule.PageCacheRule.config;

/**
* A little trick to automatically tell whether or not index format was changed without
Expand Down Expand Up @@ -86,8 +85,8 @@ public static List<Object[]> data()
@Parameter( 1 )
public String zipName;

private final TestDirectory directory = TestDirectory.testDirectory( getClass() );
private final PageCacheRule pageCacheRule = new PageCacheRule( config().withInconsistentReads( false ) );
private final TestDirectory directory = TestDirectory.testDirectory();
private final PageCacheRule pageCacheRule = new PageCacheRule();
private final DefaultFileSystemRule fsRule = new DefaultFileSystemRule();
private final RandomRule random = new RandomRule();

Expand Down
Expand Up @@ -21,9 +21,9 @@

import org.eclipse.collections.api.set.primitive.MutableLongSet;
import org.eclipse.collections.impl.set.mutable.primitive.LongHashSet;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;

import java.io.IOException;
import java.util.ArrayList;
Expand All @@ -33,17 +33,20 @@
import org.neo4j.index.internal.gbptree.FreeListIdProvider.Monitor;
import org.neo4j.io.pagecache.PageCursor;
import org.neo4j.io.pagecache.PagedFile;
import org.neo4j.test.extension.Inject;
import org.neo4j.test.extension.RandomExtension;
import org.neo4j.test.rule.RandomRule;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.ArgumentMatchers.anyInt;
import static org.mockito.ArgumentMatchers.anyLong;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import static org.neo4j.index.internal.gbptree.FreeListIdProvider.NO_MONITOR;

public class FreeListIdProviderTest
@ExtendWith( RandomExtension.class )
class FreeListIdProviderTest
{
private static final int PAGE_SIZE = 128;
private static final long GENERATION_ONE = GenerationSafePointer.MIN_GENERATION;
Expand All @@ -57,11 +60,11 @@ public class FreeListIdProviderTest
private final FreelistPageMonitor monitor = new FreelistPageMonitor();
private final FreeListIdProvider freelist = new FreeListIdProvider( pagedFile, PAGE_SIZE, BASE_ID, monitor );

@Rule
public final RandomRule random = new RandomRule();
@Inject
private RandomRule random;

@Before
public void setUpPagedFile() throws IOException
@BeforeEach
void setUpPagedFile() throws IOException
{
cursor = new PageAwareByteArrayCursor( PAGE_SIZE );
when( pagedFile.io( anyLong(), anyInt() ) ).thenAnswer(
Expand All @@ -70,7 +73,7 @@ public void setUpPagedFile() throws IOException
}

@Test
public void shouldReleaseAndAcquireId() throws Exception
void shouldReleaseAndAcquireId() throws Exception
{
// GIVEN
long releasedId = 11;
Expand All @@ -87,7 +90,7 @@ public void shouldReleaseAndAcquireId() throws Exception
}

@Test
public void shouldReleaseAndAcquireIdsFromMultiplePages() throws Exception
void shouldReleaseAndAcquireIdsFromMultiplePages() throws Exception
{
// GIVEN
int entries = freelist.entriesPerPage() + freelist.entriesPerPage() / 2;
Expand All @@ -106,7 +109,7 @@ public void shouldReleaseAndAcquireIdsFromMultiplePages() throws Exception
}

@Test
public void shouldPutFreedFreeListPagesIntoFreeListAsWell() throws Exception
void shouldPutFreedFreeListPagesIntoFreeListAsWell() throws Exception
{
// GIVEN
long prevId;
Expand Down Expand Up @@ -134,7 +137,7 @@ public void shouldPutFreedFreeListPagesIntoFreeListAsWell() throws Exception
}

@Test
public void shouldStayBoundUnderStress() throws Exception
void shouldStayBoundUnderStress() throws Exception
{
// GIVEN
MutableLongSet acquired = new LongHashSet();
Expand Down Expand Up @@ -185,11 +188,11 @@ public void shouldStayBoundUnderStress() throws Exception
}

// THEN
assertTrue( String.valueOf( freelist.lastId() ), freelist.lastId() < 200 );
assertTrue( freelist.lastId() < 200, String.valueOf( freelist.lastId() ) );
}

@Test
public void shouldVisitUnacquiredIds() throws Exception
void shouldVisitUnacquiredIds() throws Exception
{
// GIVEN a couple of released ids
MutableLongSet expected = new LongHashSet();
Expand Down Expand Up @@ -221,7 +224,7 @@ public void shouldVisitUnacquiredIds() throws Exception
}

@Test
public void shouldVisitFreelistPageIds() throws Exception
void shouldVisitFreelistPageIds() throws Exception
{
// GIVEN a couple of released ids
MutableLongSet expected = new LongHashSet();
Expand Down

0 comments on commit e0a8092

Please sign in to comment.