Search in sources :

Example 6 with MultithreadedExecutor

use of com.google.cloud.tools.jib.MultithreadedExecutor in project jib by GoogleContainerTools.

the class ProgressEventHandlerTest method testAccept.

@Test
public void testAccept() throws ExecutionException, InterruptedException, IOException {
    try (MultithreadedExecutor multithreadedExecutor = new MultithreadedExecutor()) {
        DoubleAccumulator maxProgress = new DoubleAccumulator(Double::max, 0);
        ProgressEventHandler progressEventHandler = new ProgressEventHandler(update -> maxProgress.accumulate(update.getProgress()));
        EventHandlers eventHandlers = EventHandlers.builder().add(ProgressEvent.class, progressEventHandler).build();
        // Adds root, child1, and child1Child.
        multithreadedExecutor.invoke(() -> {
            eventHandlers.dispatch(new ProgressEvent(AllocationTree.root, 0L));
            return null;
        });
        multithreadedExecutor.invoke(() -> {
            eventHandlers.dispatch(new ProgressEvent(AllocationTree.child1, 0L));
            return null;
        });
        multithreadedExecutor.invoke(() -> {
            eventHandlers.dispatch(new ProgressEvent(AllocationTree.child1Child, 0L));
            return null;
        });
        Assert.assertEquals(0.0, maxProgress.get(), DOUBLE_ERROR_MARGIN);
        // Adds 50 to child1Child and 100 to child2.
        List<Callable<Void>> callables = new ArrayList<>(150);
        callables.addAll(Collections.nCopies(50, () -> {
            eventHandlers.dispatch(new ProgressEvent(AllocationTree.child1Child, 1L));
            return null;
        }));
        callables.addAll(Collections.nCopies(100, () -> {
            eventHandlers.dispatch(new ProgressEvent(AllocationTree.child2, 1L));
            return null;
        }));
        multithreadedExecutor.invokeAll(callables);
        Assert.assertEquals(1.0 / 2 / 100 * 50 + 1.0 / 2 / 200 * 100, maxProgress.get(), DOUBLE_ERROR_MARGIN);
        // 0 progress doesn't do anything.
        multithreadedExecutor.invokeAll(Collections.nCopies(100, () -> {
            eventHandlers.dispatch(new ProgressEvent(AllocationTree.child1, 0L));
            return null;
        }));
        Assert.assertEquals(1.0 / 2 / 100 * 50 + 1.0 / 2 / 200 * 100, maxProgress.get(), DOUBLE_ERROR_MARGIN);
        // Adds 50 to child1Child and 100 to child2 to finish it up.
        multithreadedExecutor.invokeAll(callables);
        Assert.assertEquals(1.0, maxProgress.get(), DOUBLE_ERROR_MARGIN);
    }
}
Also used : DoubleAccumulator(java.util.concurrent.atomic.DoubleAccumulator) ArrayList(java.util.ArrayList) MultithreadedExecutor(com.google.cloud.tools.jib.MultithreadedExecutor) EventHandlers(com.google.cloud.tools.jib.event.EventHandlers) ProgressEvent(com.google.cloud.tools.jib.event.events.ProgressEvent) Callable(java.util.concurrent.Callable) Test(org.junit.Test)

Aggregations

MultithreadedExecutor (com.google.cloud.tools.jib.MultithreadedExecutor)6 Test (org.junit.Test)6 ArrayList (java.util.ArrayList)4 Callable (java.util.concurrent.Callable)4 EventHandlers (com.google.cloud.tools.jib.event.EventHandlers)2 ProgressEvent (com.google.cloud.tools.jib.event.events.ProgressEvent)2 DoubleAccumulator (java.util.concurrent.atomic.DoubleAccumulator)2 Lock (java.util.concurrent.locks.Lock)2 ReentrantLock (java.util.concurrent.locks.ReentrantLock)2