use of com.thoughtworks.cruise.agent.common.launcher.AgentLaunchDescriptor 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.AgentLaunchDescriptor 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.AgentLaunchDescriptor 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.AgentLaunchDescriptor in project gocd by gocd.
the class AgentLauncherImplTest method launchDescriptor.
private AgentLaunchDescriptor launchDescriptor() {
AgentLaunchDescriptor launchDescriptor = mock(AgentLaunchDescriptor.class);
Map contextMap = new ConcurrentHashMap();
contextMap.put(AgentBootstrapperArgs.SERVER_URL, "http://localhost:9090/go");
contextMap.put(AgentBootstrapperArgs.SSL_VERIFICATION_MODE, "NONE");
when(launchDescriptor.context()).thenReturn(contextMap);
return launchDescriptor;
}
use of com.thoughtworks.cruise.agent.common.launcher.AgentLaunchDescriptor in project gocd by gocd.
the class AgentLauncherImplTest method shouldNotThrowException_instedReturnAppropriateErrorCode_whenSomethingGoesWrongInLaunch.
@Test
public void shouldNotThrowException_instedReturnAppropriateErrorCode_whenSomethingGoesWrongInLaunch() {
AgentLaunchDescriptor launchDesc = mock(AgentLaunchDescriptor.class);
when((String) launchDesc.context().get(AgentBootstrapperArgs.SERVER_URL)).thenThrow(new RuntimeException("Ouch!"));
try {
assertThat(new AgentLauncherImpl().launch(launchDesc), is(-273));
} catch (Exception e) {
fail("should not have blown up, because it directly interfaces with bootstrapper");
}
}
Aggregations