Skip to content

Commit 6993a49

Browse files
slandelleStephane Landelle
authored andcommitted
ChannelProgressiveFutureListener.operationProgressed should be working this way
1 parent 8abdd5e commit 6993a49

File tree

2 files changed

+9
-7
lines changed

2 files changed

+9
-7
lines changed

api/src/test/java/org/asynchttpclient/async/TransferListenerTest.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -149,9 +149,8 @@ public void basicPutTest() throws Throwable {
149149

150150
final AtomicBoolean completed = new AtomicBoolean(false);
151151

152-
byte[] bytes = "RatherLargeFileRatherLargeFileRatherLargeFileRatherLargeFile".getBytes("UTF-16");
153-
long repeats = (1024 * 100 * 10 / bytes.length) + 1;
154-
File file = createTempFile(bytes, (int) repeats);
152+
long repeats = (1024 * 100 * 10 / PATTERN_BYTES.length) + 1;
153+
File file = createTempFile(PATTERN_BYTES, (int) repeats);
155154
long expectedFileSize = PATTERN_BYTES.length * repeats;
156155
Assert.assertEquals(expectedFileSize, file.length(), "Invalid file length");
157156

providers/netty4/src/main/java/org/asynchttpclient/providers/netty4/ProgressListener.java

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import io.netty.channel.ChannelProgressiveFutureListener;
55

66
import java.nio.channels.ClosedChannelException;
7+
import java.util.concurrent.atomic.AtomicLong;
78

89
import org.asynchttpclient.AsyncHandler;
910
import org.asynchttpclient.AsyncHttpClientConfig;
@@ -16,6 +17,7 @@ public class ProgressListener implements ChannelProgressiveFutureListener {
1617
private final boolean notifyHeaders;
1718
private final AsyncHandler<?> asyncHandler;
1819
private final NettyResponseFuture<?> future;
20+
private final AtomicLong lastProgress = new AtomicLong(0);
1921

2022
public ProgressListener(AsyncHttpClientConfig config, boolean notifyHeaders, AsyncHandler<?> asyncHandler, NettyResponseFuture<?> future) {
2123
this.config = config;
@@ -61,13 +63,13 @@ public void operationComplete(ChannelProgressiveFuture cf) {
6163
future.touch();
6264

6365
/**
64-
* We need to make sure we aren't in the middle of an authorization process before publishing events as we will re-publish again the same event after the authorization, causing unpredictable behavior.
66+
* We need to make sure we aren't in the middle of an authorization process before publishing events as we will re-publish again the same event after the authorization,
67+
* causing unpredictable behavior.
6568
*/
6669
Realm realm = future.getRequest().getRealm() != null ? future.getRequest().getRealm() : config.getRealm();
6770
boolean startPublishing = future.isInAuth() || realm == null || realm.getUsePreemptiveAuth();
6871

6972
if (startPublishing && asyncHandler instanceof ProgressAsyncHandler) {
70-
// FIXME WTF
7173
if (notifyHeaders) {
7274
ProgressAsyncHandler.class.cast(asyncHandler).onHeaderWriteCompleted();
7375
} else {
@@ -80,7 +82,8 @@ public void operationComplete(ChannelProgressiveFuture cf) {
8082
public void operationProgressed(ChannelProgressiveFuture f, long progress, long total) {
8183
future.touch();
8284
if (asyncHandler instanceof ProgressAsyncHandler) {
83-
ProgressAsyncHandler.class.cast(asyncHandler).onContentWriteProgress(total - progress, progress, total);
85+
long lastProgressValue = lastProgress.getAndSet(progress);
86+
ProgressAsyncHandler.class.cast(asyncHandler).onContentWriteProgress(progress - lastProgressValue, progress, total);
8487
}
8588
}
86-
}
89+
}

0 commit comments

Comments
 (0)