Search in sources :

Example 1 with AgentLauncher

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");
    }
}
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 AgentLauncher

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));
}
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 AgentLauncher

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));
}
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 AgentLauncher

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()));
}
Also used : AgentLauncher(com.thoughtworks.cruise.agent.common.launcher.AgentLauncher) ArrayList(java.util.ArrayList) ServerUrlGenerator(com.thoughtworks.go.agent.ServerUrlGenerator) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Test(org.junit.Test)

Example 5 with AgentLauncher

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);
}
Also used : AgentLauncher(com.thoughtworks.cruise.agent.common.launcher.AgentLauncher)

Aggregations

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