Search in sources :

Example 1 with ParkStrategy

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();
}
Also used : Assert.assertNotNull(org.junit.Assert.assertNotNull) BiFunction(java.util.function.BiFunction) OtherThreadRule(org.neo4j.test.rule.concurrent.OtherThreadRule) Test(org.junit.Test) MILLISECONDS(java.util.concurrent.TimeUnit.MILLISECONDS) Integer.parseInt(java.lang.Integer.parseInt) WorkerCommand(org.neo4j.test.OtherThreadExecutor.WorkerCommand) CountDownLatch(java.util.concurrent.CountDownLatch) Future(java.util.concurrent.Future) Rule(org.junit.Rule) Assert.assertNull(org.junit.Assert.assertNull) ThreadLocalRandom(java.util.concurrent.ThreadLocalRandom) DoubleLatch.awaitLatch(org.neo4j.test.DoubleLatch.awaitLatch) ParkStrategy(org.neo4j.unsafe.impl.batchimport.executor.ParkStrategy) Assert.assertEquals(org.junit.Assert.assertEquals) WorkerCommand(org.neo4j.test.OtherThreadExecutor.WorkerCommand) ParkStrategy(org.neo4j.unsafe.impl.batchimport.executor.ParkStrategy) Test(org.junit.Test)

Aggregations

Integer.parseInt (java.lang.Integer.parseInt)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 Future (java.util.concurrent.Future)1 ThreadLocalRandom (java.util.concurrent.ThreadLocalRandom)1 MILLISECONDS (java.util.concurrent.TimeUnit.MILLISECONDS)1 BiFunction (java.util.function.BiFunction)1 Assert.assertEquals (org.junit.Assert.assertEquals)1 Assert.assertNotNull (org.junit.Assert.assertNotNull)1 Assert.assertNull (org.junit.Assert.assertNull)1 Rule (org.junit.Rule)1 Test (org.junit.Test)1 DoubleLatch.awaitLatch (org.neo4j.test.DoubleLatch.awaitLatch)1 WorkerCommand (org.neo4j.test.OtherThreadExecutor.WorkerCommand)1 OtherThreadRule (org.neo4j.test.rule.concurrent.OtherThreadRule)1 ParkStrategy (org.neo4j.unsafe.impl.batchimport.executor.ParkStrategy)1