diff --git a/community/kernel/src/test/java/org/neo4j/kernel/impl/store/format/LimitedRecordGenerators.java b/community/kernel/src/test/java/org/neo4j/kernel/impl/store/format/LimitedRecordGenerators.java index 7f4ccc901049..49ec85917cc0 100644 --- a/community/kernel/src/test/java/org/neo4j/kernel/impl/store/format/LimitedRecordGenerators.java +++ b/community/kernel/src/test/java/org/neo4j/kernel/impl/store/format/LimitedRecordGenerators.java @@ -31,14 +31,17 @@ import org.neo4j.kernel.impl.store.record.RelationshipRecord; import org.neo4j.kernel.impl.store.record.RelationshipTypeTokenRecord; import org.neo4j.test.RandomRule; +import org.neo4j.test.Randoms; +import static java.lang.Long.max; import static java.lang.Math.abs; +import static java.lang.Math.toIntExact; public class LimitedRecordGenerators implements RecordGenerators { static final long NULL = -1; - private final RandomRule random; + private final Randoms random; private final int entityBits; private final int propertyBits; private final int nodeLabelBits; @@ -46,13 +49,13 @@ public class LimitedRecordGenerators implements RecordGenerators private final long nullValue; private final float fractionNullValues; - public LimitedRecordGenerators( RandomRule random, int entityBits, int propertyBits, int nodeLabelBits, + public LimitedRecordGenerators( Randoms random, int entityBits, int propertyBits, int nodeLabelBits, int tokenBits, long nullValue ) { this( random, entityBits, propertyBits, nodeLabelBits, tokenBits, nullValue, 0.2f ); } - public LimitedRecordGenerators( RandomRule random, int entityBits, int propertyBits, int nodeLabelBits, + public LimitedRecordGenerators( Randoms random, int entityBits, int propertyBits, int nodeLabelBits, int tokenBits, long nullValue, float fractionNullValues ) { this.random = random; @@ -67,19 +70,16 @@ public LimitedRecordGenerators( RandomRule random, int entityBits, int propertyB @Override public Generator relationshipTypeToken() { - return (recordSize, format) -> new RelationshipTypeTokenRecord( randomId() ).initialize( random.nextBoolean(), + return (recordSize, format, recordId) -> new RelationshipTypeTokenRecord( toIntExact( recordId ) ).initialize( + random.nextBoolean(), randomInt( tokenBits ) ); } - private int randomId() - { - return random.nextInt( 5 ); - } - @Override public Generator relationshipGroup() { - return (recordSize, format) -> new RelationshipGroupRecord( randomId() ).initialize( random.nextBoolean(), + return (recordSize, format, recordId) -> new RelationshipGroupRecord( recordId ).initialize( + random.nextBoolean(), randomInt( tokenBits ), randomLongOrOccasionallyNull( entityBits ), randomLongOrOccasionallyNull( entityBits ), @@ -91,7 +91,8 @@ public Generator relationshipGroup() @Override public Generator relationship() { - return (recordSize, format) -> new RelationshipRecord( randomId() ).initialize( random.nextBoolean(), + return (recordSize, format, recordId) -> new RelationshipRecord( recordId ).initialize( + random.nextBoolean(), randomLongOrOccasionallyNull( propertyBits ), random.nextLong( entityBits ), random.nextLong( entityBits ), randomInt( tokenBits ), randomLongOrOccasionallyNull( entityBits ), randomLongOrOccasionallyNull( entityBits ), @@ -102,15 +103,17 @@ public Generator relationship() @Override public Generator propertyKeyToken() { - return (recordSize, format) -> new PropertyKeyTokenRecord( randomId() ).initialize( random.nextBoolean(), - random.nextInt( tokenBits ), abs( random.nextInt() ) ); + return (recordSize, format, recordId) -> new PropertyKeyTokenRecord( toIntExact( recordId ) ).initialize( + random.nextBoolean(), + random.nextInt( tokenBits ), + abs( random.nextInt() ) ); } @Override public Generator property() { - return (recordSize, format) -> { - PropertyRecord record = new PropertyRecord( randomId() ); + return (recordSize, format, recordId) -> { + PropertyRecord record = new PropertyRecord( recordId ); int maxProperties = random.intBetween( 1, 4 ); StandaloneDynamicRecordAllocator stringAllocator = new StandaloneDynamicRecordAllocator(); StandaloneDynamicRecordAllocator arrayAllocator = new StandaloneDynamicRecordAllocator(); @@ -139,8 +142,10 @@ public Generator property() @Override public Generator node() { - return (recordSize, format) -> new NodeRecord( randomId() ).initialize( - random.nextBoolean(), randomLongOrOccasionallyNull( propertyBits ), random.nextBoolean(), + return (recordSize, format, recordId) -> new NodeRecord( recordId ).initialize( + random.nextBoolean(), + randomLongOrOccasionallyNull( propertyBits ), + random.nextBoolean(), randomLongOrOccasionallyNull( entityBits ), randomLongOrOccasionallyNull( nodeLabelBits, 0 ) ); } @@ -148,18 +153,19 @@ public Generator node() @Override public Generator labelToken() { - return (recordSize, format) -> new LabelTokenRecord( randomId() ).initialize( - random.nextBoolean(), random.nextInt( tokenBits ) ); + return (recordSize, format, recordId) -> new LabelTokenRecord( toIntExact( recordId ) ).initialize( + random.nextBoolean(), + random.nextInt( tokenBits ) ); } @Override public Generator dynamic() { - return (recordSize, format) -> { + return (recordSize, format, recordId) -> { int dataSize = recordSize - format.getRecordHeaderSize(); int length = random.nextBoolean() ? dataSize : random.nextInt( dataSize ); long next = length == dataSize ? randomLong( propertyBits ) : nullValue; - DynamicRecord record = new DynamicRecord( random.nextInt( 1, 5 ) ).initialize( random.nextBoolean(), + DynamicRecord record = new DynamicRecord( max( 1, recordId ) ).initialize( random.nextBoolean(), random.nextBoolean(), next, random.nextInt( PropertyType.values().length ), length ); byte[] data = new byte[record.getLength()]; random.nextBytes( data ); diff --git a/community/kernel/src/test/java/org/neo4j/kernel/impl/store/format/RecordFormatTest.java b/community/kernel/src/test/java/org/neo4j/kernel/impl/store/format/RecordFormatTest.java index 566ee0bd27a9..01691b8053cd 100644 --- a/community/kernel/src/test/java/org/neo4j/kernel/impl/store/format/RecordFormatTest.java +++ b/community/kernel/src/test/java/org/neo4j/kernel/impl/store/format/RecordFormatTest.java @@ -19,6 +19,7 @@ */ package org.neo4j.kernel.impl.store.format; +import org.junit.Before; import org.junit.ClassRule; import org.junit.Ignore; import org.junit.Rule; @@ -47,12 +48,14 @@ import static java.util.concurrent.TimeUnit.SECONDS; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; + +import static org.neo4j.io.ByteUnit.kibiBytes; import static org.neo4j.kernel.impl.store.record.RecordLoad.NORMAL; @Ignore( "Not a test, a base class for testing formats" ) public abstract class RecordFormatTest { - private static final int PAGE_SIZE = 1_024; + private static final int PAGE_SIZE = (int) kibiBytes( 1 ); // Whoever is hit first private static final long TEST_ITERATIONS = 20_000; @@ -72,12 +75,21 @@ public abstract class RecordFormatTest public RecordKeys keys = FullyCoveringRecordKeys.INSTANCE; private final RecordFormats formats; - private final RecordGenerators generators; + private final int entityBits; + private final int propertyBits; + private RecordGenerators generators; - protected RecordFormatTest( RecordFormats formats, RecordGenerators generators ) + protected RecordFormatTest( RecordFormats formats, int entityBits, int propertyBits ) { this.formats = formats; - this.generators = generators; + this.entityBits = entityBits; + this.propertyBits = propertyBits; + } + + @Before + public void before() + { + generators = new LimitedRecordGenerators( random.randoms(), entityBits, propertyBits, 40, 16, -1 ); } @Test @@ -158,7 +170,7 @@ private void verifyWriteAndRead( long i = 0; for ( ; i < TEST_ITERATIONS && currentTimeMillis() < endTime; i++ ) { - R written = generator.get( recordSize, format ); + R written = generator.get( recordSize, format, i % 5 ); R read = format.newRecord(); try { diff --git a/community/kernel/src/test/java/org/neo4j/kernel/impl/store/format/RecordGenerators.java b/community/kernel/src/test/java/org/neo4j/kernel/impl/store/format/RecordGenerators.java index 10ff14fba6c8..ce429d1775fa 100644 --- a/community/kernel/src/test/java/org/neo4j/kernel/impl/store/format/RecordGenerators.java +++ b/community/kernel/src/test/java/org/neo4j/kernel/impl/store/format/RecordGenerators.java @@ -33,7 +33,7 @@ public interface RecordGenerators { interface Generator { - RECORD get( int recordSize, RecordFormat format ); + RECORD get( int recordSize, RecordFormat format, long id ); } Generator node(); diff --git a/community/kernel/src/test/java/org/neo4j/kernel/impl/store/format/StandardRecordFormatTest.java b/community/kernel/src/test/java/org/neo4j/kernel/impl/store/format/StandardRecordFormatTest.java index 3b61be1256e1..902149bc2db4 100644 --- a/community/kernel/src/test/java/org/neo4j/kernel/impl/store/format/StandardRecordFormatTest.java +++ b/community/kernel/src/test/java/org/neo4j/kernel/impl/store/format/StandardRecordFormatTest.java @@ -23,10 +23,8 @@ public class StandardRecordFormatTest extends RecordFormatTest { - private static final RecordGenerators LOW_LIMITS = new LimitedRecordGenerators( random, 35, 36, 40, 16, NULL ); - public StandardRecordFormatTest() { - super( StandardV3_0.RECORD_FORMATS, LOW_LIMITS ); + super( StandardV3_0.RECORD_FORMATS, 35, 36 ); } } diff --git a/community/kernel/src/test/java/org/neo4j/test/Randoms.java b/community/kernel/src/test/java/org/neo4j/test/Randoms.java index 9476cf6509f2..f87b4b399d4e 100644 --- a/community/kernel/src/test/java/org/neo4j/test/Randoms.java +++ b/community/kernel/src/test/java/org/neo4j/test/Randoms.java @@ -26,6 +26,7 @@ import org.neo4j.helpers.ArrayUtil; import static java.lang.Integer.bitCount; +import static java.lang.Math.abs; /** * Set of useful randomizing utilities, for example randomizing a string or property value of random type and value. @@ -252,4 +253,34 @@ private char symbol() throw new IllegalArgumentException( "Unknown symbol range " + range ); } } + + public long nextLong( long bound ) + { + return abs( random.nextLong() ) % bound; + } + + public boolean nextBoolean() + { + return random.nextBoolean(); + } + + public int nextInt( int bound ) + { + return random.nextInt( bound ); + } + + public int nextInt() + { + return random.nextInt(); + } + + public void nextBytes( byte[] data ) + { + random.nextBytes( data ); + } + + public float nextFloat() + { + return random.nextFloat(); + } } diff --git a/enterprise/kernel/src/test/java/org/neo4j/kernel/impl/store/format/highlimit/HighLimitRecordFormatTest.java b/enterprise/kernel/src/test/java/org/neo4j/kernel/impl/store/format/highlimit/HighLimitRecordFormatTest.java index be4511644489..138d1bf19288 100644 --- a/enterprise/kernel/src/test/java/org/neo4j/kernel/impl/store/format/highlimit/HighLimitRecordFormatTest.java +++ b/enterprise/kernel/src/test/java/org/neo4j/kernel/impl/store/format/highlimit/HighLimitRecordFormatTest.java @@ -19,16 +19,12 @@ */ package org.neo4j.kernel.impl.store.format.highlimit; -import org.neo4j.kernel.impl.store.format.LimitedRecordGenerators; import org.neo4j.kernel.impl.store.format.RecordFormatTest; -import org.neo4j.kernel.impl.store.format.RecordGenerators; public class HighLimitRecordFormatTest extends RecordFormatTest { - private static final RecordGenerators HIGH_LIMITS = new LimitedRecordGenerators( random, 50, 50, 50, 16, NULL ); - public HighLimitRecordFormatTest() { - super( HighLimit.RECORD_FORMATS, HIGH_LIMITS ); + super( HighLimit.RECORD_FORMATS, 50, 50 ); } }