Browse Source

Merge "Using java.util.concurrent in NLSTest instead of handling threads directly."

stable-0.11
Shawn Pearce 14 years ago committed by Code Review
parent
commit
be38185a03
  1. 58
      org.eclipse.jgit.test/tst/org/eclipse/jgit/nls/NLSTest.java

58
org.eclipse.jgit.test/tst/org/eclipse/jgit/nls/NLSTest.java

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

Loading…
Cancel
Save