summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorStephan Bergmann <sbergman@redhat.com>2012-09-12 14:36:18 +0200
committerMichael Stahl <mstahl@redhat.com>2012-09-14 19:12:11 +0200
commit3e63d52210a742f6aebf92f46721191dd6eed0a7 (patch)
treea6a75134bf1a0c4610491bffc0e999dcdfe828cf
parent77e0a04561f271d87a14d6a8b6bfe20c93ef7e5d (diff)
Remove race conditions from test
Change-Id: I97e10fa3b9058211313a7faa5269d71e233f8bf8 (cherry picked from commit c414499bbd456389ac6cacf677327bff9e6b43f9) Signed-off-by: Michael Stahl <mstahl@redhat.com>
-rw-r--r--jurt/test/com/sun/star/comp/connections/PipedConnection_Test.java91
1 files changed, 29 insertions, 62 deletions
diff --git a/jurt/test/com/sun/star/comp/connections/PipedConnection_Test.java b/jurt/test/com/sun/star/comp/connections/PipedConnection_Test.java
index affbb8b05dde..d0a444465421 100644
--- a/jurt/test/com/sun/star/comp/connections/PipedConnection_Test.java
+++ b/jurt/test/com/sun/star/comp/connections/PipedConnection_Test.java
@@ -27,115 +27,82 @@
package com.sun.star.comp.connections;
+import com.sun.star.io.IOException;
import org.junit.Test;
import static org.junit.Assert.*;
public final class PipedConnection_Test {
+ private static final int ROUNDS = 2000;
+
@Test public void test() throws Exception {
PipedConnection rightSide = new PipedConnection(new Object[0]);
PipedConnection leftSide = new PipedConnection(new Object[]{rightSide});
- byte theByte[] = new byte[1];
-
- Reader reader = new Reader(rightSide, theByte);
- Writer writer = new Writer(leftSide, theByte, reader);
+ Reader reader = new Reader(rightSide);
+ Writer writer = new Writer(leftSide);
reader.start();
writer.start();
- Thread.sleep(2000);
-
- writer.term();
writer.join();
-
reader.join();
assertTrue(writer._state);
assertTrue(reader._state);
+ assertEquals(ROUNDS, reader._rounds);
}
- static class Reader extends Thread {
+ private static class Reader extends Thread {
PipedConnection _pipedConnection;
- byte _theByte[];
- boolean _quit;
boolean _state = false;
+ int _rounds = 0;
- Reader(PipedConnection pipedConnection, byte theByte[]) {
+ Reader(PipedConnection pipedConnection) {
_pipedConnection = pipedConnection;
- _theByte = theByte;
}
public void run() {
try {
- byte bytes[][] = new byte[1][];
-
- while(!_quit) {
- int read = _pipedConnection.read(bytes, 1);
-
- if(read == 1) {
-// System.err.println("read :" + bytes[0][0]);
-
- if(_theByte[0] != bytes[0][0])
- throw new NullPointerException();
-
- synchronized(this) {
- notifyAll();
- }
+ for (byte v = 0;; v++) {
+ byte[][] b = new byte[1][];
+ int n = _pipedConnection.read(b, 1);
+ if (n == 0) {
+ break;
}
- else
- _quit = true; // EOF
+ assertEquals(1, n);
+ assertEquals(1, b[0].length);
+ assertEquals(v, b[0][0]);
+ ++_rounds;
}
-
_pipedConnection.close();
_state = true;
+ } catch (IOException e) {
+ throw new RuntimeException(e);
}
- catch(com.sun.star.io.IOException ioException) {
- System.err.println("#### Reader - unexpected:" + ioException);
- }
-
}
}
- static class Writer extends Thread {
+ private static class Writer extends Thread {
PipedConnection _pipedConnection;
- byte _theByte[];
- Reader _reader;
- boolean _quit;
boolean _state = false;
- Writer(PipedConnection pipedConnection, byte theByte[], Reader reader) {
+ Writer(PipedConnection pipedConnection) {
_pipedConnection = pipedConnection;
- _reader = reader;
- _theByte = theByte;
}
public void run() {
try {
- while(!_quit) {
- synchronized(_reader) {
- _pipedConnection.write(_theByte);
- _pipedConnection.flush();
-// System.err.println("written :" + _theByte[0]);
-
- _reader.wait();
- }
- ++ _theByte[0];
+ byte v = 0;
+ for (int i = 0; i != ROUNDS; ++i) {
+ byte[] b = new byte[] { v++ };
+ _pipedConnection.write(b);
+ _pipedConnection.flush();
}
-
_pipedConnection.close();
-
_state = true;
+ } catch (IOException e) {
+ throw new RuntimeException(e);
}
- catch(com.sun.star.io.IOException ioException) {
- System.err.println("#### Writer:" + ioException);
- }
- catch(InterruptedException interruptedException) {
- System.err.println("#### Writer:" + interruptedException);
- }
- }
-
- public void term() {
- _quit = true;
}
}
}