Apache Lucene open-source search software
LUCENE-6502: Drastically reduce the probability Geo3d ShapeRectRelationTestCase will complain about not finding enough predicate occurrences.
David Wayne Smiley8ae97d60a539
diff --git a/lucene/spatial/src/test/org/apache/lucene/spatial/spatial4j/Geo3dShapeRectRelationTestCase.java b/lucene/spatial/src/test/org/apache/lucene/spatial/spatial4j/Geo3dShapeRectRelationTestCase.javaindex 14b117288c3..ba37d0fcdf9 100644--- a/lucene/spatial/src/test/org/apache/lucene/spatial/spatial4j/Geo3dShapeRectRelationTestCase.java+++ b/lucene/spatial/src/test/org/apache/lucene/spatial/spatial4j/Geo3dShapeRectRelationTestCase.java@@ -83,9 +83,27 @@ public abstract class Geo3dShapeRectRelationTestCase extends RandomizedShapeTestreturn GeoBBoxFactory.makeGeoBBox(planetModel, maxLat, minLat, leftLon, rightLon);}+ abstract class Geo3dRectIntersectionTestHelper extends RectIntersectionTestHelper<Geo3dShape> {++ public Geo3dRectIntersectionTestHelper(SpatialContext ctx) {+ super(ctx);+ }++ @Override+ protected int getMaxLaps() {+ //sometimes, getWithinMinimum needs some more attempts then normal; 20k is suggested max.+ return 200_000;//200k+ }++ @Override+ protected int getDefaultMinimumPredicateFrequency(int maxLaps) {+ return 20;//20 times each -- should be plenty in 200k+ }+ }+@Testpublic void testGeoCircleRect() {- new RectIntersectionTestHelper<Geo3dShape>(ctx) {+ new Geo3dRectIntersectionTestHelper(ctx) {@Overrideprotected Geo3dShape generateRandomShape(Point nearP) {@@ -115,7 +133,7 @@ public abstract class Geo3dShapeRectRelationTestCase extends RandomizedShapeTest@Testpublic void testGeoBBoxRect() {- new RectIntersectionTestHelper<Geo3dShape>(ctx) {+ new Geo3dRectIntersectionTestHelper(ctx) {@Overrideprotected boolean isRandomShapeRectangular() {@@ -149,7 +167,7 @@ public abstract class Geo3dShapeRectRelationTestCase extends RandomizedShapeTest@Testpublic void testGeoPolygonRect() {- new RectIntersectionTestHelper<Geo3dShape>(ctx) {+ new Geo3dRectIntersectionTestHelper(ctx) {@Overrideprotected Geo3dShape generateRandomShape(Point nearP) {@@ -184,8 +202,8 @@ public abstract class Geo3dShapeRectRelationTestCase extends RandomizedShapeTest@Overrideprotected int getWithinMinimum(int laps) {- // Long/thin so only 10% of the usual figure- return laps/10000;+ // Long/thin so lets just find 1.+ return 1;}}.testRelateWithRectangle();@@ -193,7 +211,7 @@ public abstract class Geo3dShapeRectRelationTestCase extends RandomizedShapeTest@Testpublic void testGeoPathRect() {- new RectIntersectionTestHelper<Geo3dShape>(ctx) {+ new Geo3dRectIntersectionTestHelper(ctx) {@Overrideprotected Geo3dShape generateRandomShape(Point nearP) {@@ -229,8 +247,8 @@ public abstract class Geo3dShapeRectRelationTestCase extends RandomizedShapeTest@Overrideprotected int getWithinMinimum(int laps) {- // Long/thin so only 10% of the usual figure- return laps/10000;+ // Long/thin so lets just find 1.+ return 1;}}.testRelateWithRectangle();diff --git a/lucene/spatial/src/test/org/apache/lucene/spatial/spatial4j/RectIntersectionTestHelper.java b/lucene/spatial/src/test/org/apache/lucene/spatial/spatial4j/RectIntersectionTestHelper.javaindex 56e826b459f..7fcda14cb72 100644--- a/lucene/spatial/src/test/org/apache/lucene/spatial/spatial4j/RectIntersectionTestHelper.java+++ b/lucene/spatial/src/test/org/apache/lucene/spatial/spatial4j/RectIntersectionTestHelper.java@@ -49,27 +49,37 @@ public abstract class RectIntersectionTestHelper<S extends Shape> extends Random// Each shape has different characteristics, so we don't expect (for instance) shapes that// are likely to be long and thin to contain as many rectangles as those that// short and fat.-- protected int getContainsMinimum(int laps) {- return laps/1000;++ /** Called once by {@link #testRelateWithRectangle()} to determine the max laps to try before failing. */+ protected int getMaxLaps() {+ return scaledRandomIntBetween(20_000, 200_000);}-- protected int getIntersectsMinimum(int laps) {- return laps/1000;++ /** The minimum number of times we need to see each predicate in {@code laps} iterations. */+ protected int getDefaultMinimumPredicateFrequency(int maxLaps) {+ return maxLaps / 1000;}-- protected int getWithinMinimum(int laps) {- return laps/1000;++ protected int getContainsMinimum(int maxLaps) {+ return getDefaultMinimumPredicateFrequency(maxLaps);}-- protected int getDisjointMinimum(int laps) {- return laps/1000;++ protected int getIntersectsMinimum(int maxLaps) {+ return getDefaultMinimumPredicateFrequency(maxLaps);}-- protected int getBoundingMinimum(int laps) {- return laps/1000;++ protected int getWithinMinimum(int maxLaps) {+ return getDefaultMinimumPredicateFrequency(maxLaps);}-++ protected int getDisjointMinimum(int maxLaps) {+ return getDefaultMinimumPredicateFrequency(maxLaps);+ }++ protected int getBoundingMinimum(int maxLaps) {+ return getDefaultMinimumPredicateFrequency(maxLaps);+ }+@SuppressWarnings("unchecked")@Overrideprotected Point randomPointInOrNull(Shape shape) {@@ -84,23 +94,23 @@ public abstract class RectIntersectionTestHelper<S extends Shape> extends Randompublic void testRelateWithRectangle() {//counters for the different intersection casesint i_C = 0, i_I = 0, i_W = 0, i_D = 0, i_bboxD = 0;- int laps = 0;- final int MINLAPS = scaledRandomIntBetween(20000, 200000);- while(i_C < getContainsMinimum(MINLAPS) || i_I < getIntersectsMinimum(MINLAPS) || i_W < getWithinMinimum(MINLAPS)- || (!isRandomShapeRectangular() && i_D < getDisjointMinimum(MINLAPS)) || i_bboxD < getBoundingMinimum(MINLAPS)) {- laps++;+ int lap = 0;+ final int MAXLAPS = getMaxLaps();+ while(i_C < getContainsMinimum(MAXLAPS) || i_I < getIntersectsMinimum(MAXLAPS) || i_W < getWithinMinimum(MAXLAPS)+ || (!isRandomShapeRectangular() && i_D < getDisjointMinimum(MAXLAPS)) || i_bboxD < getBoundingMinimum(MAXLAPS)) {+ lap++;LogRule.clear();- if (laps > MINLAPS) {+ if (lap > MAXLAPS) {fail("Did not find enough contains/within/intersection/disjoint/bounds cases in a reasonable number" +- " of random attempts. CWIDbD: " +- i_C + "("+getContainsMinimum(MINLAPS)+")," +- i_W + "("+getWithinMinimum(MINLAPS)+")," +- i_I + "("+getIntersectsMinimum(MINLAPS)+")," +- i_D + "("+getDisjointMinimum(MINLAPS)+")," +- i_bboxD + "("+getBoundingMinimum(MINLAPS)+")"- + " Laps exceeded " + MINLAPS);+ " of attempts. CWIDbD: " ++ i_C + "("+getContainsMinimum(MAXLAPS)+")," ++ i_W + "("+getWithinMinimum(MAXLAPS)+")," ++ i_I + "("+getIntersectsMinimum(MAXLAPS)+")," ++ i_D + "("+getDisjointMinimum(MAXLAPS)+")," ++ i_bboxD + "("+getBoundingMinimum(MAXLAPS)+")"+ + " Laps exceeded " + MAXLAPS);}Point nearP = randomPointIn(ctx.getWorldBounds());@@ -111,7 +121,7 @@ public abstract class RectIntersectionTestHelper<S extends Shape> extends RandomSpatialRelation ic = s.relate(r);- LogRule.log("S-R Rel: {}, Shape {}, Rectangle {} lap# {}", ic, s, r, laps);+ LogRule.log("S-R Rel: {}, Shape {}, Rectangle {} lap# {}", ic, s, r, lap);if (ic != DISJOINT) {assertTrue("if not disjoint then the shape's bbox shouldn't be disjoint",@@ -143,7 +153,7 @@ public abstract class RectIntersectionTestHelper<S extends Shape> extends Randomcase DISJOINT:if (!s.getBoundingBox().relate(r).intersects()) {//bboxes are disjointi_bboxD++;- if (i_bboxD >= getBoundingMinimum(MINLAPS))+ if (i_bboxD >= getBoundingMinimum(MAXLAPS))break;} else {i_D++;@@ -195,7 +205,7 @@ public abstract class RectIntersectionTestHelper<S extends Shape> extends Random} // while loop- System.out.println("Laps: "+laps + " CWIDbD: "+i_C+","+i_W+","+i_I+","+i_D+","+i_bboxD);+ System.out.println("Laps: "+lap + " CWIDbD: "+i_C+","+i_W+","+i_I+","+i_D+","+i_bboxD);}protected void onAssertFail(AssertionError e, S s, Rectangle r, SpatialRelation ic) {