From 584aaa08f84cc44b49ba2d8e2ffd787a69945864 Mon Sep 17 00:00:00 2001 From: Nicholas Knize Date: Thu, 17 Sep 2015 13:23:46 -0500 Subject: [PATCH] Fix RandomShapeGenerator to retry on JTS Assertion and RandomShapeException A JTS bug causes a misinterpretation of polygon coordinates leading to an unhelpful "geom" AssertionError. While this assertion occurs approx 0.02% of the time it can lead to a misleading test failure. This patch catches the geom assertion and retries randomShapeCreation. For safety a threshold is set to prevent unlimited retrying - though 1 retry is typically sufficient for correcting the invalid shape. closes #13551 --- .../test/geo/RandomShapeGenerator.java | 23 +++++++++++++++---- 1 file changed, 18 insertions(+), 5 deletions(-) diff --git a/core/src/test/java/org/elasticsearch/test/geo/RandomShapeGenerator.java b/core/src/test/java/org/elasticsearch/test/geo/RandomShapeGenerator.java index e0945d09761..7144ab71c22 100644 --- a/core/src/test/java/org/elasticsearch/test/geo/RandomShapeGenerator.java +++ b/core/src/test/java/org/elasticsearch/test/geo/RandomShapeGenerator.java @@ -139,13 +139,21 @@ public class RandomShapeGenerator { } private static ShapeBuilder createShape(Random r, Point nearPoint, Rectangle within, ShapeType st) throws InvalidShapeException { - return createShape(r, nearPoint, within, st, ST_VALIDATE); + ShapeBuilder shape; + short i=0; + do { + shape = createShape(r, nearPoint, within, st, ST_VALIDATE); + if (shape != null) { + return shape; + } + } while (++i != 100); + throw new InvalidShapeException("Unable to create a valid random shape with provided seed"); } /** * Creates a random shape useful for randomized testing, NOTE: exercise caution when using this to build random GeometryCollections * as creating a large random number of random shapes can result in massive resource consumption - * see: {@link org.elasticsearch.search.geo.GeoShapeIntegrationTests#testShapeFilterWithRandomGeoCollection} + * see: {@link org.elasticsearch.search.geo.GeoShapeIntegrationIT#testShapeFilterWithRandomGeoCollection} * * The following options are included * @param nearPoint Create a shape near a provided point @@ -218,9 +226,14 @@ public class RandomShapeGenerator { // The validate flag will check for these possibilities and bail if an incorrect geometry is created try { pgb.build(); - } catch (InvalidShapeException e) { - // jts bug rarely results in an invalid shape, if it does happen we try again instead of returning null - return createShape(r, nearPoint, within, st, validate); + } catch (Throwable e) { + // jts bug may occasionally misinterpret coordinate order causing an unhelpful ('geom' assertion) + // or InvalidShapeException + if (e instanceof InvalidShapeException || e instanceof AssertionError) { + return null; + } + // throw any other exception + throw e; } } return pgb;