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();
}
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();
}
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();
}
}
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));
}
}
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());
}
Aggregations