1 package hudson.remoting; 2 3 import junit.framework.Test; 4 5 import java.io.OutputStream ; 6 import java.io.IOException ; 7 import java.io.InputStream ; 8 import java.util.Arrays ; 9 10 15 public class PipeTest extends RmiTestBase { 16 19 public void testRemoteWrite() throws Exception { 20 Pipe p = Pipe.createRemoteToLocal(); 21 Future<Integer > 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 , IOException > { 31 private final Pipe pipe; 32 33 public WritingCallable(Pipe pipe) { 34 this.pipe = pipe; 35 } 36 37 public Integer call() throws IOException { 38 write(pipe); 39 return 5; 40 } 41 } 42 43 46 public void testLocalWrite() throws Exception { 47 Pipe p = Pipe.createLocalToRemote(); 48 Future<Integer > 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 { 58 Pipe p = Pipe.createLocalToRemote(); 59 Future<Integer > f = channel.callAsync(new ReadingCallable(p)); 60 61 Thread.sleep(2000); 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 , IOException > { 70 private final Pipe pipe; 71 72 public ReadingCallable(Pipe pipe) { 73 this.pipe = pipe; 74 } 75 76 public Integer call() throws IOException { 77 read(pipe); 78 return 5; 79 } 80 81 } 82 83 private static void write(Pipe pipe) throws IOException { 84 OutputStream 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 { 94 InputStream 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 { 102 return buildSuite(PipeTest.class); 103 } 104 } 105 | Popular Tags |