Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

refactored test

  • Loading branch information...
commit 281e9faa98c62822eb1bd1579f99bf6780377c87 1 parent c501a38
@belaban authored
View
2  src/org/jgroups/util/BlockingInputStream.java
@@ -194,7 +194,7 @@ public int capacity() {
/**
* Closes the stream. Writes to a closed stream will fail, reads will successfully read the bytes that are already
- * in the buffer and then return -1 or throw an EOFException
+ * in the buffer and then return -1 (EOF)
* @throws IOException
*/
public void close() throws IOException {
View
240 tests/junit-functional/org/jgroups/tests/BlockingInputStreamTest.java
@@ -6,6 +6,7 @@
import org.testng.annotations.Test;
import java.io.IOException;
+import java.io.InputStream;
import java.util.concurrent.CountDownLatch;
/**
@@ -26,6 +27,7 @@ public void testCreation() throws IOException {
assert in.available() == 4 && in.capacity() == 2000;
}
+
public void testRead() throws IOException {
final BlockingInputStream in=new BlockingInputStream(100);
byte[] input=new byte[]{'B', 'e', 'l', 'a'};
@@ -33,71 +35,85 @@ public void testRead() throws IOException {
in.close();
assert in.available() == 4;
- int b;
for(int i=0; i < input.length; i++) {
- b=in.read();
+ int b=in.read();
assert b == input[i];
}
- b=in.read();
+ int b=in.read();
assert b == -1;
}
+
public void testBlockingReadAndClose() throws IOException {
final BlockingInputStream in=new BlockingInputStream(100);
final CountDownLatch latch=new CountDownLatch(1);
-
- new Thread() {
- public void run() {
- try {
- latch.await();
- Util.sleep(1000);
- in.close();
- }
- catch(Exception e) {
- e.printStackTrace();
- }
- }
- }.start();
-
byte[] buf=new byte[100];
+
+ new Closer(latch, in, 1000L).start(); // closes input stream after 1 sec
latch.countDown();
int num=in.read(buf, 0, buf.length);
assert num == -1 : " expected -1 (EOF) but got " + num;
}
+
+ public void testBlockingWriteAndClose() throws IOException {
+ final BlockingInputStream in=new BlockingInputStream(3);
+ final CountDownLatch latch=new CountDownLatch(1);
+ byte[] buf=new byte[]{'B', 'e', 'l', 'a'};
+
+ new Closer(latch, in, 1000L).start(); // closes input stream after 1 sec
+ latch.countDown();
+ in.write(buf, 0, buf.length);
+ }
+
public void testReadOnClosedInputStream() throws IOException {
final BlockingInputStream in=new BlockingInputStream(100);
in.close();
byte[] buf=new byte[100];
int num=in.read(buf, 0, buf.length);
- System.out.println("num = " + num);
assert num == -1 : " expected -1 (EOF) but got " + num;
}
- public void testSimpleTransfer() throws IOException {
+
+ public void testWriteCloseRead() throws IOException {
final BlockingInputStream in=new BlockingInputStream(100);
- new Thread() {
- public void run() {
- byte[] buffer=new byte[500];
- for(int i=0; i < buffer.length; i++) {
- if(i % 2 == 0)
- buffer[i]=0;
- else
- buffer[i]=1;
- }
- try {
- in.write(buffer, 0, buffer.length);
- }
- catch(IOException e) {
- e.printStackTrace();
- }
- finally {
- Util.close(in);
- }
- }
- }.start();
+ for(int i=1; i <= 5; i++) {
+ byte[] buf=("Hello world " + i).getBytes();
+ in.write(buf);
+ }
+ in.close();
+
+ int size=in.available();
+ byte[] buf=new byte[100];
+ int num=in.read(buf);
+ assert num == size;
+ }
+
+
+ public void testWriteCloseRead2() throws IOException {
+ final BlockingInputStream in=new BlockingInputStream(100);
+ StringBuilder sb=new StringBuilder();
+ for(int i=1; i <=10; i++)
+ sb.append("Hello world " + i);
+ byte[] buffer=sb.toString().getBytes();
+ new Writer(in, buffer).start();
+ Util.sleep(500);
+ int size=in.available();
+ byte[] buf=new byte[200];
+ int num=in.read(buf);
+ assert num == size;
+ }
+
+
+ public void testSimpleTransfer() throws IOException {
+ final BlockingInputStream in=new BlockingInputStream(100);
+ byte[] buffer=new byte[500];
+ for(int i=0; i < buffer.length; i++)
+ buffer[i]=(byte)(i % 2 == 0? 0 : 1);
+ new Writer(in, buffer).start();
+
byte[] tmp=new byte[500];
int offset=0;
while(true) {
@@ -120,21 +136,7 @@ public void run() {
public void testLargeTransfer() throws IOException {
final BlockingInputStream in=new BlockingInputStream(2048);
final byte[] buffer=generateBuffer(100000);
-
- new Thread() {
- public void run() {
- try {
- in.write(buffer, 0, buffer.length);
- }
- catch(IOException e) {
- e.printStackTrace();
- }
- finally {
- Util.close(in);
- }
- }
- }.start();
-
+ new Writer(in, buffer).start();
byte[] tmp=new byte[buffer.length];
int offset=0;
while(true) {
@@ -152,81 +154,6 @@ public void run() {
}
- protected byte[] generateBuffer(int size) {
- byte[] buf=new byte[size];
- for(int i=0; i < buf.length; i++)
- buf[i]=(byte)(Util.random(size) % Byte.MAX_VALUE);
- return buf;
- }
-
- public void testBlockingWriteAndClose() throws IOException {
- final BlockingInputStream in=new BlockingInputStream(3);
- final CountDownLatch latch=new CountDownLatch(1);
-
- new Thread() {
- public void run() {
- try {
- latch.await();
- Util.sleep(1000);
- in.close();
- }
- catch(Exception e) {
- e.printStackTrace();
- }
- }
- }.start();
-
- byte[] buf=new byte[]{'B', 'e', 'l', 'a'};
- latch.countDown();
- in.write(buf, 0, buf.length);
- }
-
-
- public void testWriteCloseRead() throws IOException {
- final BlockingInputStream in=new BlockingInputStream(100);
- for(int i=1; i <= 5; i++) {
- byte[] buf=("Hello world " + i).getBytes();
- in.write(buf);
- }
- in.close();
-
- int size=in.available();
- byte[] buf=new byte[100];
- int num=in.read(buf);
- assert num == size;
- }
-
- public void testWriteCloseRead2() throws IOException {
- final BlockingInputStream in=new BlockingInputStream(100);
-
- new Thread() {
- public void run() {
- try {
- for(int i=1; i <= 10; i++) {
- byte[] buf=("Hello world " + i).getBytes();
- try {
- in.write(buf);
- }
- catch(IOException e) {
- e.printStackTrace();
- }
- }
- }
- finally {
- Util.close(in);
- }
- }
- }.start();
-
- Util.sleep(500);
-
- int size=in.available();
- byte[] buf=new byte[200];
- int num=in.read(buf);
- assert num == size;
- }
-
-
public void testWriteExceedingCapacity() throws IOException {
final BlockingInputStream in=new BlockingInputStream(10);
new Thread() {
@@ -256,4 +183,59 @@ public void run() {
Util.close(in);
}
}
+
+
+ protected byte[] generateBuffer(int size) {
+ byte[] buf=new byte[size];
+ for(int i=0; i < buf.length; i++)
+ buf[i]=(byte)(Util.random(size) % Byte.MAX_VALUE);
+ return buf;
+ }
+
+
+ protected static final class Closer extends Thread {
+ protected final CountDownLatch latch;
+ protected final InputStream in;
+ protected final long timeout;
+
+ public Closer(CountDownLatch latch, InputStream in, long timeout) {
+ this.latch=latch;
+ this.in=in;
+ this.timeout=timeout;
+ }
+
+ public void run() {
+ try {
+ latch.await();
+ Util.sleep(timeout);
+ in.close();
+ }
+ catch(Exception e) {
+ e.printStackTrace();
+ }
+ }
+ }
+
+ protected static final class Writer extends Thread {
+ protected final BlockingInputStream in;
+ protected final byte[] buffer;
+
+ public Writer(BlockingInputStream in, byte[] buffer) {
+ this.in=in;
+ this.buffer=buffer;
+ }
+
+ public void run() {
+ try {
+ in.write(buffer);
+ }
+ catch(IOException e) {
+ }
+ finally {
+ Util.close(in);
+ }
+ }
+ }
+
+
}
Please sign in to comment.
Something went wrong with that request. Please try again.