Skip to content

Commit

Permalink
wire up test for machine source detection and flesh out agent runner
Browse files Browse the repository at this point in the history
  • Loading branch information
RichardWarburton committed Jan 6, 2015
1 parent 51baad4 commit 44bb407
Show file tree
Hide file tree
Showing 5 changed files with 112 additions and 16 deletions.
9 changes: 9 additions & 0 deletions src/main/java/InfiniteExample.java
@@ -1,3 +1,7 @@
import java.lang.management.ManagementFactory;

import static java.lang.Long.parseLong;

/**
* Copyright (c) 2014 Richard Warburton (richard.warburton@gmail.com)
*
Expand All @@ -22,6 +26,11 @@
public class InfiniteExample {

public static void main(String[] args) throws Exception {
final String jvmName = ManagementFactory.getRuntimeMXBean().getName();
final int index = jvmName.indexOf('@');

System.out.println(parseLong(jvmName.substring(0, index)));

while (true) {
Thread.sleep(1);
subMethod();
Expand Down
Expand Up @@ -21,7 +21,6 @@
**/
package com.insightfullogic.honest_profiler.ports.sources;

import com.insightfullogic.honest_profiler.core.Monitor;
import com.insightfullogic.honest_profiler.core.MachineListener;
import com.insightfullogic.honest_profiler.core.ThreadedAgent;
import com.insightfullogic.honest_profiler.core.sources.VirtualMachine;
Expand All @@ -43,18 +42,23 @@ public class LocalMachineSource {
private static final String VM_ARGS = "sun.jvm.args";
private static final String AGENT_NAME = "liblagent.so";
private static final String USER_DIR = "user.dir";
private static final long DEFAULT_SLEEP_PERIOD = 500;

private final Logger logger;
private final MachineListener listener;
private final Monitor monitor;
private final long sleepPeriod;
private final ThreadedAgent threadedAgent;

private Set<VirtualMachineDescriptor> previous;

public LocalMachineSource(final Logger logger, final MachineListener listener, final Monitor monitor) {
public LocalMachineSource(final Logger logger, final MachineListener listener) {
this(logger, listener, DEFAULT_SLEEP_PERIOD);
}

public LocalMachineSource(final Logger logger, final MachineListener listener, final long sleepPeriod) {
this.logger = logger;
this.listener = listener;
this.monitor = monitor;
this.sleepPeriod = sleepPeriod;
previous = new HashSet<>();
threadedAgent = new ThreadedAgent(LoggerFactory.getLogger(ThreadedAgent.class), this::discoverVirtualMachines);
}
Expand All @@ -64,7 +68,7 @@ public void start() {
threadedAgent.start();
}

private boolean discoverVirtualMachines() {
public boolean discoverVirtualMachines() {
poll();

sleep();
Expand All @@ -74,7 +78,7 @@ private boolean discoverVirtualMachines() {

private void sleep() {
try {
Thread.sleep(500);
Thread.sleep(sleepPeriod);
} catch (InterruptedException e) {
// Ignore
}
Expand Down
@@ -1,17 +1,53 @@
package com.insightfullogic.honest_profiler.ports.sources;

import com.insightfullogic.honest_profiler.core.MachineListener;
import com.insightfullogic.honest_profiler.core.sources.VirtualMachine;
import com.insightfullogic.honest_profiler.testing_utilities.AgentRunner;
import com.insightfullogic.lambdabehave.JunitSuiteRunner;
import org.junit.runner.RunWith;
import org.slf4j.Logger;

import static com.insightfullogic.lambdabehave.Suite.describe;
import static org.mockito.Mockito.mock;

@RunWith(JunitSuiteRunner.class)
public class LocalMachineSourceTest {{

describe("Local Machine Sources", it -> {

Logger logger = mock(Logger.class);


it.should("detect local machines", expect -> {
AgentRunner.run("InfiniteExample", runner -> {
final int expectedProcessId = runner.getProcessId();
new LocalMachineSource(logger, new MachineListener() {
@Override
public void onNewMachine(final VirtualMachine machine) {
int machineProcessId = Integer.parseInt(machine.getId());
expect.that(machine.isAgentLoaded()).is(machineProcessId == expectedProcessId);
}

@Override
public void onClosedMachine(final VirtualMachine machine) {
expect.failure("Should never close VM " + machine);
}
}).discoverVirtualMachines();
});
});

it.should("detect no local machines if none are running", expect -> {
new LocalMachineSource(logger, new MachineListener() {
@Override
public void onNewMachine(final VirtualMachine machine) {
expect.that(machine.isAgentLoaded()).is(false);
}

@Override
public void onClosedMachine(final VirtualMachine machine) {
expect.failure("Should never close VM " + machine);
}
}).discoverVirtualMachines();
});

});
Expand Down
Expand Up @@ -33,13 +33,9 @@ public class AgentIntegrationTest {{
describe("Agent Integration", it -> {

it.should("should result in a detectable JVM", expect -> {
AgentRunner runner = new AgentRunner("InfiniteExample");
runner.start();
try {
AgentRunner.run("InfiniteExample", runner -> {

} finally {
runner.stop();
}
});
});

});
Expand Down
Expand Up @@ -21,20 +21,71 @@
**/
package com.insightfullogic.honest_profiler.testing_utilities;

// TODO: implement
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.function.Consumer;

import static java.lang.Integer.parseInt;

public class AgentRunner {

private static final Logger logger = LoggerFactory.getLogger(AgentRunner.class);
public static final int UNKNOWN_PROCESS_ID = -1;

public static void run(final String className, final Consumer<AgentRunner> handler) throws IOException {
AgentRunner runner = new AgentRunner(className);
runner.start();
try {
handler.accept(runner);
} finally {
runner.stop();
}
}

private final String className;

public AgentRunner(final String className) {
private Process process;
private int processId;

private AgentRunner(final String className) {
this.className = className;
}

public void start() {
private void start() throws IOException {
startProcess();
readProcessId();
}

private void startProcess() throws IOException {
// Eg: java -agentpath:build/liblagent.so -cp target/classes/ InfiniteExample
process = new ProcessBuilder()
.command("/usr/bin/java", "-agentpath:build/liblagent.so", "-cp", "target/classes/", className)
.redirectError(new File("/tmp/error.log"))
.start();
}

private void readProcessId() throws IOException {
try (BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()))) {
processId = parseInt(reader.readLine());
}
}

public void stop() {
private void stop() {
process.destroy();
try {
process.waitFor();
} catch (InterruptedException e) {
logger.info(e.getMessage(), e);
}
}

public int getProcessId() {
return processId;
}

}

0 comments on commit 44bb407

Please sign in to comment.