use of org.neo4j.test.OtherThreadExecutor.WorkerCommand in project neo4j by neo4j.
the class TicketedProcessingTest method shouldReturnTicketsInOrder.
@Test
public void shouldReturnTicketsInOrder() throws Exception {
// GIVEN
int items = 1_000;
ParkStrategy park = new ParkStrategy.Park(2, MILLISECONDS);
BiFunction<Integer, Void, Integer> processor = (from, ignore) -> {
if (ThreadLocalRandom.current().nextFloat() < 0.01f) {
park.park(Thread.currentThread());
}
return from * 2;
};
int processorCount = Runtime.getRuntime().availableProcessors();
Future<Void> assertions;
try (TicketedProcessing<Integer, Void, Integer> processing = new TicketedProcessing<>("Doubler", processorCount, processor, () -> null)) {
processing.processors(processorCount - processing.processors(0));
// WHEN
assertions = t2.execute(new WorkerCommand<Void, Void>() {
@Override
public Void doWork(Void state) throws Exception {
for (int i = 0; i < items; i++) {
Integer next = processing.next();
assertNotNull(next);
assertEquals(i * 2, next.intValue());
}
assertNull(processing.next());
return null;
}
});
for (int i = 0; i < items; i++) {
processing.submit(i);
}
}
// THEN
assertions.get();
}
use of org.neo4j.test.OtherThreadExecutor.WorkerCommand in project neo4j by neo4j.
the class PropertyConstraintsStressIT method performInserts.
/**
* Inserts a bunch of new nodes with the label and property key currently set in the fields in this class, where
* running this method twice will insert nodes with duplicate property values, assuming property key or label has
* not changed.
*/
private WorkerCommand<Object, Integer> performInserts(final HighlyAvailableGraphDatabase slave, final boolean constraintCompliant) {
return new WorkerCommand<Object, Integer>() {
@Override
public Integer doWork(Object state) throws Exception {
int i = 0;
try {
for (; i < 100; i++) {
try (Transaction tx = slave.beginTx()) {
constraintOps.createEntity(slave, labelOrRelType, property, "value" + i, constraintCompliant);
tx.success();
}
}
} catch (TransactionFailureException | TransientTransactionFailureException e) {
// Swallowed on purpose, we except it to fail sometimes due to either
// - constraint violation on master
// - concurrent schema operation on master
} catch (ConstraintViolationException e) {
// Constraint violation detected on slave while building transaction
} catch (ComException e) {
// Happens sometimes, cause:
// - The lock session requested to start is already in use.
// Please retry your request in a few seconds.
}
return i;
}
};
}
use of org.neo4j.test.OtherThreadExecutor.WorkerCommand in project neo4j by neo4j.
the class PropertyConstraintsStressIT method createPropertyExistenceConstraintCommand.
private static WorkerCommand<Object, Boolean> createPropertyExistenceConstraintCommand(final GraphDatabaseService db, final String query) {
return new WorkerCommand<Object, Boolean>() {
@Override
public Boolean doWork(Object state) throws Exception {
boolean constraintCreationFailed = false;
try (Transaction tx = db.beginTx()) {
db.execute(query);
tx.success();
} catch (QueryExecutionException e) {
System.out.println("Constraint failed: " + e.getMessage());
if (Exceptions.rootCause(e) instanceof ConstraintValidationException) {
// Unable to create constraint since it is not consistent with existing data
constraintCreationFailed = true;
} else {
throw e;
}
}
if (!constraintCreationFailed) {
System.out.println("Constraint created: " + query);
}
return constraintCreationFailed;
}
};
}
Aggregations