KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > hudson > remoting > PipeTest


1 package hudson.remoting;
2
3 import junit.framework.Test;
4
5 import java.io.OutputStream JavaDoc;
6 import java.io.IOException JavaDoc;
7 import java.io.InputStream JavaDoc;
8 import java.util.Arrays JavaDoc;
9
10 /**
11  * Test {@link Pipe}.
12  *
13  * @author Kohsuke Kawaguchi
14  */

15 public class PipeTest extends RmiTestBase {
16     /**
17      * Test the "remote-write local-read" pipe.
18      */

19     public void testRemoteWrite() throws Exception JavaDoc {
20         Pipe p = Pipe.createRemoteToLocal();
21         Future<Integer JavaDoc> f = channel.callAsync(new WritingCallable(p));
22
23         read(p);
24
25         int r = f.get();
26         System.out.println("result=" + r);
27         assertEquals(5,r);
28     }
29
30     private static class WritingCallable implements Callable<Integer JavaDoc, IOException JavaDoc> {
31         private final Pipe pipe;
32
33         public WritingCallable(Pipe pipe) {
34             this.pipe = pipe;
35         }
36
37         public Integer JavaDoc call() throws IOException JavaDoc {
38             write(pipe);
39             return 5;
40         }
41     }
42
43     /**
44      * Test the "local-write remote-read" pipe.
45      */

46     public void testLocalWrite() throws Exception JavaDoc {
47         Pipe p = Pipe.createLocalToRemote();
48         Future<Integer JavaDoc> f = channel.callAsync(new ReadingCallable(p));
49
50         write(p);
51
52         int r = f.get();
53         System.out.println("result=" + r);
54         assertEquals(5,r);
55     }
56
57     public void testLocalWrite2() throws Exception JavaDoc {
58         Pipe p = Pipe.createLocalToRemote();
59         Future<Integer JavaDoc> f = channel.callAsync(new ReadingCallable(p));
60
61         Thread.sleep(2000); // wait for remote to connect to local.
62
write(p);
63
64         int r = f.get();
65         System.out.println("result=" + r);
66         assertEquals(5,r);
67     }
68
69     private static class ReadingCallable implements Callable<Integer JavaDoc, IOException JavaDoc> {
70         private final Pipe pipe;
71
72         public ReadingCallable(Pipe pipe) {
73             this.pipe = pipe;
74         }
75
76         public Integer JavaDoc call() throws IOException JavaDoc {
77             read(pipe);
78             return 5;
79         }
80
81     }
82
83     private static void write(Pipe pipe) throws IOException JavaDoc {
84         OutputStream JavaDoc os = pipe.getOut();
85         byte[] buf = new byte[384];
86         for( int i=0; i<256; i++ ) {
87             Arrays.fill(buf,(byte)i);
88             os.write(buf,0,256);
89         }
90         os.close();
91     }
92
93     private static void read(Pipe p) throws IOException JavaDoc {
94         InputStream JavaDoc in = p.getIn();
95         for( int cnt=0; cnt<256*256; cnt++ )
96             assertEquals(cnt/256,in.read());
97         assertEquals(-1,in.read());
98         in.close();
99     }
100
101     public static Test suite() throws Exception JavaDoc {
102         return buildSuite(PipeTest.class);
103     }
104 }
105
Popular Tags