Kaynağa Gözat

HADOOP-3471. Fix spurious errors from TestIndexedSort and add additional
logging to let failures be reproducible.



git-svn-id: https://svn.apache.org/repos/asf/hadoop/core/trunk@662569 13f79535-47bb-0310-9956-ffa450edef68

Christopher Douglas 17 yıl önce
ebeveyn
işleme
eaa591dd94

+ 3 - 0
CHANGES.txt

@@ -385,6 +385,9 @@ Trunk (unreleased changes)
     /bin/bash and fix the test patch to require bash instead of sh.
     (Brice Arnould via omalley)
 
+    HADOOP-3471. Fix spurious errors from TestIndexedSort and add additional
+    logging to let failures be reproducible. (cdouglas)
+
 Release 0.17.0 - 2008-05-18
 
   INCOMPATIBLE CHANGES

+ 26 - 4
src/test/org/apache/hadoop/util/TestIndexedSort.java

@@ -35,6 +35,7 @@ public class TestIndexedSort extends TestCase {
     private int[] valindex;
     private int[] valindirect;
     private int[] values;
+    private final long seed;
 
     public SampleSortable() {
       this(50);
@@ -42,6 +43,8 @@ public class TestIndexedSort extends TestCase {
 
     public SampleSortable(int j) {
       Random r = new Random();
+      seed = r.nextLong();
+      r.setSeed(seed);
       values = new int[j];
       valindex = new int[j];
       valindirect = new int[j];
@@ -58,6 +61,11 @@ public class TestIndexedSort extends TestCase {
       for (int i = 0; i < values.length; ++i) {
         valindex[i] = valindirect[i] = i;
       }
+      seed = 0;
+    }
+
+    public long getSeed() {
+      return seed;
     }
 
     public int compare(int i, int j) {
@@ -97,12 +105,15 @@ public class TestIndexedSort extends TestCase {
     private final byte[] bytes;
     private final WritableComparator comparator;
     private final String[] check;
+    private final long seed;
 
     public WritableSortable() throws IOException {
       this(100);
     }
 
     public WritableSortable(int j) throws IOException {
+      seed = r.nextLong();
+      r.setSeed(seed);
       Text t = new Text();
       StringBuffer sb = new StringBuffer();
       indices = new int[j];
@@ -121,6 +132,10 @@ public class TestIndexedSort extends TestCase {
       comparator = WritableComparator.get(Text.class);
     }
 
+    public long getSeed() {
+      return seed;
+    }
+
     private static void genRandom(Text t, int len, StringBuffer sb) {
       sb.setLength(0);
       for (int i = 0; i < len; ++i) {
@@ -174,10 +189,13 @@ public class TestIndexedSort extends TestCase {
     int[] check = s.getSorted();
     assertTrue(Arrays.toString(values) + "\ndoesn't match\n" +
         Arrays.toString(check), Arrays.equals(values, check));
+    // Set random min/max, re-sort.
     Random r = new Random();
-    int diff = r.nextInt(SAMPLE);
-    values[diff] = 9;
-    values[(diff + r.nextInt(SAMPLE >>> 1)) % SAMPLE] = 11;
+    int min = r.nextInt(SAMPLE);
+    int max = (min + 1 + r.nextInt(SAMPLE - 2)) % SAMPLE;
+    values[min] = 9;
+    values[max] = 11;
+    System.out.println("testAllEqual setting min/max at " + min + "/" + max);
     s = new SampleSortable(values);
     sorter.sort(s, 0, SAMPLE);
     check = s.getSorted();
@@ -192,6 +210,9 @@ public class TestIndexedSort extends TestCase {
     final int SAMPLE = 500;
     int[] values = new int[SAMPLE];
     Random r = new Random();
+    long seed = r.nextLong();
+    r.setSeed(seed);
+    System.out.println("testSorted seed: " + seed);
     for (int i = 0; i < SAMPLE; ++i) {
       values[i] = r.nextInt(100);
     }
@@ -222,7 +243,6 @@ public class TestIndexedSort extends TestCase {
     final int SAMPLE = 1;
     SampleSortable s = new SampleSortable(SAMPLE);
     int[] values = s.getValues();
-    Arrays.sort(values);
     IndexedSorter sorter = new QuickSort();
     sorter.sort(s, 0, SAMPLE);
     int[] check = s.getSorted();
@@ -233,6 +253,7 @@ public class TestIndexedSort extends TestCase {
   public void testQuickSort() throws Exception {
     final int SAMPLE = 100000;
     SampleSortable s = new SampleSortable(SAMPLE);
+    System.out.println("testQuickSort seed: " + s.getSeed());
     int[] values = s.getValues();
     Arrays.sort(values);
     IndexedSorter sorter = new QuickSort();
@@ -245,6 +266,7 @@ public class TestIndexedSort extends TestCase {
   public void testWritable() throws Exception {
     final int SAMPLE = 1000;
     WritableSortable s = new WritableSortable(SAMPLE);
+    System.out.println("testWritable seed: " + s.getSeed());
     String[] values = s.getValues();
     Arrays.sort(values);
     IndexedSorter sorter = new QuickSort();