[Libreoffice-commits] .: Branch 'libreoffice-3-6' - jurt/test

Libreoffice Gerrit user logerrit at kemper.freedesktop.org
Fri Sep 14 10:14:39 PDT 2012


 jurt/test/com/sun/star/comp/connections/PipedConnection_Test.java |   91 +++-------
 1 file changed, 29 insertions(+), 62 deletions(-)

New commits:
commit 3e63d52210a742f6aebf92f46721191dd6eed0a7
Author: Stephan Bergmann <sbergman at redhat.com>
Date:   Wed Sep 12 14:36:18 2012 +0200

    Remove race conditions from test
    
    Change-Id: I97e10fa3b9058211313a7faa5269d71e233f8bf8
    (cherry picked from commit c414499bbd456389ac6cacf677327bff9e6b43f9)
    
    Signed-off-by: Michael Stahl <mstahl at redhat.com>

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 affbb8b..d0a4444 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;
         }
     }
 }


More information about the Libreoffice-commits mailing list