/*
 * 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.
 */

package hudson;

import hudson.util.ProcessTree;
import hudson.util.StreamTaskListener;
import hudson.remoting.Callable;
import org.apache.commons.io.FileUtils;

import java.io.File;

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

        File tmp = File.createTempFile("testRemoteKill", "");
        tmp.delete();
        FilePath f = new FilePath(french, tmp.getPath());
        Launcher l = f.createLauncher(new StreamTaskListener(System.err));
        Proc p = l.launch().cmds("sh", "-c", "echo $$$$ > "+tmp+"; sleep 30").stdout(System.out).stderr(System.err).start();
        while (!tmp.exists())
            Thread.sleep(100);
        long start = System.currentTimeMillis();
        p.kill();
        assertTrue(p.join()!=0);
        long end = System.currentTimeMillis();
        assertTrue("join finished promptly", (end - start < 5000));
        french.call(NOOP); // this only returns after the other side of the channel has finished executing cancellation
        Thread.sleep(2000); // more delay to make sure it's gone
        assertNull("process should be gone",ProcessTree.get().get(Integer.parseInt(FileUtils.readFileToString(tmp).trim())));

        // 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.
    }

    private static final Callable<Object,RuntimeException> NOOP = new Callable() {
        public Object call() throws Exception {
            return null;
        }
    };
}

@@ -1707,24 +1707,43 @@
 p.delete();%0A
+%0A        try %7B%0A    
         File
@@ -1796,16 +1796,20 @@
         
+    
 Launcher
@@ -1863,24 +1863,28 @@
 stem.err));%0A
+    
         Proc
@@ -2004,16 +2004,20 @@
         
+  
+  
 while (!
@@ -2034,32 +2034,36 @@
 ())%0A            
+    
 Thread.sleep(100
@@ -2069,24 +2069,28 @@
 0);%0A        
+   
+ 
 long start =
@@ -2110,32 +2110,36 @@
 ntTimeMillis();%0A
+    
         p.kill()
@@ -2140,32 +2140,36 @@
 kill();%0A        
+    
 assertTrue(p.joi
@@ -2181,24 +2181,28 @@
 0);%0A        
+ 
+   
 long end = S
@@ -2220,32 +2220,36 @@
 ntTimeMillis();%0A
+    
         assertTr
@@ -2308,16 +2308,20 @@
         
+  
+  
 french.c
@@ -2424,32 +2424,36 @@
 llation%0A        
+    
 Thread.sleep(200
@@ -2493,16 +2493,20 @@
 's gone%0A
+    
         
@@ -2632,16 +2632,20 @@
         
+    
 // Manua
@@ -2715,16 +2715,20 @@
 console%0A
+    
         
@@ -2840,32 +2840,36 @@
 .err)).%0A        
+ 
+   
 //   launch().cm
@@ -2947,16 +2947,20 @@
         
+    
 // retur
@@ -3022,24 +3022,28 @@
 fix%0A        
+   
+ 
 // hudson.mo
@@ -3144,32 +3144,36 @@
 .err)).%0A        
+    
 //   launch().cm
@@ -3235,32 +3235,36 @@
 .start().kill()%0A
+    
         // hangs
@@ -3348,16 +3348,72 @@
 eturns.%0A
+        %7D finally %7B%0A            tmp.delete();%0A        %7D%0A
     %7D%0A%0A 

clean up the tmp file after a test