Search in sources :

Example 21 with EventHandlers

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

the class PushImageStep method makeListForManifestList.

static ImmutableList<PushImageStep> makeListForManifestList(BuildContext buildContext, ProgressEventDispatcher.Factory progressEventDispatcherFactory, RegistryClient registryClient, ManifestTemplate manifestList, boolean manifestListAlreadyExists) throws IOException {
    Set<String> tags = buildContext.getAllTargetImageTags();
    EventHandlers eventHandlers = buildContext.getEventHandlers();
    try (TimerEventDispatcher ignored = new TimerEventDispatcher(eventHandlers, "Preparing manifest list pushers");
        ProgressEventDispatcher progressEventDispatcher = progressEventDispatcherFactory.create("launching manifest list pushers", tags.size())) {
        boolean singlePlatform = buildContext.getContainerConfiguration().getPlatforms().size() == 1;
        if (singlePlatform) {
            // single image; no need to push a manifest list
            return ImmutableList.of();
        }
        if (JibSystemProperties.skipExistingImages() && manifestListAlreadyExists) {
            eventHandlers.dispatch(LogEvent.info("Skipping pushing manifest list; already exists."));
            return ImmutableList.of();
        }
        DescriptorDigest manifestListDigest = Digests.computeJsonDigest(manifestList);
        return tags.stream().map(tag -> new PushImageStep(buildContext, progressEventDispatcher.newChildProducer(), registryClient, manifestList, tag, manifestListDigest, // return value and type.
        manifestListDigest)).collect(ImmutableList.toImmutableList());
    }
}
Also used : TimerEventDispatcher(com.google.cloud.tools.jib.builder.TimerEventDispatcher) BuildableManifestTemplate(com.google.cloud.tools.jib.image.json.BuildableManifestTemplate) ImageToJsonTranslator(com.google.cloud.tools.jib.image.json.ImageToJsonTranslator) Set(java.util.Set) IOException(java.io.IOException) Callable(java.util.concurrent.Callable) RegistryException(com.google.cloud.tools.jib.api.RegistryException) BuildContext(com.google.cloud.tools.jib.configuration.BuildContext) Collectors(java.util.stream.Collectors) BlobDescriptor(com.google.cloud.tools.jib.blob.BlobDescriptor) DescriptorDigest(com.google.cloud.tools.jib.api.DescriptorDigest) Digests(com.google.cloud.tools.jib.hash.Digests) RegistryClient(com.google.cloud.tools.jib.registry.RegistryClient) LogEvent(com.google.cloud.tools.jib.api.LogEvent) ImmutableList(com.google.common.collect.ImmutableList) JibSystemProperties(com.google.cloud.tools.jib.global.JibSystemProperties) ManifestTemplate(com.google.cloud.tools.jib.image.json.ManifestTemplate) ProgressEventDispatcher(com.google.cloud.tools.jib.builder.ProgressEventDispatcher) EventHandlers(com.google.cloud.tools.jib.event.EventHandlers) Collections(java.util.Collections) Image(com.google.cloud.tools.jib.image.Image) ProgressEventDispatcher(com.google.cloud.tools.jib.builder.ProgressEventDispatcher) DescriptorDigest(com.google.cloud.tools.jib.api.DescriptorDigest) TimerEventDispatcher(com.google.cloud.tools.jib.builder.TimerEventDispatcher) EventHandlers(com.google.cloud.tools.jib.event.EventHandlers)

Example 22 with EventHandlers

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

the class TimerEventDispatcherTest method testLogging.

@Test
public void testLogging() {
    EventHandlers eventHandlers = EventHandlers.builder().add(TimerEvent.class, timerEventQueue::add).build();
    Mockito.when(mockClock.instant()).thenReturn(Instant.EPOCH);
    try (TimerEventDispatcher parentTimerEventDispatcher = new TimerEventDispatcher(eventHandlers, "description", mockClock, null)) {
        Mockito.when(mockClock.instant()).thenReturn(Instant.EPOCH.plusMillis(1));
        parentTimerEventDispatcher.lap();
        Mockito.when(mockClock.instant()).thenReturn(Instant.EPOCH.plusMillis(1).plusNanos(1));
        try (TimerEventDispatcher ignored = parentTimerEventDispatcher.subTimer("child description")) {
            Mockito.when(mockClock.instant()).thenReturn(Instant.EPOCH.plusMillis(2));
        // Laps on close.
        }
    }
    TimerEvent timerEvent = getNextTimerEvent();
    verifyNoParent(timerEvent);
    verifyStartState(timerEvent);
    verifyDescription(timerEvent, "description");
    TimerEvent.Timer parentTimer = timerEvent.getTimer();
    timerEvent = getNextTimerEvent();
    verifyNoParent(timerEvent);
    verifyStateFirstLap(timerEvent, State.LAP);
    verifyDescription(timerEvent, "description");
    timerEvent = getNextTimerEvent();
    verifyParent(timerEvent, parentTimer);
    verifyStartState(timerEvent);
    verifyDescription(timerEvent, "child description");
    timerEvent = getNextTimerEvent();
    verifyParent(timerEvent, parentTimer);
    verifyStateFirstLap(timerEvent, State.FINISHED);
    verifyDescription(timerEvent, "child description");
    timerEvent = getNextTimerEvent();
    verifyNoParent(timerEvent);
    verifyStateNotFirstLap(timerEvent, State.FINISHED);
    verifyDescription(timerEvent, "description");
    Assert.assertTrue(timerEventQueue.isEmpty());
}
Also used : TimerEvent(com.google.cloud.tools.jib.event.events.TimerEvent) EventHandlers(com.google.cloud.tools.jib.event.EventHandlers) Test(org.junit.Test)

Example 23 with EventHandlers

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

the class ProgressEventTest method testSmoke.

@Test
public void testSmoke() {
    Consumer<ProgressEvent> progressEventConsumer = progressEvent -> {
        Allocation allocation = progressEvent.getAllocation();
        long units = progressEvent.getUnits();
        updateCompletionMap(allocation, units);
    };
    EventHandlers eventHandlers = makeEventHandlers(progressEventConsumer);
    eventHandlers.dispatch(new ProgressEvent(AllocationTree.child1Child, 50));
    Assert.assertEquals(1, allocationCompletionMap.size());
    Assert.assertEquals(50, allocationCompletionMap.get(AllocationTree.child1Child).longValue());
    eventHandlers.dispatch(new ProgressEvent(AllocationTree.child1Child, 50));
    Assert.assertEquals(3, allocationCompletionMap.size());
    Assert.assertEquals(100, allocationCompletionMap.get(AllocationTree.child1Child).longValue());
    Assert.assertEquals(1, allocationCompletionMap.get(AllocationTree.child1).longValue());
    Assert.assertEquals(1, allocationCompletionMap.get(AllocationTree.root).longValue());
    eventHandlers.dispatch(new ProgressEvent(AllocationTree.child2, 200));
    Assert.assertEquals(4, allocationCompletionMap.size());
    Assert.assertEquals(100, allocationCompletionMap.get(AllocationTree.child1Child).longValue());
    Assert.assertEquals(1, allocationCompletionMap.get(AllocationTree.child1).longValue());
    Assert.assertEquals(200, allocationCompletionMap.get(AllocationTree.child2).longValue());
    Assert.assertEquals(2, allocationCompletionMap.get(AllocationTree.root).longValue());
}
Also used : Consumer(java.util.function.Consumer) Allocation(com.google.cloud.tools.jib.event.progress.Allocation) Map(java.util.Map) EventHandlers(com.google.cloud.tools.jib.event.EventHandlers) HashMap(java.util.HashMap) Test(org.junit.Test) Assert(org.junit.Assert) Allocation(com.google.cloud.tools.jib.event.progress.Allocation) EventHandlers(com.google.cloud.tools.jib.event.EventHandlers) Test(org.junit.Test)

Example 24 with EventHandlers

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

the class ProgressEventTest method testType.

@Test
public void testType() {
    // Used to test whether or not progress event was consumed
    boolean[] called = new boolean[] { false };
    Consumer<ProgressEvent> buildImageConsumer = progressEvent -> {
        called[0] = true;
    };
    EventHandlers eventHandlers = makeEventHandlers(buildImageConsumer);
    eventHandlers.dispatch(new ProgressEvent(AllocationTree.child1, 50));
    Assert.assertTrue(called[0]);
}
Also used : Consumer(java.util.function.Consumer) Allocation(com.google.cloud.tools.jib.event.progress.Allocation) Map(java.util.Map) EventHandlers(com.google.cloud.tools.jib.event.EventHandlers) HashMap(java.util.HashMap) Test(org.junit.Test) Assert(org.junit.Assert) EventHandlers(com.google.cloud.tools.jib.event.EventHandlers) Test(org.junit.Test)

Example 25 with EventHandlers

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

Aggregations

EventHandlers (com.google.cloud.tools.jib.event.EventHandlers)42 ProgressEventDispatcher (com.google.cloud.tools.jib.builder.ProgressEventDispatcher)24 TimerEventDispatcher (com.google.cloud.tools.jib.builder.TimerEventDispatcher)22 Test (org.junit.Test)14 DescriptorDigest (com.google.cloud.tools.jib.api.DescriptorDigest)10 Image (com.google.cloud.tools.jib.image.Image)10 IOException (java.io.IOException)10 RegistryClient (com.google.cloud.tools.jib.registry.RegistryClient)8 ImmutableList (com.google.common.collect.ImmutableList)8 Map (java.util.Map)8 RegistryException (com.google.cloud.tools.jib.api.RegistryException)6 Platform (com.google.cloud.tools.jib.api.buildplan.Platform)6 BlobDescriptor (com.google.cloud.tools.jib.blob.BlobDescriptor)6 Cache (com.google.cloud.tools.jib.cache.Cache)6 Allocation (com.google.cloud.tools.jib.event.progress.Allocation)6 BuildableManifestTemplate (com.google.cloud.tools.jib.image.json.BuildableManifestTemplate)6 ImageToJsonTranslator (com.google.cloud.tools.jib.image.json.ImageToJsonTranslator)6 ManifestTemplate (com.google.cloud.tools.jib.image.json.ManifestTemplate)6 ArrayList (java.util.ArrayList)6 HashMap (java.util.HashMap)6