Search in sources :

Example 1 with ProgressEvent

use of com.google.cloud.tools.jib.event.events.ProgressEvent in project jib by GoogleContainerTools.

the class ProgressEventDispatcher method dispatchProgress.

/**
 * Dispatches a {@link ProgressEvent} representing {@code progressUnits} of progress on the
 * managed {@link #allocation}.
 *
 * @param progressUnits units of progress
 */
public void dispatchProgress(long progressUnits) {
    long unitsDecremented = decrementRemainingAllocationUnits(progressUnits);
    eventHandlers.dispatch(new ProgressEvent(allocation, unitsDecremented));
}
Also used : ProgressEvent(com.google.cloud.tools.jib.event.events.ProgressEvent)

Example 2 with ProgressEvent

use of com.google.cloud.tools.jib.event.events.ProgressEvent in project jib by google.

the class ProgressEventDispatcher method dispatchProgress.

/**
 * Dispatches a {@link ProgressEvent} representing {@code progressUnits} of progress on the
 * managed {@link #allocation}.
 *
 * @param progressUnits units of progress
 */
public void dispatchProgress(long progressUnits) {
    long unitsDecremented = decrementRemainingAllocationUnits(progressUnits);
    eventHandlers.dispatch(new ProgressEvent(allocation, unitsDecremented));
}
Also used : ProgressEvent(com.google.cloud.tools.jib.event.events.ProgressEvent)

Example 3 with ProgressEvent

use of com.google.cloud.tools.jib.event.events.ProgressEvent in project jib by google.

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)

Example 4 with ProgressEvent

use of com.google.cloud.tools.jib.event.events.ProgressEvent 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

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