Search in sources :

Example 1 with AgentLaunchDescriptor

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");
    }
}
Also used : AgentLauncher(com.thoughtworks.cruise.agent.common.launcher.AgentLauncher) AgentLaunchDescriptor(com.thoughtworks.cruise.agent.common.launcher.AgentLaunchDescriptor) AgentBootstrapperArgs(com.thoughtworks.go.agent.common.AgentBootstrapperArgs) Semaphore(java.util.concurrent.Semaphore) URL(java.net.URL) Test(org.junit.Test)

Example 2 with AgentLaunchDescriptor

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));
}
Also used : AgentLauncher(com.thoughtworks.cruise.agent.common.launcher.AgentLauncher) AgentLaunchDescriptor(com.thoughtworks.cruise.agent.common.launcher.AgentLaunchDescriptor) AgentBootstrapperArgs(com.thoughtworks.go.agent.common.AgentBootstrapperArgs) URL(java.net.URL) Test(org.junit.Test)

Example 3 with AgentLaunchDescriptor

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));
}
Also used : HashMap(java.util.HashMap) AgentLauncher(com.thoughtworks.cruise.agent.common.launcher.AgentLauncher) AgentLaunchDescriptor(com.thoughtworks.cruise.agent.common.launcher.AgentLaunchDescriptor) AgentBootstrapperArgs(com.thoughtworks.go.agent.common.AgentBootstrapperArgs) HashMap(java.util.HashMap) Map(java.util.Map) URL(java.net.URL) Test(org.junit.Test)

Example 4 with AgentLaunchDescriptor

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;
}
Also used : AgentLaunchDescriptor(com.thoughtworks.cruise.agent.common.launcher.AgentLaunchDescriptor) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap)

Example 5 with AgentLaunchDescriptor

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");
    }
}
Also used : AgentLaunchDescriptor(com.thoughtworks.cruise.agent.common.launcher.AgentLaunchDescriptor) IOException(java.io.IOException) Test(org.junit.Test)

Aggregations

AgentLaunchDescriptor (com.thoughtworks.cruise.agent.common.launcher.AgentLaunchDescriptor)5 Test (org.junit.Test)4 AgentLauncher (com.thoughtworks.cruise.agent.common.launcher.AgentLauncher)3 AgentBootstrapperArgs (com.thoughtworks.go.agent.common.AgentBootstrapperArgs)3 URL (java.net.URL)3 Map (java.util.Map)2 IOException (java.io.IOException)1 HashMap (java.util.HashMap)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 Semaphore (java.util.concurrent.Semaphore)1