Skip to content

Commit

Permalink
add test
Browse files Browse the repository at this point in the history
  • Loading branch information
korniltsev committed Apr 23, 2024
1 parent 25f85f3 commit c61bc7e
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
public class PyroscopeAgent {
private static final Object sLock = new Object();
private static Options sOptions = null;
private static boolean sFailed = false;

public static void premain(final String agentArgs,
final Instrumentation inst) {
Expand Down Expand Up @@ -47,17 +46,12 @@ public static void start(Options options) {
logger.log(Logger.Level.ERROR, "Failed to start profiling - already started");
return;
}
if (sFailed) {
logger.log(Logger.Level.ERROR, "Not start profiling due to recent failure");
return;
}
sOptions = options;
logger.log(Logger.Level.DEBUG, "Config: %s", options.config);
try {
options.scheduler.start(options.profiler);
logger.log(Logger.Level.INFO, "Profiling started");
} catch (final Throwable e) {
sFailed = true;
logger.log(Logger.Level.ERROR, "Error starting profiler %s", e);
sOptions = null;
}
Expand All @@ -75,7 +69,6 @@ public static void stop() {
sOptions.logger.log(Logger.Level.INFO, "Profiling stopped");
} catch (Throwable e) {
sOptions.logger.log(Logger.Level.ERROR, "Error stopping profiler %s", e);
sFailed = true;
}

sOptions = null;
Expand Down
59 changes: 59 additions & 0 deletions agent/src/test/java/io/pyroscope/javaagent/StartStopTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package io.pyroscope.javaagent;

import io.pyroscope.http.Format;
import io.pyroscope.javaagent.api.Logger;
import io.pyroscope.javaagent.config.Config;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;

public class StartStopTest {

public static final Config INVALID = new Config.Builder()
.setApplicationName("demo.app{qweqwe=asdasd}")
.setFormat(Format.JFR)
.setProfilingAlloc("512k")
.setAPExtraArguments("event=qwe") // java.lang.IllegalArgumentException: Duplicate event argument
.setProfilingEvent(EventType.ITIMER)
.setLogLevel(Logger.Level.DEBUG)
.build();

public static final Config VALID = new Config.Builder()
.setApplicationName("demo.app{qweqwe=asdasd}")
.setFormat(Format.JFR)
.setProfilingEvent(EventType.ITIMER)
.setLogLevel(Logger.Level.DEBUG)
.build();


@Test
void testStartFail() {
notStarted();

PyroscopeAgent.start(INVALID);
notStarted();

PyroscopeAgent.start(INVALID);
notStarted();

PyroscopeAgent.stop();
notStarted();
PyroscopeAgent.stop();
notStarted();

PyroscopeAgent.start(VALID);
started();

PyroscopeAgent.stop();
notStarted();
}

private static void started() {
assertTrue(PyroscopeAgent.isStarted());
}

private static void notStarted() {
assertFalse(PyroscopeAgent.isStarted());
}
}

0 comments on commit c61bc7e

Please sign in to comment.