use of org.neo4j.unsafe.impl.batchimport.executor.ParkStrategy 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();
}
Aggregations