aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorShawn Pearce2011-01-10 10:05:28 -0500
committerCode Review2011-01-10 10:05:28 -0500
commitbe38185a033021da707f934caf96b68070050bd5 (patch)
treee88355f08581efd63c7764b2e1226619fe5ab616
parent838fdb342b7964af2de1b163fffb7b22279e780d (diff)
parentd7ca89204217e61649247512a22196705ae6f433 (diff)
downloadjgit-be38185a033021da707f934caf96b68070050bd5.zip
jgit-be38185a033021da707f934caf96b68070050bd5.tar.gz
jgit-be38185a033021da707f934caf96b68070050bd5.tar.xz
Merge "Using java.util.concurrent in NLSTest instead of handling threads directly."
-rw-r--r--org.eclipse.jgit.test/tst/org/eclipse/jgit/nls/NLSTest.java58
1 files changed, 28 insertions, 30 deletions
diff --git a/org.eclipse.jgit.test/tst/org/eclipse/jgit/nls/NLSTest.java b/org.eclipse.jgit.test/tst/org/eclipse/jgit/nls/NLSTest.java
index 6d81f86..a74ea98 100644
--- a/org.eclipse.jgit.test/tst/org/eclipse/jgit/nls/NLSTest.java
+++ b/org.eclipse.jgit.test/tst/org/eclipse/jgit/nls/NLSTest.java
@@ -44,12 +44,16 @@
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);
+ }
}
}