diff --git a/x-pack/plugin/blob-cache/src/main/java/org/elasticsearch/blobcache/common/ProgressListenableActionFuture.java b/x-pack/plugin/blob-cache/src/main/java/org/elasticsearch/blobcache/common/ProgressListenableActionFuture.java index 0912da200735e..472a208f684d3 100644 --- a/x-pack/plugin/blob-cache/src/main/java/org/elasticsearch/blobcache/common/ProgressListenableActionFuture.java +++ b/x-pack/plugin/blob-cache/src/main/java/org/elasticsearch/blobcache/common/ProgressListenableActionFuture.java @@ -78,6 +78,9 @@ public void onProgress(final long progressValue) { assert false : end + " < " + progressValue; throw new IllegalArgumentException("Cannot update progress with a value greater than [end=" + end + ']'); } + if (progressValue == end) { + return; // reached the end of the range, listeners will be completed by {@link #onResponse(Long)} + } List> listenersToExecute = null; synchronized (this) { diff --git a/x-pack/plugin/blob-cache/src/test/java/org/elasticsearch/blobcache/common/SparseFileTrackerTests.java b/x-pack/plugin/blob-cache/src/test/java/org/elasticsearch/blobcache/common/SparseFileTrackerTests.java index 41b323b769a93..6f1635e67ca4b 100644 --- a/x-pack/plugin/blob-cache/src/test/java/org/elasticsearch/blobcache/common/SparseFileTrackerTests.java +++ b/x-pack/plugin/blob-cache/src/test/java/org/elasticsearch/blobcache/common/SparseFileTrackerTests.java @@ -119,7 +119,7 @@ public void testInvalidRange() { } } - public void testCallsListenerWhenWholeRangeIsAvailable() { + public void testCallsListenerWhenRangeIsCompleted() { final byte[] fileContents = new byte[between(0, 1000)]; final SparseFileTracker sparseFileTracker = new SparseFileTracker("test", fileContents.length); @@ -155,21 +155,19 @@ public void testCallsListenerWhenWholeRangeIsAvailable() { final SparseFileTracker.Gap gap = gaps.get(gapIndex); assertThat(gap.start(), greaterThanOrEqualTo(start)); assertThat(gap.end(), lessThanOrEqualTo(end)); - // listener is notified when the last gap is completed - final AtomicBoolean shouldNotifyListener = new AtomicBoolean(); for (long i = gap.start(); i < gap.end(); i++) { assertThat(fileContents[toIntBytes(i)], equalTo(UNAVAILABLE)); fileContents[toIntBytes(i)] = AVAILABLE; - // listener is notified when the progress reached the last byte of the last gap - if ((gapIndex == gaps.size() - 1) && (i == gap.end() - 1L)) { - assertTrue(shouldNotifyListener.compareAndSet(false, true)); - expectNotification.set(true); - } gap.onProgress(i + 1L); - assertThat(wasNotified.get(), equalTo(shouldNotifyListener.get())); + assertThat(wasNotified.get(), equalTo(false)); + } + // listener is notified when the last gap is completed + if (gapIndex == gaps.size() - 1) { + expectNotification.set(true); } - assertThat(wasNotified.get(), equalTo(shouldNotifyListener.get())); + assertThat(wasNotified.get(), equalTo(false)); gap.onCompletion(); + assertThat(wasNotified.get(), equalTo(expectNotification.get())); } assertTrue(wasNotified.get()); }