]> granicus.if.org Git - icu/commitdiff
ICU-10746 Fixed a collation thread test execution problem. Made testFrozen (was the...
authorYoshito Umaoka <y.umaoka@gmail.com>
Tue, 4 Mar 2014 04:39:04 +0000 (04:39 +0000)
committerYoshito Umaoka <y.umaoka@gmail.com>
Tue, 4 Mar 2014 04:39:04 +0000 (04:39 +0000)
X-SVN-Rev: 35310

icu4j/main/tests/collate/src/com/ibm/icu/dev/test/collator/CollationThreadTest.java

index e40c5168a96acf1f1fb01dc1f7bb78af4051ecce..8f06b9c349545a4bdd6af4d40d32230cde7c4e79 100644 (file)
@@ -1,6 +1,6 @@
 /*
  *******************************************************************************
- * Copyright (C) 2007-2011, International Business Machines Corporation and    *
+ * Copyright (C) 2007-2014, International Business Machines Corporation and    *
  * others. All Rights Reserved.                                                *
  *******************************************************************************
  */
@@ -158,82 +158,70 @@ public class CollationThreadTest extends TestFmwk {
         return true;
     }
 
-    public void testThreads() {
-        final Collator theCollator = Collator.getInstance(new Locale("pl", "", ""));
-        final String[] theData = threadTestData;
-        final Random r = new Random();
-
-        class Control {
-            private boolean go;
-            private String fail;
-
-            synchronized void start() {
-                go = true;
-                notifyAll();
-            }
-
-            synchronized void stop() {
-                go = false;
-                notifyAll();
-            }
+    private static class Control {
+        private boolean go;
+        private String fail;
 
-            boolean go() {
-                return go;
-            }
+        synchronized void start() {
+            go = true;
+            notifyAll();
+        }
 
-            void fail(String msg) {
-                fail = msg;
-                stop();
-            }
+        synchronized void stop() {
+            go = false;
+            notifyAll();
         }
 
-        final Control control = new Control();
+        boolean go() {
+            return go;
+        }
 
-        class Test implements Runnable {
-            private String[] data;
-            private Collator collator;
-            private String name;
+        void fail(String msg) {
+            fail = msg;
+            stop();
+        }
+    }
 
-            Test(String name) {
-                this.name = name;
+    private static class Test implements Runnable {
+        private String[] data;
+        private Collator collator;
+        private String name;
+        private Control control;
+        private Random r;
 
-                try {
-                    data = (String[]) theData.clone();
-                    collator = (Collator) theCollator.clone();
-                } catch (CloneNotSupportedException e) {
-                    // should not happen, if it does we'll get an exception right away
-                    errln("could not clone");
-                    data = null;
-                    collator = null;
-                }
-            }
+        Test(String name, String[] data, Collator collator, Random r, Control control) {
+            this.name = name;
+            this.data = data;
+            this.collator = collator;
+            this.control = control;
+            this.r = r;
+        }
 
-            public void run() {
-                try {
-                    synchronized (control) {
-                        while (!control.go()) {
-                            control.wait();
-                        }
+        public void run() {
+            try {
+                synchronized (control) {
+                    while (!control.go()) {
+                        control.wait();
                     }
+                }
 
-                    while (control.go()) {
-                        scramble(this.data, r);
-                        sort(this.data, this.collator);
-                        if (!verifySort(this.data)) {
-                            control.fail(name + ": incorrect sort");
-                        }
+                while (control.go()) {
+                    scramble(this.data, r);
+                    sort(this.data, this.collator);
+                    if (!verifySort(this.data)) {
+                        control.fail(name + ": incorrect sort");
                     }
-                } catch (InterruptedException e) {
-                    // die
-                } catch (IndexOutOfBoundsException e) {
-                    control.fail(name + " " + e.getMessage());
                 }
+            } catch (InterruptedException e) {
+                // die
+            } catch (IndexOutOfBoundsException e) {
+                control.fail(name + " " + e.getMessage());
             }
         }
+    }
 
-        Thread[] threads = new Thread[10];
+    private void runThreads(Thread[] threads, Control control) {
         for (int i = 0; i < threads.length; ++i) {
-            threads[i] = new Thread(new Test("test " + i));
             threads[i].start();
         }
 
@@ -259,51 +247,42 @@ public class CollationThreadTest extends TestFmwk {
         }
     }
 
+    public void testThreads() {
+        final Collator theCollator = Collator.getInstance(new Locale("pl", "", ""));
+        final Random r = new Random();
+        final Control control = new Control();
 
-    class FrozenCollatorTest implements Runnable {
-        private final String name;
-        private final Collator collator;
-        private final String[] data;
-        private final Random r;
-
-        public FrozenCollatorTest(String name, Collator collator, String[] data, Random r) {
-            this.name = name;
-            this.collator = collator;
-            this.data = data;
-            this.r = r;
+        Thread[] threads = new Thread[10];
+        for (int i = 0; i < threads.length; ++i) {
+            Collator coll;
+            try {
+                coll = (Collator)theCollator.clone();
+            } catch (CloneNotSupportedException e) {
+                // should not happen, if it does we'll get an exception right away
+                errln("could not clone");
+                return;
+            }
+            Test test = new Test("Collation test thread" + i, threadTestData.clone(), coll,
+                    r, control);
+            threads[i] = new Thread(test);
         }
 
-        public void run() {
-            while(true) {
-                try {
-                    scramble(this.data, r);
-                    sort(this.data, this.collator);
-                    if (!verifySort(this.data)) {
-                        errln("Error in frozen collation test: thread (" + this.name + ") incorrect sort");
-                    }
-                } catch (Exception e) {
-                    errln("Error in frozen collation test: thread (" + this.name + ") exception = " + e);
-                }
-            }
-        }   
+        runThreads(threads, control);
     }
 
-    public void testFrozenThreads() throws Exception {
+    public void testFrozen() {
         final Collator theCollator = Collator.getInstance(new Locale("pl", "", ""));
         theCollator.freeze();
         final Random r = new Random();
+        Control control = new Control();
 
         Thread[] threads = new Thread[10];
         for (int i = 0; i < threads.length; ++i) {
-            threads[i] = new Thread(new FrozenCollatorTest("test " + i, theCollator, threadTestData.clone(), r));
+            Test test = new Test("Frozen collation test thread " + i, threadTestData.clone(), theCollator,
+                    r, control);
+            threads[i] = new Thread(test);
         }
 
-        for (int i = 0; i < threads.length; ++i) {
-            threads[i].start();
-        }
-
-        for (int i = 0; i < threads.length; ++i) {
-            threads[i].join(500);
-        }
+        runThreads(threads, control);
     }
 }