Skip to content

Commit

Permalink
Do not execute listeners when progress is updated to the end of the r…
Browse files Browse the repository at this point in the history
…ange
  • Loading branch information
tlrx committed Apr 30, 2024
1 parent a2d9cc6 commit e0e4444
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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<ActionListener<Long>> listenersToExecute = null;
synchronized (this) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down Expand Up @@ -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());
}
Expand Down

0 comments on commit e0e4444

Please sign in to comment.