Search in sources :

Example 1 with MultithreadedExecutor

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

the class SingleThreadedExecutorTest method testExecute_mutualExclusion.

// use of Thread.yield()
@SuppressWarnings("ThreadPriorityCheck")
@Test
public void testExecute_mutualExclusion() throws IOException, ExecutionException, InterruptedException {
    SingleThreadedExecutor singleThreadedExecutor = new SingleThreadedExecutor();
    Lock lock = new ReentrantLock();
    try (MultithreadedExecutor multithreadedExecutor = new MultithreadedExecutor()) {
        multithreadedExecutor.invokeAll(Collections.nCopies(100, () -> {
            singleThreadedExecutor.execute(() -> {
                Assert.assertTrue(lock.tryLock());
                Thread.yield();
                lock.unlock();
            });
            return null;
        }));
    }
    singleThreadedExecutor.shutDownAndAwaitTermination(SHUTDOWN_TIMEOUT);
}
Also used : ReentrantLock(java.util.concurrent.locks.ReentrantLock) MultithreadedExecutor(com.google.cloud.tools.jib.MultithreadedExecutor) Lock(java.util.concurrent.locks.Lock) ReentrantLock(java.util.concurrent.locks.ReentrantLock) Test(org.junit.Test)

Example 2 with MultithreadedExecutor

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

the class AllocationCompletionTrackerTest method testGetUnfinishedAllocations_multipleThreads.

@Test
public void testGetUnfinishedAllocations_multipleThreads() throws InterruptedException, ExecutionException, IOException {
    try (MultithreadedExecutor multithreadedExecutor = new MultithreadedExecutor()) {
        AllocationCompletionTracker allocationCompletionTracker = new AllocationCompletionTracker();
        // Adds root, child1, and child1Child.
        Assert.assertEquals(true, multithreadedExecutor.invoke(() -> allocationCompletionTracker.updateProgress(AllocationTree.root, 0L)));
        Assert.assertEquals(true, multithreadedExecutor.invoke(() -> allocationCompletionTracker.updateProgress(AllocationTree.child1, 0L)));
        Assert.assertEquals(true, multithreadedExecutor.invoke(() -> allocationCompletionTracker.updateProgress(AllocationTree.child1Child, 0L)));
        Assert.assertEquals(Arrays.asList(AllocationTree.root, AllocationTree.child1, AllocationTree.child1Child), allocationCompletionTracker.getUnfinishedAllocations());
        // Adds 50 to child1Child and 100 to child2.
        List<Callable<Boolean>> callables = new ArrayList<>(150);
        callables.addAll(Collections.nCopies(50, () -> allocationCompletionTracker.updateProgress(AllocationTree.child1Child, 1L)));
        callables.addAll(Collections.nCopies(100, () -> allocationCompletionTracker.updateProgress(AllocationTree.child2, 1L)));
        Assert.assertEquals(Collections.nCopies(150, true), multithreadedExecutor.invokeAll(callables));
        Assert.assertEquals(Arrays.asList(AllocationTree.root, AllocationTree.child1, AllocationTree.child1Child, AllocationTree.child2), allocationCompletionTracker.getUnfinishedAllocations());
        // 0 progress doesn't do anything.
        Assert.assertEquals(Collections.nCopies(100, false), multithreadedExecutor.invokeAll(Collections.nCopies(100, () -> allocationCompletionTracker.updateProgress(AllocationTree.child1, 0L))));
        Assert.assertEquals(Arrays.asList(AllocationTree.root, AllocationTree.child1, AllocationTree.child1Child, AllocationTree.child2), allocationCompletionTracker.getUnfinishedAllocations());
        // Adds 50 to child1Child and 100 to child2 to finish it up.
        Assert.assertEquals(Collections.nCopies(150, true), multithreadedExecutor.invokeAll(callables));
        Assert.assertEquals(Collections.emptyList(), allocationCompletionTracker.getUnfinishedAllocations());
    }
}
Also used : ArrayList(java.util.ArrayList) MultithreadedExecutor(com.google.cloud.tools.jib.MultithreadedExecutor) Callable(java.util.concurrent.Callable) Test(org.junit.Test)

Example 3 with MultithreadedExecutor

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

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

the class SingleThreadedExecutorTest method testExecute_mutualExclusion.

// use of Thread.yield()
@SuppressWarnings("ThreadPriorityCheck")
@Test
public void testExecute_mutualExclusion() throws IOException, ExecutionException, InterruptedException {
    SingleThreadedExecutor singleThreadedExecutor = new SingleThreadedExecutor();
    Lock lock = new ReentrantLock();
    try (MultithreadedExecutor multithreadedExecutor = new MultithreadedExecutor()) {
        multithreadedExecutor.invokeAll(Collections.nCopies(100, () -> {
            singleThreadedExecutor.execute(() -> {
                Assert.assertTrue(lock.tryLock());
                Thread.yield();
                lock.unlock();
            });
            return null;
        }));
    }
    singleThreadedExecutor.shutDownAndAwaitTermination(SHUTDOWN_TIMEOUT);
}
Also used : ReentrantLock(java.util.concurrent.locks.ReentrantLock) MultithreadedExecutor(com.google.cloud.tools.jib.MultithreadedExecutor) Lock(java.util.concurrent.locks.Lock) ReentrantLock(java.util.concurrent.locks.ReentrantLock) Test(org.junit.Test)

Example 5 with MultithreadedExecutor

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

the class AllocationCompletionTrackerTest method testGetUnfinishedAllocations_multipleThreads.

@Test
public void testGetUnfinishedAllocations_multipleThreads() throws InterruptedException, ExecutionException, IOException {
    try (MultithreadedExecutor multithreadedExecutor = new MultithreadedExecutor()) {
        AllocationCompletionTracker allocationCompletionTracker = new AllocationCompletionTracker();
        // Adds root, child1, and child1Child.
        Assert.assertEquals(true, multithreadedExecutor.invoke(() -> allocationCompletionTracker.updateProgress(AllocationTree.root, 0L)));
        Assert.assertEquals(true, multithreadedExecutor.invoke(() -> allocationCompletionTracker.updateProgress(AllocationTree.child1, 0L)));
        Assert.assertEquals(true, multithreadedExecutor.invoke(() -> allocationCompletionTracker.updateProgress(AllocationTree.child1Child, 0L)));
        Assert.assertEquals(Arrays.asList(AllocationTree.root, AllocationTree.child1, AllocationTree.child1Child), allocationCompletionTracker.getUnfinishedAllocations());
        // Adds 50 to child1Child and 100 to child2.
        List<Callable<Boolean>> callables = new ArrayList<>(150);
        callables.addAll(Collections.nCopies(50, () -> allocationCompletionTracker.updateProgress(AllocationTree.child1Child, 1L)));
        callables.addAll(Collections.nCopies(100, () -> allocationCompletionTracker.updateProgress(AllocationTree.child2, 1L)));
        Assert.assertEquals(Collections.nCopies(150, true), multithreadedExecutor.invokeAll(callables));
        Assert.assertEquals(Arrays.asList(AllocationTree.root, AllocationTree.child1, AllocationTree.child1Child, AllocationTree.child2), allocationCompletionTracker.getUnfinishedAllocations());
        // 0 progress doesn't do anything.
        Assert.assertEquals(Collections.nCopies(100, false), multithreadedExecutor.invokeAll(Collections.nCopies(100, () -> allocationCompletionTracker.updateProgress(AllocationTree.child1, 0L))));
        Assert.assertEquals(Arrays.asList(AllocationTree.root, AllocationTree.child1, AllocationTree.child1Child, AllocationTree.child2), allocationCompletionTracker.getUnfinishedAllocations());
        // Adds 50 to child1Child and 100 to child2 to finish it up.
        Assert.assertEquals(Collections.nCopies(150, true), multithreadedExecutor.invokeAll(callables));
        Assert.assertEquals(Collections.emptyList(), allocationCompletionTracker.getUnfinishedAllocations());
    }
}
Also used : ArrayList(java.util.ArrayList) MultithreadedExecutor(com.google.cloud.tools.jib.MultithreadedExecutor) 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