-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
build/refactor: add
enable-only-hello-agent-mode
maven profile; ref…
…actor agent and its test code
- Loading branch information
Showing
18 changed files
with
526 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
22 changes: 21 additions & 1 deletion
22
hello-agent/src/main/java/io/foldright/study/hello/agent/HelloAgent.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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("========================================"); | ||
} | ||
} |
23 changes: 23 additions & 0 deletions
23
main-runner/src/main/java/io/foldright/study/main/StudyAgentMode.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
11
main-runner/src/test/java/io/foldright/study/main/MainTest.java
This file was deleted.
Oops, something went wrong.
36 changes: 36 additions & 0 deletions
36
main-runner/src/test/java/io/foldright/study/main/MainTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
} | ||
} | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.