File D:\maven-qcr-plugin-testsunitaires\qcr4java\jenkins-master\target\site\qc\test\src\test\java\hudson\ProcTest.java.html

1 package hudson;
2
3 import hudson.Launcher.LocalLauncher;
4 import hudson.Launcher.RemoteLauncher;
5 import hudson.Proc.RemoteProc;
6 import hudson.remoting.Callable;
7 import hudson.remoting.Future;
8 import hudson.remoting.Pipe;
9 import hudson.remoting.VirtualChannel;
10 import hudson.slaves.DumbSlave;
11 import hudson.util.IOUtils;
12 import hudson.util.StreamTaskListener;
13 import org.jvnet.hudson.test.Bug;
14 import org.jvnet.hudson.test.HudsonTestCase;
15
16 import java.io.ByteArrayInputStream;
17 import java.io.ByteArrayOutputStream;
18 import java.io.IOException;
19 import java.io.OutputStream;
20 import java.nio.charset.Charset;
21
22 /**
23 * @author Kohsuke Kawaguchi
24 */
25 public class ProcTest extends HudsonTestCase {
26 /**
27 * Makes sure that the output flushing and {@link RemoteProc#join()} is synced.
28 */
29 @Bug(7809)
30 public void testRemoteProcOutputSync() throws Exception {
31 VirtualChannel ch = createSlaveChannel();
32
33 // keep the pipe fairly busy
34 final Pipe p = Pipe.createRemoteToLocal();
35 for (int i=0; i<10; i++)
36 ch.callAsync(new ChannelFiller(p.getOut()));
37 new Thread() {
38 @Override
39 public void run() {
40 try {
41 IOUtils.drain(p.getIn());
42 } catch (IOException e) {
43 }
44 }
45 }.start();
46
47 RemoteLauncher launcher = new RemoteLauncher(StreamTaskListener.NULL, ch, true);
48
49 String str="";
50 for (int i=0; i<256; i++)
51 str += "oxox";
52
53 for (int i=0; i<1000; i++) {
54 ByteArrayOutputStream baos = new ByteArrayOutputStream();
55 launcher.launch().cmds("echo",str).stdout(baos).join();
56 assertEquals(str, baos.toString().trim());
57 }
58
59 ch.close();
60 }
61
62 private VirtualChannel createSlaveChannel() throws Exception {
63 DumbSlave s = createSlave();
64 s.toComputer().connect(false).get();
65 VirtualChannel ch=null;
66 while (ch==null) {
67 ch = s.toComputer().getChannel();
68 Thread.sleep(100);
69 }
70 return ch;
71 }
72
73 private static class ChannelFiller implements Callable<Void,IOException> {
74 private final OutputStream o;
75
76 private ChannelFiller(OutputStream o) {
77 this.o = o;
78 }
79
80 public Void call() throws IOException {
81 while (!Thread.interrupted()) {
82 o.write(new byte[256]);
83 }
84 return null;
85 }
86 }
87
88 @Bug(7809)
89 public void testIoPumpingWithLocalLaunch() throws Exception {
90 doIoPumpingTest(new LocalLauncher(new StreamTaskListener(System.out, Charset.defaultCharset())));
91 }
92
93 @Bug(7809)
94 public void testIoPumpingWithRemoteLaunch() throws Exception {
95 doIoPumpingTest(new RemoteLauncher(
96 new StreamTaskListener(System.out, Charset.defaultCharset()),
97 createSlaveChannel(), true));
98 }
99
100 private void doIoPumpingTest(Launcher l) throws IOException, InterruptedException {
101 String[] ECHO_BACK_CMD = {"cat"}; // TODO: what is the echo back command for Windows? "cmd /C copy CON CON"?
102
103 ByteArrayOutputStream out = new ByteArrayOutputStream();
104 l.launch().cmds(ECHO_BACK_CMD).stdin(new ByteArrayInputStream("Hello".getBytes())).stdout(out).join();
105 assertEquals("Hello",out.toString());
106
107 Proc p = l.launch().cmds(ECHO_BACK_CMD).stdin(new ByteArrayInputStream("Hello".getBytes())).readStdout().start();
108 p.join();
109 assertEquals("Hello", org.apache.commons.io.IOUtils.toString(p.getStdout()));
110 assertNull(p.getStderr());
111 assertNull(p.getStdin());
112
113
114 p = l.launch().cmds(ECHO_BACK_CMD).writeStdin().readStdout().start();
115 p.getStdin().write("Hello".getBytes());
116 p.getStdin().close();
117 p.join();
118 assertEquals("Hello", org.apache.commons.io.IOUtils.toString(p.getStdout()));
119 assertNull(p.getStderr());
120 }
121 }
122