]> source.dussan.org Git - jgit.git/commitdiff
Using java.util.concurrent in NLSTest instead of handling threads directly. 19/2219/2
authorSasa Zivkov <sasa.zivkov@sap.com>
Fri, 7 Jan 2011 12:38:28 +0000 (13:38 +0100)
committerSasa Zivkov <sasa.zivkov@sap.com>
Mon, 10 Jan 2011 08:15:26 +0000 (09:15 +0100)
A test in NLSTest was mixing the "old" and the "new" way of handling
concurrency. This change makes use of the java.util.concurrent facilities to
control concurrency and removes the code that was directly dealing with Thread
objects.

Change-Id: Ie7267776e988a48a5443f0f3fe4eb43e79eee4b1
Signed-off-by: Sasa Zivkov <sasa.zivkov@sap.com>
org.eclipse.jgit.test/tst/org/eclipse/jgit/nls/NLSTest.java

index 6d81f867ee382546f1aa246ff7f0e73db2a8f81c..a74ea98742f528a39913161dc46b25fae1fc45cc 100644 (file)
 package org.eclipse.jgit.nls;
 
 import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertNull;
 import static org.junit.Assert.assertSame;
 
 import java.util.Locale;
-import java.util.concurrent.BrokenBarrierException;
+import java.util.concurrent.Callable;
 import java.util.concurrent.CyclicBarrier;
+import java.util.concurrent.ExecutionException;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.Executors;
+import java.util.concurrent.Future;
+import java.util.concurrent.TimeUnit;
 
 import org.junit.Test;
 
@@ -106,43 +110,37 @@ public class NLSTest {
        }
 
        @Test
-       public void testParallelThreadsWithDifferentLocales() throws InterruptedException {
+       public void testParallelThreadsWithDifferentLocales()
+                       throws InterruptedException, ExecutionException {
 
                final CyclicBarrier barrier = new CyclicBarrier(2);
 
-               class T extends Thread {
-                       Locale locale;
-                       GermanTranslatedBundle bundle;
-                       Exception e;
+               class GetBundle implements Callable<TranslationBundle> {
+
+                       private Locale locale;
 
-                       T(Locale locale) {
+                       GetBundle(Locale locale) {
                                this.locale = locale;
                        }
 
-                       @Override
-                       public void run() {
-                               try {
-                                       NLS.setLocale(locale);
-                                       barrier.await(); // wait for the other thread to set its locale
-                                       bundle = GermanTranslatedBundle.get();
-                               } catch (InterruptedException e) {
-                                       this.e = e;
-                               } catch (BrokenBarrierException e) {
-                                       this.e = e;
-                               }
+                       public TranslationBundle call() throws Exception {
+                               NLS.setLocale(locale);
+                               barrier.await(); // wait for the other thread to set its locale
+                               return GermanTranslatedBundle.get();
                        }
                }
 
-               T t1 = new T(NLS.ROOT_LOCALE);
-               T t2 = new T(Locale.GERMAN);
-               t1.start();
-               t2.start();
-               t1.join();
-               t2.join();
-
-               assertNull("t1 was interrupted or barrier was broken", t1.e);
-               assertNull("t2 was interrupted or barrier was broken", t2.e);
-               assertEquals(NLS.ROOT_LOCALE, t1.bundle.effectiveLocale());
-               assertEquals(Locale.GERMAN, t2.bundle.effectiveLocale());
+               ExecutorService pool = Executors.newFixedThreadPool(2);
+               try {
+                       Future<TranslationBundle> root = pool.submit(new GetBundle(
+                                       NLS.ROOT_LOCALE));
+                       Future<TranslationBundle> german = pool.submit(new GetBundle(
+                                       Locale.GERMAN));
+                       assertEquals(NLS.ROOT_LOCALE, root.get().effectiveLocale());
+                       assertEquals(Locale.GERMAN, german.get().effectiveLocale());
+               } finally {
+                       pool.shutdown();
+                       pool.awaitTermination(Long.MAX_VALUE, TimeUnit.SECONDS);
+               }
        }
 }