1 36 package org.columba.ristretto.message.io; 37 38 import java.io.ByteArrayInputStream ; 39 import java.io.IOException ; 40 import java.util.Random ; 41 42 import org.columba.ristretto.io.AsyncInputStream; 43 44 import junit.framework.TestCase; 45 46 public class AsyncSourceInputStreamTest extends TestCase { 47 48 byte[] test = new byte[1000]; 49 50 51 public void test1() throws IOException { 52 AsyncInputStream in = new AsyncInputStream(new ByteArrayInputStream (test), test.length); 53 54 long time = System.currentTimeMillis(); 55 56 new Thread (new TestWriter(in, 1000)).start(); 58 59 for( int i=0; i<1000; i++) { 60 assertEquals(test[i], (byte) in.read()); 62 } 63 64 assertTrue( System.currentTimeMillis() - time > 1000); 66 } 67 70 protected void setUp() throws Exception { 71 Random r = new Random (); 72 r.nextBytes(test); 73 } 74 } 75 76 77 class TestWriter implements Runnable { 78 79 private AsyncInputStream in; 80 private int size; 81 private int pos; 82 83 86 public void run() { 87 try { 88 while( pos < size ) { 89 Thread.sleep(100); 90 in.grow(100); 91 pos += 10; 92 } 93 } catch (InterruptedException e) { 94 e.printStackTrace(); 95 } 96 } 97 102 public TestWriter(AsyncInputStream in, int size) { 103 super(); 104 this.in = in; 105 this.size = size; 106 } 107 } | Popular Tags |