LauncherTest.java 4.6 KB
Newer Older
1
/*
J
jglick 已提交
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
 * The MIT License
 *
 * Copyright 2009 Sun Microsystems, Inc., Kohsuke Kawaguchi, Jesse Glick.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
23 24 25 26 27 28
 */

package hudson;

import hudson.remoting.Channel;
import hudson.util.StreamTaskListener;
29 30
import hudson.util.ProcessTree;

31 32 33 34 35 36
import java.io.File;
import java.io.PipedInputStream;
import java.io.PipedOutputStream;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
37 38
import java.util.concurrent.Callable;

39
import junit.framework.TestCase;
40 41
import org.apache.commons.io.IOUtils;
import org.apache.commons.io.FileUtils;
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74

public class LauncherTest extends TestCase {
    public LauncherTest(String n) {
        super(n);
    }

    //@Bug(4611)
    public void testRemoteKill() throws Exception {
        if (File.pathSeparatorChar != ':') {
            System.err.println("Skipping, currently Unix-specific test");
            return;
        }

        // XXX setup stolen from FilePathTest; should it be refactored into common test utility?
        final ExecutorService executors = Executors.newCachedThreadPool();
        final PipedInputStream p1i = new PipedInputStream();
        final PipedInputStream p2i = new PipedInputStream();
        final PipedOutputStream p1o = new PipedOutputStream(p1i);
        final PipedOutputStream p2o = new PipedOutputStream(p2i);
        Future<Channel> f1 = executors.submit(new Callable<Channel>() {
            public Channel call() throws Exception {
                return new Channel("This side of the channel", executors, p1i, p2o);
            }
        });
        Future<Channel> f2 = executors.submit(new Callable<Channel>() {
            public Channel call() throws Exception {
                return new Channel("The other side of the channel", executors, p2i, p1o);
            }
        });
        Channel french = f1.get();
        Channel british = f2.get();

        File tmp = File.createTempFile("testRemoteKill", "");
75
        tmp.delete();
76 77
        FilePath f = new FilePath(french, tmp.getPath());
        Launcher l = f.createLauncher(new StreamTaskListener(System.err));
78 79 80
        Proc p = l.launch().cmds("sh", "-c", "echo $$$$ > "+tmp+"; sleep 30").stdout(System.out).stderr(System.err).start();
        while (!tmp.exists())
            Thread.sleep(100);
81 82
        long start = System.currentTimeMillis();
        p.kill();
83
        assertTrue(p.join()!=0);
84
        long end = System.currentTimeMillis();
85
        assertTrue("join finished promptly", (end - start < 5000));
86
        french.call(NOOP); // this only returns after the other side of the channel has finished executing cancellation 
87
        assertNull("process should be gone",ProcessTree.get().get(Integer.parseInt(FileUtils.readFileToString(tmp).trim())));
88 89 90 91 92 93 94 95 96 97

        // Manual version of test: set up instance w/ one slave. Now in script console
        // new hudson.FilePath(new java.io.File("/tmp")).createLauncher(new hudson.util.StreamTaskListener(System.err)).
        //   launch().cmds("sleep", "1d").stdout(System.out).stderr(System.err).start().kill()
        // returns immediately and pgrep sleep => nothing. But without fix
        // hudson.model.Hudson.instance.nodes[0].rootPath.createLauncher(new hudson.util.StreamTaskListener(System.err)).
        //   launch().cmds("sleep", "1d").stdout(System.out).stderr(System.err).start().kill()
        // hangs and on slave machine pgrep sleep => one process; after manual kill, script returns.
    }

98 99 100 101 102
    private static final hudson.remoting.Callable<Object,RuntimeException> NOOP = new hudson.remoting.Callable() {
        public Object call() throws Exception {
            return null;
        }
    };
103
}