use of com.thoughtworks.cruise.agent.common.launcher.AgentLauncher in project gocd by gocd.
the class AgentBootstrapperTest method shouldNotDieWhenInvocationOfLauncherRaisesException_butCreationOfLauncherWentThrough.
@Test
public void shouldNotDieWhenInvocationOfLauncherRaisesException_butCreationOfLauncherWentThrough() throws InterruptedException {
final Semaphore waitForLauncherInvocation = new Semaphore(1);
waitForLauncherInvocation.acquire();
final AgentBootstrapper bootstrapper = new AgentBootstrapper() {
@Override
AgentLauncherCreator getLauncherCreator() {
return new AgentLauncherCreator() {
public AgentLauncher createLauncher() {
return new AgentLauncher() {
public int launch(AgentLaunchDescriptor descriptor) {
try {
throw new RuntimeException("fail!!! i say.");
} finally {
if (waitForLauncherInvocation.availablePermits() == 0) {
waitForLauncherInvocation.release();
}
}
}
};
}
@Override
public void destroy() {
}
};
}
};
final AgentBootstrapper spyBootstrapper = stubJVMExit(bootstrapper);
Thread stopLoopThd = new Thread(new Runnable() {
public void run() {
try {
waitForLauncherInvocation.acquire();
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
ReflectionUtil.setField(spyBootstrapper, "loop", false);
}
});
stopLoopThd.start();
try {
spyBootstrapper.go(true, new AgentBootstrapperArgs(new URL("http://" + "ghost-name" + ":" + 3518 + "/go"), null, AgentBootstrapperArgs.SslMode.NONE));
stopLoopThd.join();
} catch (Exception e) {
fail("should not have propagated exception thrown while invoking the launcher");
}
}
use of com.thoughtworks.cruise.agent.common.launcher.AgentLauncher in project gocd by gocd.
the class AgentBootstrapperTest method shouldNotRelaunchAgentLauncherWhenItReturnsAnIrrecoverableCode.
@Test(timeout = 10 * 1000)
public void shouldNotRelaunchAgentLauncherWhenItReturnsAnIrrecoverableCode() throws InterruptedException {
final boolean[] destroyCalled = new boolean[1];
final AgentBootstrapper bootstrapper = new AgentBootstrapper(new AgentLauncherCreator() {
public AgentLauncher createLauncher() {
return new AgentLauncher() {
public int launch(AgentLaunchDescriptor descriptor) {
return AgentLauncher.IRRECOVERABLE_ERROR;
}
};
}
@Override
public void destroy() {
destroyCalled[0] = true;
}
});
final AgentBootstrapper spyBootstrapper = stubJVMExit(bootstrapper);
try {
spyBootstrapper.go(true, new AgentBootstrapperArgs(new URL("http://" + "ghost-name" + ":" + 3518 + "/go"), null, AgentBootstrapperArgs.SslMode.NONE));
} catch (Exception e) {
fail("should not have propagated exception thrown while invoking the launcher");
}
assertThat(destroyCalled[0], is(true));
}
use of com.thoughtworks.cruise.agent.common.launcher.AgentLauncher in project gocd by gocd.
the class AgentBootstrapperTest method shouldRetainStateAcrossLauncherInvocations.
@Test
public void shouldRetainStateAcrossLauncherInvocations() throws Exception {
final Map expectedContext = new HashMap();
AgentBootstrapper agentBootstrapper = new AgentBootstrapper() {
@Override
AgentLauncherCreator getLauncherCreator() {
return new AgentLauncherCreator() {
public AgentLauncher createLauncher() {
return new AgentLauncher() {
public static final String COUNT = "count";
public int launch(AgentLaunchDescriptor descriptor) {
Map descriptorContext = descriptor.context();
incrementCount(descriptorContext);
incrementCount(expectedContext);
Integer expectedCount = (Integer) expectedContext.get(COUNT);
assertThat(descriptorContext.get(COUNT), is(expectedCount));
if (expectedCount > 3) {
((AgentBootstrapper) descriptor.getBootstrapper()).stopLooping();
}
return 0;
}
private void incrementCount(Map map) {
Integer currentInvocationCount = map.containsKey(COUNT) ? (Integer) map.get(COUNT) : 0;
map.put(COUNT, currentInvocationCount + 1);
}
};
}
@Override
public void destroy() {
}
};
}
};
AgentBootstrapper spy = stubJVMExit(agentBootstrapper);
spy.go(true, new AgentBootstrapperArgs(new URL("http://" + "localhost" + ":" + 80 + "/go"), null, AgentBootstrapperArgs.SslMode.NONE));
}
use of com.thoughtworks.cruise.agent.common.launcher.AgentLauncher in project gocd by gocd.
the class AgentLauncherImplTest method shouldPassLauncherVersionToAgent.
@Test
public void shouldPassLauncherVersionToAgent() throws InterruptedException, IOException {
final List<String> actualVersion = new ArrayList<>();
final AgentLauncher launcher = new AgentLauncherImpl(new AgentLauncherImpl.AgentProcessParentRunner() {
public int run(String launcherVersion, String launcherMd5, ServerUrlGenerator urlConstructor, Map<String, String> environmentVariables, Map context) {
actualVersion.add(launcherVersion);
return 0;
}
});
TEST_AGENT_LAUNCHER.copyTo(AGENT_LAUNCHER_JAR);
launcher.launch(launchDescriptor());
assertThat(actualVersion.size(), is(1));
assertThat(actualVersion.get(0), is(CurrentGoCDVersion.getInstance().fullVersion()));
}
use of com.thoughtworks.cruise.agent.common.launcher.AgentLauncher in project gocd by gocd.
the class AgentBootstrapper method go.
public void go(boolean shouldLoop, AgentBootstrapperArgs bootstrapperArgs) {
loop = shouldLoop;
launcherThread = Thread.currentThread();
validate();
cleanupTempFiles();
int returnValue = 0;
DefaultAgentLaunchDescriptorImpl descriptor = new DefaultAgentLaunchDescriptorImpl(bootstrapperArgs, this);
do {
ClassLoader tccl = launcherThread.getContextClassLoader();
try {
AgentLauncher launcher = getLauncher();
LOG.info("Attempting create and start launcher...");
setContextClassLoader(launcher.getClass().getClassLoader());
returnValue = launcher.launch(descriptor);
resetContextClassLoader(tccl);
LOG.info("Launcher returned with code " + returnValue + "(0x" + Integer.toHexString(returnValue).toUpperCase() + ")");
if (returnValue == AgentLauncher.IRRECOVERABLE_ERROR) {
loop = false;
}
} catch (Exception e) {
LOG.error("Error starting launcher", e);
} finally {
resetContextClassLoader(tccl);
forceGCToPreventOOM();
destoryLauncherCreator();
}
waitForRelaunchTime();
} while (loop);
destoryLauncherCreator();
LOG.info("Agent Bootstrapper stopped");
jvmExit(returnValue);
}
Aggregations