Skip to content

Commit

Permalink
Use actions/cache v4.2.0 (helidon-io#9729)
Browse files Browse the repository at this point in the history
* Use actions/cache v4.2.0

* Fix TestSpanListenersWithInjection to register listener on CDI container startup.
- Move io.helidon.common.Functions.UncheckedException to io.helidon.common.UncheckedException
- Fix InvocationTargetException handling for the instrumented invocations
  • Loading branch information
romain-grecourt authored Feb 4, 2025
1 parent 16f9948 commit 88b4977
Show file tree
Hide file tree
Showing 7 changed files with 69 additions and 49 deletions.
8 changes: 4 additions & 4 deletions .github/actions/common/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ runs:
java-version: ${{ env.JAVA_VERSION }}
- name: Cache local Maven repository (read-write)
if: ${{ inputs.maven-cache == 'read-write' }}
uses: actions/cache@v4.0.2
uses: actions/cache@v4.2.0
with:
# See https://github.com/actions/toolkit/issues/713
# Include must not match top level directories
Expand All @@ -98,7 +98,7 @@ runs:
local-maven-
- name: Cache local Maven repository (read-only)
if: ${{ inputs.maven-cache == 'read-only' }}
uses: actions/cache/restore@v4.0.2
uses: actions/cache/restore@v4.2.0
with:
path: |
.m2/repository/**/*.*
Expand All @@ -109,7 +109,7 @@ runs:
local-maven-
- name: Build cache (read-write)
if: ${{ inputs.build-cache == 'read-write' }}
uses: actions/cache@v4.0.2
uses: actions/cache@v4.2.0
with:
path: |
./**/target/**
Expand All @@ -121,7 +121,7 @@ runs:
build-cache-${{ github.run_id }}-
- name: Build cache (read-only)
if: ${{ inputs.build-cache == 'read-only' }}
uses: actions/cache/restore@v4.0.2
uses: actions/cache/restore@v4.2.0
with:
path: |
./**/target/**
Expand Down
17 changes: 0 additions & 17 deletions common/common/src/main/java/io/helidon/common/Functions.java
Original file line number Diff line number Diff line change
Expand Up @@ -246,21 +246,4 @@ public interface CheckedSupplier<T, E extends Throwable> {
*/
T get() throws E;
}

/**
* Unchecked exception.
*
* @see #unwrap(Throwable)
*/
public static class UncheckedException extends RuntimeException {

/**
* Create a new unchecked exception.
*
* @param cause cause
*/
public UncheckedException(Throwable cause) {
super(cause);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
* Copyright (c) 2025 Oracle and/or its affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.helidon.common;

/**
* Unchecked exception.
*/
public class UncheckedException extends RuntimeException {

/**
* Create a new unchecked exception.
*
* @param cause cause
*/
public UncheckedException(Throwable cause) {
super(cause);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,10 @@
import io.opentelemetry.api.trace.SpanBuilder;
import io.opentelemetry.api.trace.Tracer;
import io.opentelemetry.context.Scope;
import jakarta.enterprise.context.ApplicationScoped;
import jakarta.enterprise.context.Initialized;
import jakarta.enterprise.event.Observes;
import jakarta.inject.Inject;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

Expand All @@ -41,21 +43,18 @@
@AddBean(TestSpanListenersWithInjection.MyBean.class)
class TestSpanListenersWithInjection {

private static MyListener listener;
private static final MyListener LISTENER = new MyListener();

@Inject
private MyBean myBean;

@BeforeAll
static void setup() {
listener = new MyListener();
io.helidon.tracing.Tracer.global().register(listener);
}

@BeforeEach
void clearListener() {
listener.clear();
LISTENER.clear();
}

void onStartup(@Observes @Initialized(ApplicationScoped.class) Object event) {
io.helidon.tracing.Tracer.global().register(LISTENER);
}

@Test
Expand Down Expand Up @@ -108,13 +107,14 @@ private void checkListenerCounts(String message,
int expectedEnded,
int expectedActivated,
int expectedClosed) {
assertThat(message + ": Starting spans", listener.starting(), hasSize(expectedStarting));
assertThat(message + ": Started spans", listener.started(), hasSize(expectedStarted));
assertThat(message + ": Ended spans", listener.ended(), hasSize(expectedEnded));
assertThat(message + ": Activated scopes", listener.activated().keySet(), hasSize(expectedActivated));
assertThat(message + ": Closed scopes", listener.closed().keySet(), hasSize(expectedClosed));
assertThat(message + ": Starting spans", LISTENER.starting(), hasSize(expectedStarting));
assertThat(message + ": Started spans", LISTENER.started(), hasSize(expectedStarted));
assertThat(message + ": Ended spans", LISTENER.ended(), hasSize(expectedEnded));
assertThat(message + ": Activated scopes", LISTENER.activated().keySet(), hasSize(expectedActivated));
assertThat(message + ": Closed scopes", LISTENER.closed().keySet(), hasSize(expectedClosed));
}

@ApplicationScoped
static class MyBean {

@Inject
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,15 @@
import java.lang.invoke.MethodHandles;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.List;
import java.util.function.BiFunction;
import java.util.stream.Stream;

import io.helidon.common.UncheckedException;

import net.bytebuddy.ByteBuddy;
import net.bytebuddy.description.method.MethodDescription;
import net.bytebuddy.dynamic.DynamicType;
Expand Down Expand Up @@ -100,7 +103,14 @@ static <T> Class<? extends T> instrument(Class<T> type,
return instrument(type, annotations, methodExcludes, (proxy, method, args) -> {
T instance = resolver.apply(type, method);
if (instance != null) {
return invoke(Object.class, method, instance, args);
try {
return invoke(Object.class, method, instance, args);
} catch (UncheckedException e) {
if (e.getCause() instanceof InvocationTargetException te) {
throw te;
}
throw e;
}
}
return null;
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@
import java.util.stream.Collectors;
import java.util.stream.Stream;

import io.helidon.common.UncheckedException;

/**
* Reflection helper.
*/
Expand Down Expand Up @@ -285,14 +287,8 @@ static <T> T invoke(Class<T> type, Method method, Object instance, Object... arg
method.setAccessible(true);
Object value = method.invoke(instance, args);
return type.cast(value);
} catch (IllegalAccessException e) {
throw new RuntimeException(e);
} catch (InvocationTargetException e) {
Throwable target = e.getTargetException();
if (e.getTargetException() instanceof RuntimeException re) {
throw re;
}
throw new RuntimeException(target);
} catch (IllegalAccessException | InvocationTargetException e) {
throw new UncheckedException(e);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.Semaphore;

import io.helidon.common.Functions.UncheckedException;
import io.helidon.common.UncheckedException;
import io.helidon.common.context.Context;
import io.helidon.common.context.Contexts;
import io.helidon.microprofile.testing.HelidonTestContainer;
Expand Down Expand Up @@ -268,13 +268,13 @@ private Object invoke(Object proxy, Method method, Object[] args) throws Throwab
method.setAccessible(true);
return method.invoke(instance, args);
} catch (InvocationTargetException e) {
if (e.getTargetException() instanceof RuntimeException) {
throw (RuntimeException) e.getTargetException();
}
throw new UncheckedException(e.getTargetException());
throw new UncheckedException(e);
}
});
} catch (UncheckedException e) {
if (e.getCause() instanceof InvocationTargetException te) {
throw te.getCause();
}
throw e.getCause();
}
}
Expand Down

0 comments on commit 88b4977

Please sign in to comment.