Home | History | Annotate | Download | only in format
      1 /* GENERATED SOURCE. DO NOT MODIFY. */
      2 //  2016 and later: Unicode, Inc. and others.
      3 // License & terms of use: http://www.unicode.org/copyright.html#License
      4 
      5 package android.icu.dev.test.format;
      6 
      7 import org.junit.Test;
      8 import org.junit.runner.RunWith;
      9 import org.junit.runners.JUnit4;
     10 
     11 import android.icu.dev.test.TestFmwk;
     12 import android.icu.impl.DontCareFieldPosition;
     13 import android.icu.text.MeasureFormat;
     14 import android.icu.util.Currency;
     15 import android.icu.util.Measure;
     16 import android.icu.util.MeasureUnit;
     17 import android.icu.util.ULocale;
     18 import android.icu.testsharding.MainTestShard;
     19 
     20 @MainTestShard
     21 @RunWith(JUnit4.class)
     22 public class MeasureUnitThreadTest extends TestFmwk {
     23 
     24     @Test
     25     public void MUThreadTest() {
     26         // Ticket #12034 deadlock on multi-threaded static init of MeasureUnit.
     27         // The code below reliably deadlocks with ICU 56.
     28         // The test is here in its own file so it can be made to run independent of anything else.
     29         Thread thread = new Thread()  {
     30             @Override
     31             public void run() {
     32                 MeasureUnit.getAvailableTypes();
     33             }
     34         };
     35         thread.start();
     36         Currency.getInstance(ULocale.ENGLISH);
     37         try {thread.join();} catch(InterruptedException e) {};
     38     }
     39 
     40     static class NumericMeasureThread extends Thread {
     41         final MeasureFormat mf;
     42         final Measure[] arg;
     43         final String expected;
     44         volatile boolean running = true;
     45         AssertionError error;
     46 
     47         NumericMeasureThread(Measure[] arg, String expected) {
     48             this.mf = MeasureFormat.getInstance(ULocale.ENGLISH, MeasureFormat.FormatWidth.NUMERIC);
     49             this.arg = arg;
     50             this.expected = expected;
     51             this.error = null;
     52         }
     53 
     54         @Override
     55         public void run() {
     56             while (running) {
     57                 try {
     58                     StringBuilder sb = new StringBuilder();
     59                     mf.formatMeasures(sb, DontCareFieldPosition.INSTANCE, arg);
     60                     org.junit.Assert.assertEquals(expected, sb.toString());
     61                 } catch (AssertionError e) {
     62                     error = e;
     63                     break;
     64                 }
     65             }
     66         }
     67     }
     68 
     69     // Race in formatMeasures with width NUMERIC:
     70     // http://bugs.icu-project.org/trac/ticket/13606
     71     @Test
     72     public void NumericRaceTest() throws InterruptedException {
     73         NumericMeasureThread t1 = new NumericMeasureThread(new Measure[] {
     74           new Measure(3, MeasureUnit.MINUTE),
     75           new Measure(4, MeasureUnit.SECOND)
     76         }, "3:04");
     77         NumericMeasureThread t2 = new NumericMeasureThread(new Measure[] {
     78           new Measure(5, MeasureUnit.MINUTE),
     79           new Measure(6, MeasureUnit.SECOND)
     80         }, "5:06");
     81         t1.start();
     82         t2.start();
     83         Thread.sleep(5);
     84         t1.running = false;
     85         t2.running = false;
     86         t1.join();
     87         t2.join();
     88         if (t1.error != null) {
     89             AssertionError error = new AssertionError("Failure in thread 1");
     90             error.initCause(t1.error);
     91             throw error;
     92         }
     93         if (t2.error != null) {
     94             AssertionError error = new AssertionError("Failure in thread 2");
     95             error.initCause(t2.error);
     96             throw error;
     97         }
     98     }
     99 }
    100