Skip to content

Commit

Permalink
build/refactor: add enable-only-hello-agent-mode maven profile; ref…
Browse files Browse the repository at this point in the history
…actor agent and its test code
  • Loading branch information
oldratlee committed Jul 16, 2023
1 parent 2084e27 commit f19c1d3
Show file tree
Hide file tree
Showing 18 changed files with 526 additions and 43 deletions.
40 changes: 38 additions & 2 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,52 @@ jobs:
distribution: temurin
# only first java setup need enable cache
cache: maven
- name: build and test with Java 17
- name: build and test with Java 17 without agents
run: ./mvnw -V --no-transfer-progress clean install
- name: test under hello and world agents
run: ./mvnw dependency:properties surefire:test
env:
STUDY_AGENT_RUN_MODE: hello-and-world-agents
- name: test under hello agent
run: ./mvnw dependency:properties surefire:test
env:
STUDY_AGENT_RUN_MODE: only-hello-agent
- name: run Main without agents
run: ./mvnw dependency:properties exec:exec -pl main-runner
- name: run Main under hello and world agents
run: ./mvnw dependency:properties exec:exec -pl main-runner
env:
STUDY_AGENT_RUN_MODE: hello-and-world-agents
- name: run Main under hello agent
run: ./mvnw dependency:properties exec:exec -pl main-runner
env:
STUDY_AGENT_RUN_MODE: only-hello-agent

- name: setup Java 8
uses: actions/setup-java@v3
with:
java-version: 8
distribution: zulu
- name: test with Java 8
- name: test with Java 8 without agents
run: ./mvnw -V --no-transfer-progress dependency:properties surefire:test
- name: test under hello and world agents
run: ./mvnw dependency:properties surefire:test
env:
STUDY_AGENT_RUN_MODE: hello-and-world-agents
- name: test under hello agent
run: ./mvnw dependency:properties surefire:test
env:
STUDY_AGENT_RUN_MODE: only-hello-agent
- name: run Main without agents
run: ./mvnw dependency:properties exec:exec -pl main-runner
- name: run Main under hello and world agents
run: ./mvnw dependency:properties exec:exec -pl main-runner
env:
STUDY_AGENT_RUN_MODE: hello-and-world-agents
- name: run Main under hello agent
run: ./mvnw dependency:properties exec:exec -pl main-runner
env:
STUDY_AGENT_RUN_MODE: only-hello-agent

- name: 'remove self maven install files(OS: *nix)'
run: rm -rf $HOME/.m2/repository/io/foldright/study*
Expand Down
26 changes: 23 additions & 3 deletions hello-agent/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,16 @@

<artifactId>hello-agent</artifactId>

<properties>
<project.root.package>io.foldright.study.hello.agent</project.root.package>
</properties>

<dependencies>
<dependency>
<groupId>io.foldright.study</groupId>
<artifactId>utils</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.javassist</groupId>
<artifactId>javassist</artifactId>
Expand All @@ -25,10 +34,10 @@
<configuration>
<archive>
<manifestEntries>
<Premain-Class>io.foldright.study.hello.agent.HelloAgent</Premain-Class>
<Premain-Class>${project.root.package}.HelloAgent</Premain-Class>
<Boot-Class-Path>${project.build.finalName}.jar</Boot-Class-Path>
<Can-Redefine-Classes>false</Can-Redefine-Classes>
<Can-Retransform-Classes>true</Can-Retransform-Classes>
<Can-Redefine-Classes>true</Can-Redefine-Classes>
<Can-Set-Native-Method-Prefix>false</Can-Set-Native-Method-Prefix>
</manifestEntries>
</archive>
Expand All @@ -46,17 +55,28 @@
</goals>
<configuration>
<relocations>
<relocation>
<pattern>io.foldright.study.agent.utils</pattern>
<shadedPattern>${project.root.package}.internal.utils</shadedPattern>
</relocation>
<relocation>
<pattern>javassist</pattern>
<shadedPattern>io.foldright.study.hello.agent.internal.javassist</shadedPattern>
<shadedPattern>${project.root.package}.internal.javassist</shadedPattern>
</relocation>
</relocations>
<artifactSet>
<includes>
<include>io.foldright.study:utils</include>
<include>org.javassist:javassist</include>
</includes>
</artifactSet>
<filters>
<filter>
<artifact>io.foldright.study:utils</artifact>
<excludes>
<exclude>META-INF/MANIFEST.MF</exclude>
</excludes>
</filter>
<filter>
<artifact>org.javassist:javassist</artifact>
<excludes>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,32 @@
package io.foldright.study.hello.agent;

import edu.umd.cs.findbugs.annotations.NonNull;
import io.foldright.study.agent.utils.transform.DispatchTransformer;
import io.foldright.study.agent.utils.transform.ThreadPoolExecutorTransformlet;

import java.lang.instrument.Instrumentation;
import java.util.Collections;
import java.util.List;

import static io.foldright.study.agent.utils.transform.ThreadPoolExecutorTransformlet.THREAD_POOL_EXECUTOR_CLASS_NAME;
import static io.foldright.study.agent.utils.Utils.isClassLoaded;


/**
* a demo agent.
*/
public class HelloAgent {
private static final String NAME = "Hello";

public static void premain(final String agentArgs, @NonNull final Instrumentation inst) {
System.out.println("Hello Agent!");
System.out.println("[" + NAME + "Agent] Enter premain!");

if (isClassLoaded(inst, THREAD_POOL_EXECUTOR_CLASS_NAME))
throw new IllegalStateException("class " + THREAD_POOL_EXECUTOR_CLASS_NAME + " already loaded");

List<ThreadPoolExecutorTransformlet> transformlets = Collections.singletonList(
new ThreadPoolExecutorTransformlet(NAME)
);
inst.addTransformer(new DispatchTransformer(NAME, transformlets));
}
}
72 changes: 59 additions & 13 deletions main-runner/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,8 @@
<properties>
<exec.mainClass>io.foldright.study.main.Main</exec.mainClass>

<agent.opt.hello>-javaagent:${io.foldright.study:hello-agent:jar}</agent.opt.hello>
<dopt.enable.agent.hello>-Denable.hello.agent=true</dopt.enable.agent.hello>
<agent.opt.world>-javaagent:${io.foldright.study:world-agent:jar}</agent.opt.world>
<dopt.enable.agent.world>-Denable.world.agent=true</dopt.enable.agent.world>
<hello.agent.opt>-javaagent:${io.foldright.study:hello-agent:jar}</hello.agent.opt>
<world.agent.opt>-javaagent:${io.foldright.study:world-agent:jar}</world.agent.opt>
</properties>

<dependencies>
Expand Down Expand Up @@ -60,17 +58,20 @@
<profiles>
<!-- agent config mode profiles -->
<profile>
<id>enable-hello-world-agents</id>
<id>enable-hello-and-world-agents-mode</id>
<activation>
<property>
<name>env.STUDY_AGENT_RUN_MODE</name>
<value>hello-and-world-agents</value>
</property>
</activation>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<argLine>@{argLine}
${agent.opt.hello} ${dopt.enable.agent.hello}
${agent.opt.world} ${dopt.enable.agent.world}
</argLine>
<argLine>@{argLine} ${hello.agent.opt} ${world.agent.opt}</argLine>
</configuration>
</plugin>
<plugin>
Expand All @@ -83,10 +84,55 @@
<argument>-Duser.language=en</argument>
<argument>-Duser.country=US</argument>

<argument>${agent.opt.hello}</argument>
<argument>${dopt.enable.agent.hello}</argument>
<argument>${agent.opt.world}</argument>
<argument>${dopt.enable.agent.world}</argument>
<!--
<argument>-agentlib:jdwp=transport=dt_socket,server=y,suspend=y,address=5005</argument>
-->

<argument>${hello.agent.opt}</argument>
<argument>${world.agent.opt}</argument>

<argument>-classpath</argument>
<classpath/>

<argument>${exec.mainClass}</argument>
</arguments>
</configuration>
</plugin>
</plugins>
</build>
</profile>
<profile>
<id>enable-only-hello-agent-mode</id>
<activation>
<property>
<name>env.STUDY_AGENT_RUN_MODE</name>
<value>only-hello-agent</value>
</property>
</activation>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<argLine>@{argLine} ${hello.agent.opt}</argLine>
</configuration>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<configuration>
<arguments>
<argument>-Xms256m</argument>
<argument>-ea</argument>
<argument>-Duser.language=en</argument>
<argument>-Duser.country=US</argument>

<!--
<argument>-agentlib:jdwp=transport=dt_socket,server=y,suspend=y,address=5005</argument>
-->

<argument>${hello.agent.opt}</argument>

<argument>-classpath</argument>
<classpath/>
Expand Down
8 changes: 8 additions & 0 deletions main-runner/src/main/java/io/foldright/study/main/Main.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,15 @@
package io.foldright.study.main;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;


public class Main {
public static void main(String[] args) {
ExecutorService executor = Executors.newCachedThreadPool();
System.out.println("========================================");
System.out.println("Hello World!");
System.out.println(executor);
System.out.println("========================================");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package io.foldright.study.main;

public enum StudyAgentMode {
NO_AGENTS,
HELLO_AND_WORLD_AGENTS,
ONLY_HELLO_AGENT,

;

public static StudyAgentMode getStudyAgentMode() {
final String envVarName = "STUDY_AGENT_RUN_MODE";
final String value = System.getenv(envVarName);
if (value == null || "".equals(value)) {
return NO_AGENTS;
} else if ("hello-and-world-agents".equals(value)) {
return HELLO_AND_WORLD_AGENTS;
} else if ("only-hello-agent".equals(value)) {
return ONLY_HELLO_AGENT;
} else {
throw new IllegalStateException("Illegal value of env var(" + envVarName + "): " + value);
}
}
}
11 changes: 0 additions & 11 deletions main-runner/src/test/java/io/foldright/study/main/MainTest.java

This file was deleted.

36 changes: 36 additions & 0 deletions main-runner/src/test/java/io/foldright/study/main/MainTest.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package io.foldright.study.main

import io.kotest.assertions.fail
import io.kotest.core.spec.style.FunSpec
import io.kotest.matchers.string.shouldContainOnlyOnce
import io.kotest.matchers.string.shouldNotContain
import java.util.concurrent.Executors


class MainTest : FunSpec({
test("agent hacked toString of ThreadPoolExecutor") {
val executor = Executors.newCachedThreadPool() as java.util.concurrent.ThreadPoolExecutor
val addedByHelloTransformlet = "[[[AddedByHelloTransformlet]]]"
val addedByWorldTransformlet = "[[[AddedByWorldTransformlet]]]"

val executorString = executor.toString()
when (val studyAgentMode = StudyAgentMode.getStudyAgentMode()) {
StudyAgentMode.NO_AGENTS -> {
executorString.shouldNotContain(addedByHelloTransformlet)
executorString.shouldNotContain(addedByWorldTransformlet)
}

StudyAgentMode.HELLO_AND_WORLD_AGENTS -> {
executorString.shouldContainOnlyOnce(addedByHelloTransformlet)
executorString.shouldContainOnlyOnce(addedByWorldTransformlet)
}

StudyAgentMode.ONLY_HELLO_AGENT -> {
executorString.shouldContainOnlyOnce(addedByHelloTransformlet)
executorString.shouldNotContain(addedByWorldTransformlet)
}

else -> fail("Unknown StudyAgentMode: $studyAgentMode")
}
}
})
1 change: 1 addition & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
</developers>

<modules>
<module>utils</module>
<module>hello-agent</module>
<module>world-agent</module>
<module>main-runner</module>
Expand Down
36 changes: 27 additions & 9 deletions scripts/integration_test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -52,22 +52,40 @@ jvb::mvn_cmd clean install
# test by multiply version jdks
########################################

for jdk_version in "${JDK_VERSIONS[@]}"; do
# skip default jdk, already tested above
[ "$jdk_version" = "$default_build_jdk_version" ] && continue
# about CI env var
# https://docs.github.com/en/actions/learn-github-actions/variables#default-environment-variables
if [ "${CI:-}" = true ]; then
CI_MORE_BEGIN_OPTS=jacoco:prepare-agent
CI_MORE_END_OPTS=jacoco:report
fi

for jdk_version in "${JDK_VERSIONS[@]}"; do
jh_var_name="JAVA${jdk_version}_HOME"
[ -d "${!jh_var_name:-}" ] || cu::die "\$${jh_var_name}(${!jh_var_name:-}) dir is not existed!"
export JAVA_HOME="${!jh_var_name}"

# just test without build
cu::head_line_echo "test with Java $jdk_version: $JAVA_HOME"

# about CI env var
# https://docs.github.com/en/actions/learn-github-actions/variables#default-environment-variables
if [ "${CI:-}" = true ]; then
jvb::mvn_cmd jacoco:prepare-agent dependency:properties surefire:test jacoco:report
else
jvb::mvn_cmd dependency:properties surefire:test
# skip default jdk, already tested above
if [ "$jdk_version" != "$default_build_jdk_version" ]; then
# just test without build
cu::head_line_echo "test without agents: $JAVA_HOME"
jvb::mvn_cmd ${CI_MORE_BEGIN_OPTS:-} dependency:properties surefire:test ${CI_MORE_END_OPTS:-}
fi

cu::head_line_echo test under hello and world agents
STUDY_AGENT_RUN_MODE=hello-and-world-agents jvb::mvn_cmd ${CI_MORE_BEGIN_OPTS:-} dependency:properties surefire:test ${CI_MORE_END_OPTS:-}

cu::head_line_echo test under hello agent
STUDY_AGENT_RUN_MODE=only-hello-agent jvb::mvn_cmd ${CI_MORE_BEGIN_OPTS:-} dependency:properties surefire:test ${CI_MORE_END_OPTS:-}

cu::head_line_echo run Main without agents
jvb::mvn_cmd dependency:properties exec:exec -pl main-runner

cu::head_line_echo run Main under hello and world agents
STUDY_AGENT_RUN_MODE=hello-and-world-agents jvb::mvn_cmd dependency:properties exec:exec -pl main-runner

cu::head_line_echo run Main under hello agent
STUDY_AGENT_RUN_MODE=only-hello-agent jvb::mvn_cmd dependency:properties exec:exec -pl main-runner
done
Loading

0 comments on commit f19c1d3

Please sign in to comment.