GrowableOffsetRangeTracker extends OffsetRangeTracker { @Override public Progress getProgress() { if (range.getTo() != Long.MAX_VALUE || range.getTo() == range.getFrom()) { return super.getProgress(); } BigDecimal estimateRangeEnd = BigDecimal.valueOf(rangeEndEstimator.estimate()); if (lastAttemptedOffset == null) { return Progress.from( 0, estimateRangeEnd .subtract(BigDecimal.valueOf(range.getFrom()), MathContext.DECIMAL128) .max(BigDecimal.ZERO) .doubleValue()); } BigDecimal workRemaining = estimateRangeEnd .subtract(BigDecimal.valueOf(lastAttemptedOffset), MathContext.DECIMAL128) .max(BigDecimal.ZERO); BigDecimal totalWork = estimateRangeEnd .max(BigDecimal.valueOf(lastAttemptedOffset)) .subtract(BigDecimal.valueOf(range.getFrom()), MathContext.DECIMAL128); return Progress.from( totalWork.subtract(workRemaining, MathContext.DECIMAL128).doubleValue(), workRemaining.doubleValue()); } GrowableOffsetRangeTracker(long start, RangeEndEstimator rangeEndEstimator); @Override SplitResult<OffsetRange> trySplit(double fractionOfRemainder); @Override Progress getProgress(); @Override IsBounded isBounded();  }
@Test public void testProgressBeforeStart() throws Exception { SimpleEstimator simpleEstimator = new SimpleEstimator(); GrowableOffsetRangeTracker tracker = new GrowableOffsetRangeTracker(10L, simpleEstimator); simpleEstimator.setEstimateRangeEnd(20); Progress currentProcess = tracker.getProgress(); assertEquals(0, currentProcess.getWorkCompleted(), 0.001); assertEquals(10, currentProcess.getWorkRemaining(), 0.001); simpleEstimator.setEstimateRangeEnd(15); currentProcess = tracker.getProgress(); assertEquals(0, currentProcess.getWorkCompleted(), 0.001); assertEquals(5, currentProcess.getWorkRemaining(), 0.001); simpleEstimator.setEstimateRangeEnd(5); currentProcess = tracker.getProgress(); assertEquals(0, currentProcess.getWorkCompleted(), 0.001); assertEquals(0, currentProcess.getWorkRemaining(), 0.001); }
@Test public void testProgressAfterFinished() throws Exception { SimpleEstimator simpleEstimator = new SimpleEstimator(); GrowableOffsetRangeTracker tracker = new GrowableOffsetRangeTracker(10L, simpleEstimator); assertFalse(tracker.tryClaim(Long.MAX_VALUE)); tracker.checkDone(); simpleEstimator.setEstimateRangeEnd(0L); Progress currentProgress = tracker.getProgress(); assertEquals(Long.MAX_VALUE - 10L, currentProgress.getWorkCompleted(), 0.001); assertEquals(0, currentProgress.getWorkRemaining(), 0.001); }
@Test public void testProgress() throws Exception { long start = 10L; SimpleEstimator simpleEstimator = new SimpleEstimator(); GrowableOffsetRangeTracker tracker = new GrowableOffsetRangeTracker(start, simpleEstimator); long cur = 20L; assertTrue(tracker.tryClaim(cur)); simpleEstimator.setEstimateRangeEnd(5L); Progress currentProgress = tracker.getProgress(); assertEquals(cur - start, currentProgress.getWorkCompleted(), 0.001); assertEquals(0, currentProgress.getWorkRemaining(), 0.001); simpleEstimator.setEstimateRangeEnd(35L); currentProgress = tracker.getProgress(); assertEquals(cur - start, currentProgress.getWorkCompleted(), 0.001); assertEquals(35L - cur, currentProgress.getWorkRemaining(), 0.001); simpleEstimator.setEstimateRangeEnd(25L); currentProgress = tracker.getProgress(); assertEquals(cur - start, currentProgress.getWorkCompleted(), 0.001); assertEquals(25L - cur, currentProgress.getWorkRemaining(), 0.001); simpleEstimator.setEstimateRangeEnd(Long.MAX_VALUE); currentProgress = tracker.getProgress(); assertEquals(cur - start, currentProgress.getWorkCompleted(), 0.001); assertEquals(Long.MAX_VALUE - cur, currentProgress.getWorkRemaining(), 0.001); }