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);
}
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());
}
}
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);
}
}
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);
}
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());
}
}
Aggregations