Search in sources :

Example 6 with Race

use of org.neo4j.test.Race in project neo4j by neo4j.

the class RecordCheckWorkerTest method shouldDoProcessingInitializationInOrder.

@Test
public void shouldDoProcessingInitializationInOrder() throws Throwable {
    // GIVEN
    final Race race = new Race();
    final AtomicInteger coordination = new AtomicInteger(-1);
    final AtomicInteger expected = new AtomicInteger();
    final int threads = 30;
    @SuppressWarnings("unchecked") final RecordCheckWorker<Integer>[] workers = new RecordCheckWorker[threads];
    final RecordProcessor<Integer> processor = new RecordProcessor.Adapter<Integer>() {

        @Override
        public void process(Integer record) {
        // We're testing init() here, not really process()
        }

        @Override
        public void init(int id) {
            assertEquals(id, expected.getAndAdd(1));
        }
    };
    for (int id = 0; id < threads; id++) {
        ArrayBlockingQueue<Integer> queue = new ArrayBlockingQueue<>(10);
        race.addContestant(workers[id] = new RecordCheckWorker<>(id, coordination, queue, processor));
    }
    race.addContestant(new Runnable() {

        @Override
        public void run() {
            try {
                long end = currentTimeMillis() + SECONDS.toMillis(100);
                while (currentTimeMillis() < end && expected.get() < threads) {
                    parkNanos(MILLISECONDS.toNanos(10));
                }
                assertEquals(threads, expected.get());
            } finally {
                for (RecordCheckWorker<Integer> worker : workers) {
                    worker.done();
                }
            }
        }
    });
    // WHEN
    race.go();
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ArrayBlockingQueue(java.util.concurrent.ArrayBlockingQueue) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Race(org.neo4j.test.Race) Test(org.junit.Test)

Example 7 with Race

use of org.neo4j.test.Race in project neo4j by neo4j.

the class NeoStoresIT method shouldWriteOutThePropertyRecordBeforeReferencingItFromANodeRecord.

@Test
public void shouldWriteOutThePropertyRecordBeforeReferencingItFromANodeRecord() throws Throwable {
    Race race = new Race();
    long[] latestNodeId = new long[1];
    AtomicLong writes = new AtomicLong();
    AtomicLong reads = new AtomicLong();
    long endTime = currentTimeMillis() + SECONDS.toMillis(2);
    race.withEndCondition(() -> (writes.get() > 100 && reads.get() > 10_000) || currentTimeMillis() > endTime);
    race.addContestant(() -> {
        try (Transaction tx = db.beginTx()) {
            Node node = db.createNode();
            latestNodeId[0] = node.getId();
            node.setProperty("largeProperty", LONG_STRING_VALUE);
            tx.success();
        }
        writes.incrementAndGet();
    });
    race.addContestant(() -> {
        try (Transaction tx = db.getGraphDatabaseAPI().beginTx()) {
            Node node = db.getGraphDatabaseAPI().getNodeById(latestNodeId[0]);
            for (String propertyKey : node.getPropertyKeys()) {
                node.getProperty(propertyKey);
            }
            tx.success();
        } catch (NotFoundException e) {
            if (Exceptions.contains(e, InvalidRecordException.class)) {
                throw e;
            }
        }
        reads.incrementAndGet();
    });
    race.go();
}
Also used : AtomicLong(java.util.concurrent.atomic.AtomicLong) Transaction(org.neo4j.graphdb.Transaction) Race(org.neo4j.test.Race) Node(org.neo4j.graphdb.Node) NotFoundException(org.neo4j.graphdb.NotFoundException) Test(org.junit.Test)

Example 8 with Race

use of org.neo4j.test.Race in project neo4j by neo4j.

the class MetaDataStoreTest method transactionCommittedMustBeAtomic.

@Test
public void transactionCommittedMustBeAtomic() throws Throwable {
    try (MetaDataStore store = newMetaDataStore()) {
        PagedFile pf = store.storeFile;
        store.transactionCommitted(2, 2, 2);
        AtomicLong writeCount = new AtomicLong();
        AtomicLong fileReadCount = new AtomicLong();
        AtomicLong apiReadCount = new AtomicLong();
        int upperLimit = 10_000;
        int lowerLimit = 100;
        long endTime = currentTimeMillis() + SECONDS.toMillis(10);
        Race race = new Race();
        race.withEndCondition(() -> writeCount.get() >= upperLimit && fileReadCount.get() >= upperLimit && apiReadCount.get() >= upperLimit);
        race.withEndCondition(() -> writeCount.get() >= lowerLimit && fileReadCount.get() >= lowerLimit && apiReadCount.get() >= lowerLimit && currentTimeMillis() >= endTime);
        race.addContestants(3, () -> {
            long count = writeCount.incrementAndGet();
            store.transactionCommitted(count, count, count);
        });
        race.addContestants(3, throwing(() -> {
            try (PageCursor cursor = pf.io(0, PagedFile.PF_SHARED_READ_LOCK)) {
                assertTrue(cursor.next());
                long id, checksum;
                do {
                    id = store.getRecordValue(cursor, MetaDataStore.Position.LAST_TRANSACTION_ID);
                    checksum = store.getRecordValue(cursor, MetaDataStore.Position.LAST_TRANSACTION_CHECKSUM);
                } while (cursor.shouldRetry());
                assertIdEqualsChecksum(id, checksum, "file");
                fileReadCount.incrementAndGet();
            }
        }));
        race.addContestants(3, () -> {
            TransactionId transaction = store.getLastCommittedTransaction();
            assertIdEqualsChecksum(transaction.transactionId(), transaction.checksum(), "API");
            apiReadCount.incrementAndGet();
        });
        race.go();
    }
}
Also used : AtomicLong(java.util.concurrent.atomic.AtomicLong) PagedFile(org.neo4j.io.pagecache.PagedFile) DelegatingPagedFile(org.neo4j.io.pagecache.DelegatingPagedFile) Race(org.neo4j.test.Race) PageCursor(org.neo4j.io.pagecache.PageCursor) DelegatingPageCursor(org.neo4j.io.pagecache.impl.DelegatingPageCursor) Test(org.junit.Test)

Example 9 with Race

use of org.neo4j.test.Race in project neo4j by neo4j.

the class MetaDataStoreTest method incrementAndGetVersionMustBeAtomic.

@Test
public void incrementAndGetVersionMustBeAtomic() throws Throwable {
    try (MetaDataStore store = newMetaDataStore()) {
        long initialVersion = store.incrementAndGetVersion();
        int threads = Runtime.getRuntime().availableProcessors(), iterations = 500;
        Race race = new Race();
        race.addContestants(threads, () -> {
            for (int i = 0; i < iterations; i++) {
                store.incrementAndGetVersion();
            }
        });
        race.go();
        assertThat(store.incrementAndGetVersion(), is(initialVersion + (threads * iterations) + 1));
    }
}
Also used : Race(org.neo4j.test.Race) Test(org.junit.Test)

Example 10 with Race

use of org.neo4j.test.Race in project neo4j by neo4j.

the class HighestTransactionIdTest method shouldKeepHighestDuringConcurrentOfferings.

@Test
public void shouldKeepHighestDuringConcurrentOfferings() throws Throwable {
    // GIVEN
    final HighestTransactionId highest = new HighestTransactionId(-1, -1, -1);
    Race race = new Race();
    int updaters = max(2, getRuntime().availableProcessors());
    final AtomicInteger accepted = new AtomicInteger();
    for (int i = 0; i < updaters; i++) {
        final long id = i + 1;
        race.addContestant(new Runnable() {

            @Override
            public void run() {
                if (highest.offer(id, id, id)) {
                    accepted.incrementAndGet();
                }
            }
        });
    }
    // WHEN
    race.go();
    // THEN
    assertTrue(accepted.get() > 0);
    assertEquals(updaters, highest.get().transactionId());
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Race(org.neo4j.test.Race) Test(org.junit.Test)

Aggregations

Race (org.neo4j.test.Race)26 Test (org.junit.Test)24 AtomicLong (java.util.concurrent.atomic.AtomicLong)10 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)5 Transaction (org.neo4j.graphdb.Transaction)5 Node (org.neo4j.graphdb.Node)4 NotFoundException (org.neo4j.graphdb.NotFoundException)4 DelegatingPagedFile (org.neo4j.io.pagecache.DelegatingPagedFile)3 IOException (java.io.IOException)2 ThreadLocalRandom (java.util.concurrent.ThreadLocalRandom)2 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2 Relationship (org.neo4j.graphdb.Relationship)2 PageCursor (org.neo4j.io.pagecache.PageCursor)2 PagedFile (org.neo4j.io.pagecache.PagedFile)2 DelegatingPageCursor (org.neo4j.io.pagecache.impl.DelegatingPageCursor)2 File (java.io.File)1 Clock (java.time.Clock)1 ArrayList (java.util.ArrayList)1 ArrayBlockingQueue (java.util.concurrent.ArrayBlockingQueue)1 CountDownLatch (java.util.concurrent.CountDownLatch)1