Search in sources :

Example 1 with RandomRule

use of org.neo4j.test.rule.RandomRule in project neo4j by neo4j.

the class GBPTreeGenericCountsStoreTest method generateAndApplyTransaction.

/**
 * Generates a transaction, i.e. a counts change set. The data is random, but uses a seed which is the seed of the {@link RandomRule} in this test
 * as well as the the supplied txId. Calling this method in any given test multiple times with any specific txId will generate the same data.
 *
 * @param expected map of counts to update with the generated changes.
 * @param txId transaction id to generate transaction data for and ultimately apply to the counts store (and the expected map).
 */
private void generateAndApplyTransaction(ConcurrentMap<CountsKey, AtomicLong> expected, long txId) {
    Random rng = new Random(random.seed() + txId);
    try (CountUpdater updater = countsStore.updater(txId, NULL)) {
        if (updater != null) {
            int numberOfKeys = rng.nextInt(10);
            for (int j = 0; j < numberOfKeys; j++) {
                // chance to get -1
                long delta = rng.nextInt(11) - 1;
                CountsKey expectedKey;
                if (rng.nextBoolean()) {
                    // Node
                    int labelId = randomTokenId(rng);
                    updater.increment(nodeKey(labelId), delta);
                    expectedKey = nodeKey(labelId);
                } else {
                    // Relationship
                    int startLabelId = randomTokenId(rng);
                    int type = randomTokenId(rng);
                    int endLabelId = randomTokenId(rng);
                    updater.increment(relationshipKey(startLabelId, type, endLabelId), delta);
                    expectedKey = relationshipKey(startLabelId, type, endLabelId);
                }
                expected.computeIfAbsent(expectedKey, k -> new AtomicLong()).addAndGet(delta);
            }
        }
    }
}
Also used : BeforeEach(org.junit.jupiter.api.BeforeEach) NoSuchFileException(java.nio.file.NoSuchFileException) DatabaseReadOnlyChecker.readOnly(org.neo4j.configuration.helpers.DatabaseReadOnlyChecker.readOnly) RandomExtension(org.neo4j.test.extension.RandomExtension) CursorContext(org.neo4j.io.pagecache.context.CursorContext) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) Random(java.util.Random) DefaultPageCacheTracer(org.neo4j.io.pagecache.tracing.DefaultPageCacheTracer) ANY_LABEL(org.neo4j.token.api.TokenConstants.ANY_LABEL) ByteBuffer(java.nio.ByteBuffer) NO_MONITOR(org.neo4j.internal.counts.GBPTreeCountsStore.NO_MONITOR) DEFAULT_DATABASE_NAME(org.neo4j.configuration.GraphDatabaseSettings.DEFAULT_DATABASE_NAME) Future(java.util.concurrent.Future) StoreChannel(org.neo4j.io.fs.StoreChannel) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) Assertions.assertFalse(org.junit.jupiter.api.Assertions.assertFalse) RandomRule(org.neo4j.test.rule.RandomRule) Map(java.util.Map) Mockito.doAnswer(org.mockito.Mockito.doAnswer) PageCacheTracer(org.neo4j.io.pagecache.tracing.PageCacheTracer) NULL(org.neo4j.io.pagecache.context.CursorContext.NULL) Path(java.nio.file.Path) Exceptions(org.neo4j.internal.helpers.Exceptions) PageCache(org.neo4j.io.pagecache.PageCache) EMPTY_REBUILD(org.neo4j.internal.counts.GBPTreeGenericCountsStore.EMPTY_REBUILD) ArrayQueueOutOfOrderSequence(org.neo4j.util.concurrent.ArrayQueueOutOfOrderSequence) OtherThreadExecutor.command(org.neo4j.test.OtherThreadExecutor.command) DatabaseReadOnlyChecker.writable(org.neo4j.configuration.helpers.DatabaseReadOnlyChecker.writable) StandardOpenOption(java.nio.file.StandardOpenOption) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Set(java.util.Set) TestDirectory(org.neo4j.test.rule.TestDirectory) String.format(java.lang.String.format) Test(org.junit.jupiter.api.Test) INSTANCE(org.neo4j.memory.EmptyMemoryTracker.INSTANCE) GBPTreeCountsStore.nodeKey(org.neo4j.internal.counts.GBPTreeCountsStore.nodeKey) Assertions.assertTrue(org.junit.jupiter.api.Assertions.assertTrue) Rebuilder(org.neo4j.internal.counts.GBPTreeGenericCountsStore.Rebuilder) EMPTY_LONG_ARRAY(org.apache.commons.lang3.ArrayUtils.EMPTY_LONG_ARRAY) Race.throwing(org.neo4j.test.Race.throwing) MutableBoolean(org.apache.commons.lang3.mutable.MutableBoolean) DatabaseReadOnlyChecker(org.neo4j.configuration.helpers.DatabaseReadOnlyChecker) Assertions.assertDoesNotThrow(org.junit.jupiter.api.Assertions.assertDoesNotThrow) Mockito.mock(org.mockito.Mockito.mock) Assertions.assertThrows(org.junit.jupiter.api.Assertions.assertThrows) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) OutOfOrderSequence(org.neo4j.util.concurrent.OutOfOrderSequence) TreeFileNotFoundException(org.neo4j.index.internal.gbptree.TreeFileNotFoundException) HashMap(java.util.HashMap) MINUTES(java.util.concurrent.TimeUnit.MINUTES) BASE_TX_ID(org.neo4j.storageengine.api.TransactionIdStore.BASE_TX_ID) AtomicReference(java.util.concurrent.atomic.AtomicReference) ConcurrentMap(java.util.concurrent.ConcurrentMap) ReadOnlyDbException(org.neo4j.kernel.api.exceptions.ReadOnlyDbException) Inject(org.neo4j.test.extension.Inject) ThreadLocalRandom(java.util.concurrent.ThreadLocalRandom) GBPTreeCountsStore.relationshipKey(org.neo4j.internal.counts.GBPTreeCountsStore.relationshipKey) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) OtherThreadExecutor(org.neo4j.test.OtherThreadExecutor) MemoryTracker(org.neo4j.memory.MemoryTracker) PageCacheExtension(org.neo4j.test.extension.pagecache.PageCacheExtension) IOException(java.io.IOException) Mockito.when(org.mockito.Mockito.when) Mockito.verify(org.mockito.Mockito.verify) TimeUnit(java.util.concurrent.TimeUnit) AtomicLong(java.util.concurrent.atomic.AtomicLong) RecoveryCleanupWorkCollector.immediate(org.neo4j.index.internal.gbptree.RecoveryCleanupWorkCollector.immediate) AfterEach(org.junit.jupiter.api.AfterEach) ANY_RELATIONSHIP_TYPE(org.neo4j.token.api.TokenConstants.ANY_RELATIONSHIP_TYPE) Race(org.neo4j.test.Race) FileSystemAbstraction(org.neo4j.io.fs.FileSystemAbstraction) AtomicLong(java.util.concurrent.atomic.AtomicLong) Random(java.util.Random) ThreadLocalRandom(java.util.concurrent.ThreadLocalRandom)

Example 2 with RandomRule

use of org.neo4j.test.rule.RandomRule in project neo4j by neo4j.

the class RandomExtension method createField.

@Override
protected RandomRule createField(ExtensionContext extensionContext) {
    RandomRule randomRule = new RandomRule();
    randomRule.setSeed(System.currentTimeMillis());
    return randomRule;
}
Also used : RandomRule(org.neo4j.test.rule.RandomRule)

Example 3 with RandomRule

use of org.neo4j.test.rule.RandomRule in project neo4j by neo4j.

the class NumberArrayTest method arrays.

public static Collection<NumberArrayTestData> arrays() {
    PageCache pageCache = fixture.pageCache;
    Path dir = fixture.directory;
    NullLog log = NullLog.getInstance();
    Collection<NumberArrayTestData> list = new ArrayList<>();
    Map<String, NumberArrayFactory> factories = new HashMap<>();
    factories.put("HEAP", NumberArrayFactories.HEAP);
    factories.put("OFF_HEAP", NumberArrayFactories.OFF_HEAP);
    factories.put("AUTO_WITHOUT_PAGECACHE", NumberArrayFactories.AUTO_WITHOUT_PAGECACHE);
    factories.put("CHUNKED_FIXED_SIZE", NumberArrayFactories.CHUNKED_FIXED_SIZE);
    factories.put("autoWithPageCacheFallback", NumberArrayFactories.auto(pageCache, NULL, dir, true, NO_MONITOR, log, DEFAULT_DATABASE_NAME));
    factories.put("PageCachedNumberArrayFactory", new PageCachedNumberArrayFactory(pageCache, NULL, dir, log, DEFAULT_DATABASE_NAME));
    for (Map.Entry<String, NumberArrayFactory> entry : factories.entrySet()) {
        String name = entry.getKey() + " => ";
        NumberArrayFactory factory = entry.getValue();
        list.add(arrayData(name + "IntArray", factory.newIntArray(INDEXES, -1, INSTANCE), random -> random.nextInt(1_000_000_000), (array, index, value) -> array.set(index, (Integer) value), IntArray::get));
        list.add(arrayData(name + "DynamicIntArray", factory.newDynamicIntArray(CHUNK_SIZE, -1, INSTANCE), random -> random.nextInt(1_000_000_000), (array, index, value) -> array.set(index, (Integer) value), IntArray::get));
        list.add(arrayData(name + "LongArray", factory.newLongArray(INDEXES, -1, INSTANCE), random -> random.nextLong(1_000_000_000), (array, index, value) -> array.set(index, (Long) value), LongArray::get));
        list.add(arrayData(name + "DynamicLongArray", factory.newDynamicLongArray(CHUNK_SIZE, -1, INSTANCE), random -> random.nextLong(1_000_000_000), (array, index, value) -> array.set(index, (Long) value), LongArray::get));
        list.add(arrayData(name + "ByteArray5", factory.newByteArray(INDEXES, defaultByteArray(5), INSTANCE), random -> random.nextInt(1_000_000_000), (array, index, value) -> array.setInt(index, 1, (Integer) value), (array, index) -> array.getInt(index, 1)));
        list.add(arrayData(name + "DynamicByteArray5", factory.newDynamicByteArray(CHUNK_SIZE, defaultByteArray(5), INSTANCE), random -> random.nextInt(1_000_000_000), (array, index, value) -> array.setInt(index, 1, (Integer) value), (array, index) -> array.getInt(index, 1)));
        Function<RandomRule, Object> valueGenerator = random -> new long[] { random.nextLong(), random.nextInt(), (short) random.nextInt(), (byte) random.nextInt() };
        Writer<ByteArray> writer = (array, index, value) -> {
            long[] values = (long[]) value;
            array.setLong(index, 0, values[0]);
            array.setInt(index, 8, (int) values[1]);
            array.setShort(index, 12, (short) values[2]);
            array.setByte(index, 14, (byte) values[3]);
        };
        Reader<ByteArray> reader = (array, index) -> new long[] { array.getLong(index, 0), array.getInt(index, 8), array.getShort(index, 12), array.getByte(index, 14) };
        list.add(arrayData(name + "ByteArray15", factory.newByteArray(INDEXES, defaultByteArray(15), INSTANCE), valueGenerator, writer, reader));
        list.add(arrayData(name + "DynamicByteArray15", factory.newDynamicByteArray(CHUNK_SIZE, defaultByteArray(15), INSTANCE), valueGenerator, writer, reader));
    }
    return list;
}
Also used : Path(java.nio.file.Path) PageCache(org.neo4j.io.pagecache.PageCache) Arrays(java.util.Arrays) TestFactory(org.junit.jupiter.api.TestFactory) Collection(java.util.Collection) IOException(java.io.IOException) HashMap(java.util.HashMap) NULL(org.neo4j.io.pagecache.tracing.PageCacheTracer.NULL) Function(java.util.function.Function) NO_MONITOR(org.neo4j.internal.batchimport.cache.NumberArrayFactories.NO_MONITOR) ArrayList(java.util.ArrayList) AfterAll(org.junit.jupiter.api.AfterAll) Assertions.assertArrayEquals(org.junit.jupiter.api.Assertions.assertArrayEquals) Integer.max(java.lang.Integer.max) DEFAULT_DATABASE_NAME(org.neo4j.configuration.GraphDatabaseSettings.DEFAULT_DATABASE_NAME) INSTANCE(org.neo4j.memory.EmptyMemoryTracker.INSTANCE) Stream(java.util.stream.Stream) BeforeAll(org.junit.jupiter.api.BeforeAll) RandomRule(org.neo4j.test.rule.RandomRule) Map(java.util.Map) DynamicTest(org.junit.jupiter.api.DynamicTest) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) NullLog(org.neo4j.logging.NullLog) Path(java.nio.file.Path) ThrowingConsumer(org.junit.jupiter.api.function.ThrowingConsumer) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) RandomRule(org.neo4j.test.rule.RandomRule) NullLog(org.neo4j.logging.NullLog) HashMap(java.util.HashMap) Map(java.util.Map) PageCache(org.neo4j.io.pagecache.PageCache)

Aggregations

RandomRule (org.neo4j.test.rule.RandomRule)3 IOException (java.io.IOException)2 Path (java.nio.file.Path)2 HashMap (java.util.HashMap)2 Map (java.util.Map)2 Assertions.assertEquals (org.junit.jupiter.api.Assertions.assertEquals)2 DEFAULT_DATABASE_NAME (org.neo4j.configuration.GraphDatabaseSettings.DEFAULT_DATABASE_NAME)2 Integer.max (java.lang.Integer.max)1 String.format (java.lang.String.format)1 ByteBuffer (java.nio.ByteBuffer)1 NoSuchFileException (java.nio.file.NoSuchFileException)1 StandardOpenOption (java.nio.file.StandardOpenOption)1 ArrayList (java.util.ArrayList)1 Arrays (java.util.Arrays)1 Collection (java.util.Collection)1 Random (java.util.Random)1 Set (java.util.Set)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 ConcurrentMap (java.util.concurrent.ConcurrentMap)1 Future (java.util.concurrent.Future)1