< prev index next >

test/java/lang/invoke/MethodHandles/CatchExceptionTest.java

Print this page
rev 11972 : 8055269: java/lang/invoke/MethodHandles/CatchExceptionTest.java fails intermittently
Reviewed-by:


  55                 byte.class,
  56                 Integer[].class,
  57                 double[].class,
  58                 String.class,
  59         };
  60         ARGS_CLASSES = Collections.unmodifiableList(
  61                 Helper.randomClasses(classes, MAX_ARITY));
  62     }
  63 
  64     private final TestCase testCase;
  65     private final int nargs;
  66     private final int argsCount;
  67     private final MethodHandle catcher;
  68     private int dropped;
  69     private MethodHandle thrower;
  70 
  71     public CatchExceptionTest(TestCase testCase, final boolean isVararg, final int argsCount,
  72             final int catchDrops) {
  73         this.testCase = testCase;
  74         this.dropped = catchDrops;
  75         if (Helper.IS_VERBOSE) {
  76             System.out.printf("CatchException::CatchException(%s, isVararg=%b " +
  77                             "argsCount=%d catchDrops=%d)%n",
  78                     testCase, isVararg, argsCount, catchDrops
  79             );
  80         }
  81         MethodHandle thrower = testCase.thrower;
  82         int throwerLen = thrower.type().parameterCount();
  83         List<Class<?>> classes;
  84         int extra = Math.max(0, argsCount - throwerLen);
  85         classes = getThrowerParams(isVararg, extra);
  86         this.argsCount = throwerLen + classes.size();
  87         thrower = Helper.addTrailingArgs(thrower, this.argsCount, classes);
  88         if (isVararg && argsCount > throwerLen) {
  89             MethodType mt = thrower.type();
  90             Class<?> lastParam = mt.parameterType(mt.parameterCount() - 1);
  91             thrower = thrower.asVarargsCollector(lastParam);
  92         }
  93         this.thrower = thrower;
  94         this.dropped = Math.min(this.argsCount, catchDrops);
  95         catcher = testCase.getCatcher(getCatcherParams());
  96         nargs = Math.max(2, this.argsCount);
  97     }
  98 
  99     public static void main(String[] args) throws Throwable {


 100         TestFactory factory = new TestFactory();
 101         long timeout = Helper.IS_THOROUGH ? 0L : Utils.adjustTimeout(Utils.DEFAULT_TEST_TIMEOUT);
 102         // substract vm init time and reserve time for vm exit
 103         timeout *= 0.9;
 104         TimeLimitedRunner runner = new TimeLimitedRunner(timeout, 2.0d,
 105                 () -> {
 106                     CatchExceptionTest test = factory.nextTest();
 107                     if (test != null) {
 108                         test.runTest();
 109                         return true;
 110                     }
 111                     return false;
 112                 });
 113         for (CatchExceptionTest test : TestFactory.MANDATORY_TEST_CASES) {
 114             test.runTest();
 115         }
 116         runner.call();
 117     }
 118 
 119     private List<Class<?>> getThrowerParams(boolean isVararg, int argsCount) {
 120         return Helper.getParams(ARGS_CLASSES, isVararg, argsCount);
 121     }
 122 
 123 
 124     private List<Class<?>> getCatcherParams() {
 125         int catchArgc = 1 + this.argsCount - dropped;
 126         List<Class<?>> result = new ArrayList<>(
 127                 thrower.type().parameterList().subList(0, catchArgc - 1));
 128         // prepend throwable
 129         result.add(0, testCase.throwableClass);
 130         return result;
 131     }
 132 
 133     private void runTest() {






 134         Helper.clear();
 135 
 136         Object[] args = Helper.randomArgs(
 137                 argsCount, thrower.type().parameterArray());
 138         Object arg0 = Helper.MISSING_ARG;
 139         Object arg1 = testCase.thrown;
 140         if (argsCount > 0) {
 141             arg0 = args[0];
 142         }
 143         if (argsCount > 1) {
 144             args[1] = arg1;
 145         }
 146         Asserts.assertEQ(nargs, thrower.type().parameterCount());
 147         if (argsCount < nargs) {
 148             Object[] appendArgs = {arg0, arg1};
 149             appendArgs = Arrays.copyOfRange(appendArgs, argsCount, nargs);
 150             thrower = MethodHandles.insertArguments(
 151                     thrower, argsCount, appendArgs);
 152         }
 153         Asserts.assertEQ(argsCount, thrower.type().parameterCount());


 195     private int maxArgs;
 196     private int maxDrops;
 197     private int constructor;
 198     private int constructorSize;
 199     private boolean isVararg;
 200 
 201     public TestFactory() {
 202         if (Helper.IS_THOROUGH) {
 203             maxArgs = maxDrops = CatchExceptionTest.MAX_ARITY;
 204         } else {
 205             maxArgs = MIN_TESTED_ARITY
 206                     + Helper.RNG.nextInt(CatchExceptionTest.MAX_ARITY
 207                             - MIN_TESTED_ARITY)
 208                     + 1;
 209             maxDrops = MIN_TESTED_ARITY
 210                     + Helper.RNG.nextInt(maxArgs - MIN_TESTED_ARITY)
 211                     + 1;
 212             args = 1;
 213         }
 214 
 215         if (Helper.IS_VERBOSE) {
 216             System.out.printf("maxArgs = %d%nmaxDrops = %d%n",
 217                     maxArgs, maxDrops);
 218         }
 219         constructorSize = TestCase.CONSTRUCTORS.size();
 220     }
 221 
 222     private static List<CatchExceptionTest> createTests(int argsCount,
 223             int catchDrops) {
 224         if (catchDrops > argsCount || argsCount < 0 || catchDrops < 0) {
 225             throw new IllegalArgumentException("argsCount = " + argsCount
 226                     + ", catchDrops = " + catchDrops
 227             );
 228         }
 229         List<CatchExceptionTest> result = new ArrayList<>(
 230                 TestCase.CONSTRUCTORS.size());
 231         for (Supplier<TestCase> constructor : TestCase.CONSTRUCTORS) {
 232             result.add(new CatchExceptionTest(constructor.get(),
 233                     /* isVararg = */ true,
 234                     argsCount,
 235                     catchDrops));
 236             result.add(new CatchExceptionTest(constructor.get(),
 237                     /* isVararg = */ false,
 238                     argsCount,
 239                     catchDrops));
 240         }
 241         return result;
 242     }
 243 
 244     /**
 245      * @return next test from test matrix:
 246      * {varArgs, noVarArgs} x TestCase.rtypes x TestCase.THROWABLES x {1, .., maxArgs } x {1, .., maxDrops}
 247      */
 248     public CatchExceptionTest nextTest() {
 249         if (constructor < constructorSize) {
 250             return createTest();
 251         }
 252         constructor = 0;
 253         count++;
 254         if (!Helper.IS_THOROUGH && count > Helper.TEST_LIMIT) {
 255             System.out.println("test limit is exceeded");
 256             return null;
 257         }
 258         if (dropArgs <= currentMaxDrops) {
 259             if (dropArgs == 1) {
 260                 if (Helper.IS_THOROUGH || Helper.RNG.nextBoolean()) {
 261                     ++dropArgs;
 262                     return createTest();
 263                 } else if (Helper.IS_VERBOSE) {
 264                     System.out.printf(
 265                             "argsCount=%d : \"drop\" scenarios are skipped%n",
 266                             args);
 267                 }
 268             } else {
 269                 ++dropArgs;
 270                 return createTest();
 271             }
 272         }
 273 
 274         if (args <= maxArgs) {
 275             dropArgs = 1;
 276             currentMaxDrops = Math.min(args, maxDrops);
 277             ++args;
 278             return createTest();
 279         }
 280         return null;
 281     }
 282 
 283     private CatchExceptionTest createTest() {
 284         if (!Helper.IS_THOROUGH) {
 285             return new CatchExceptionTest(
 286                     TestCase.CONSTRUCTORS.get(constructor++).get(),
 287                     Helper.RNG.nextBoolean(), args, dropArgs);
 288         } else {
 289            if (isVararg) {
 290                isVararg = false;
 291                return new CatchExceptionTest(
 292                        TestCase.CONSTRUCTORS.get(constructor++).get(),
 293                        isVararg, args, dropArgs);
 294            } else {
 295                isVararg = true;




  55                 byte.class,
  56                 Integer[].class,
  57                 double[].class,
  58                 String.class,
  59         };
  60         ARGS_CLASSES = Collections.unmodifiableList(
  61                 Helper.randomClasses(classes, MAX_ARITY));
  62     }
  63 
  64     private final TestCase testCase;
  65     private final int nargs;
  66     private final int argsCount;
  67     private final MethodHandle catcher;
  68     private int dropped;
  69     private MethodHandle thrower;
  70 
  71     public CatchExceptionTest(TestCase testCase, final boolean isVararg, final int argsCount,
  72             final int catchDrops) {
  73         this.testCase = testCase;
  74         this.dropped = catchDrops;






  75         MethodHandle thrower = testCase.thrower;
  76         int throwerLen = thrower.type().parameterCount();
  77         List<Class<?>> classes;
  78         int extra = Math.max(0, argsCount - throwerLen);
  79         classes = getThrowerParams(isVararg, extra);
  80         this.argsCount = throwerLen + classes.size();
  81         thrower = Helper.addTrailingArgs(thrower, this.argsCount, classes);
  82         if (isVararg && argsCount > throwerLen) {
  83             MethodType mt = thrower.type();
  84             Class<?> lastParam = mt.parameterType(mt.parameterCount() - 1);
  85             thrower = thrower.asVarargsCollector(lastParam);
  86         }
  87         this.thrower = thrower;
  88         this.dropped = Math.min(this.argsCount, catchDrops);
  89         catcher = testCase.getCatcher(getCatcherParams());
  90         nargs = Math.max(2, this.argsCount);
  91     }
  92 
  93     public static void main(String[] args) throws Throwable {
  94         System.out.println("classes = " + ARGS_CLASSES);
  95 
  96         TestFactory factory = new TestFactory();
  97         long timeout = Helper.IS_THOROUGH ? 0L : Utils.adjustTimeout(Utils.DEFAULT_TEST_TIMEOUT);
  98         // subtract vm init time and reserve time for vm exit
  99         timeout *= 0.9;
 100         TimeLimitedRunner runner = new TimeLimitedRunner(timeout, 2.0d,
 101                 () -> {
 102                     CatchExceptionTest test = factory.nextTest();
 103                     if (test != null) {
 104                         test.runTest();
 105                         return true;
 106                     }
 107                     return false;
 108                 });
 109         for (CatchExceptionTest test : TestFactory.MANDATORY_TEST_CASES) {
 110             test.runTest();
 111         }
 112         runner.call();
 113     }
 114 
 115     private List<Class<?>> getThrowerParams(boolean isVararg, int argsCount) {
 116         return Helper.getParams(ARGS_CLASSES, isVararg, argsCount);
 117     }
 118 
 119 
 120     private List<Class<?>> getCatcherParams() {
 121         int catchArgc = 1 + this.argsCount - dropped;
 122         List<Class<?>> result = new ArrayList<>(
 123                 thrower.type().parameterList().subList(0, catchArgc - 1));
 124         // prepend throwable
 125         result.add(0, testCase.throwableClass);
 126         return result;
 127     }
 128 
 129     private void runTest() {
 130         if (Helper.IS_VERBOSE) {
 131             System.out.printf("CatchException(%s, isVararg=%b argsCount=%d " +
 132                             "dropped=%d)%n",
 133                     testCase, thrower.isVarargsCollector(), argsCount, dropped);
 134         }
 135 
 136         Helper.clear();
 137 
 138         Object[] args = Helper.randomArgs(
 139                 argsCount, thrower.type().parameterArray());
 140         Object arg0 = Helper.MISSING_ARG;
 141         Object arg1 = testCase.thrown;
 142         if (argsCount > 0) {
 143             arg0 = args[0];
 144         }
 145         if (argsCount > 1) {
 146             args[1] = arg1;
 147         }
 148         Asserts.assertEQ(nargs, thrower.type().parameterCount());
 149         if (argsCount < nargs) {
 150             Object[] appendArgs = {arg0, arg1};
 151             appendArgs = Arrays.copyOfRange(appendArgs, argsCount, nargs);
 152             thrower = MethodHandles.insertArguments(
 153                     thrower, argsCount, appendArgs);
 154         }
 155         Asserts.assertEQ(argsCount, thrower.type().parameterCount());


 197     private int maxArgs;
 198     private int maxDrops;
 199     private int constructor;
 200     private int constructorSize;
 201     private boolean isVararg;
 202 
 203     public TestFactory() {
 204         if (Helper.IS_THOROUGH) {
 205             maxArgs = maxDrops = CatchExceptionTest.MAX_ARITY;
 206         } else {
 207             maxArgs = MIN_TESTED_ARITY
 208                     + Helper.RNG.nextInt(CatchExceptionTest.MAX_ARITY
 209                             - MIN_TESTED_ARITY)
 210                     + 1;
 211             maxDrops = MIN_TESTED_ARITY
 212                     + Helper.RNG.nextInt(maxArgs - MIN_TESTED_ARITY)
 213                     + 1;
 214             args = 1;
 215         }
 216 
 217         System.out.printf("maxArgs = %d%nmaxDrops = %d%n", maxArgs, maxDrops);



 218         constructorSize = TestCase.CONSTRUCTORS.size();
 219     }
 220 
 221     private static List<CatchExceptionTest> createTests(int argsCount,
 222             int catchDrops) {
 223         if (catchDrops > argsCount || argsCount < 0 || catchDrops < 0) {
 224             throw new IllegalArgumentException("argsCount = " + argsCount
 225                     + ", catchDrops = " + catchDrops
 226             );
 227         }
 228         List<CatchExceptionTest> result = new ArrayList<>(
 229                 TestCase.CONSTRUCTORS.size());
 230         for (Supplier<TestCase> constructor : TestCase.CONSTRUCTORS) {
 231             result.add(new CatchExceptionTest(constructor.get(),
 232                     /* isVararg = */ true,
 233                     argsCount,
 234                     catchDrops));
 235             result.add(new CatchExceptionTest(constructor.get(),
 236                     /* isVararg = */ false,
 237                     argsCount,
 238                     catchDrops));
 239         }
 240         return result;
 241     }
 242 
 243     /**
 244      * @return next test from test matrix:
 245      * {varArgs, noVarArgs} x TestCase.rtypes x TestCase.THROWABLES x {1, .., maxArgs } x {0, .., maxDrops}
 246      */
 247     public CatchExceptionTest nextTest() {
 248         if (constructor < constructorSize) {
 249             return createTest();
 250         }
 251         constructor = 0;
 252         count++;
 253         if (!Helper.IS_THOROUGH && count > Helper.TEST_LIMIT) {
 254             System.out.println("test limit is exceeded");
 255             return null;
 256         }
 257         if (dropArgs <= currentMaxDrops) {
 258             if (dropArgs == 0) {
 259                 if (Helper.IS_THOROUGH || Helper.RNG.nextBoolean()) {
 260                     ++dropArgs;
 261                     return createTest();
 262                 } else if (Helper.IS_VERBOSE) {
 263                     System.out.printf(
 264                             "argsCount=%d : \"drop\" scenarios are skipped%n",
 265                             args);
 266                 }
 267             } else {
 268                 ++dropArgs;
 269                 return createTest();
 270             }
 271         }
 272 
 273         if (args < maxArgs) {
 274             dropArgs = 0;
 275             currentMaxDrops = Math.min(args, maxDrops);
 276             ++args;
 277             return createTest();
 278         }
 279         return null;
 280     }
 281 
 282     private CatchExceptionTest createTest() {
 283         if (!Helper.IS_THOROUGH) {
 284             return new CatchExceptionTest(
 285                     TestCase.CONSTRUCTORS.get(constructor++).get(),
 286                     Helper.RNG.nextBoolean(), args, dropArgs);
 287         } else {
 288            if (isVararg) {
 289                isVararg = false;
 290                return new CatchExceptionTest(
 291                        TestCase.CONSTRUCTORS.get(constructor++).get(),
 292                        isVararg, args, dropArgs);
 293            } else {
 294                isVararg = true;


< prev index next >