choke uploadProgress signals
authorShane Kearns <ext-shane.2.kearns@nokia.com>
Mon, 28 May 2012 15:27:58 +0000 (16:27 +0100)
committerQt by Nokia <qt-info@nokia.com>
Thu, 14 Jun 2012 03:26:37 +0000 (05:26 +0200)
The QNetworkReply::uploadProgress signal is intended for updating UI
elements such as a progress bar.
Limit the signal emissions to 10 per second to prevent overloading
the UI with updates.
As with the downloadProgress choke, this is implemented by dropping
signals that occur within 100ms of the previous emission.

The 100% signal is always emitted (bytesSent == bytesTotal)
When the upload size is initially unknown, this behaviour is still
provided by the upload device emitting a suitable readProgress
signal when EOF is reached.

Task-number: QTBUG-20449
Change-Id: I77e03c8a49109106e1c375ee00380293fd326b63
Reviewed-by: Martin Petersson <Martin.Petersson@nokia.com>
src/network/access/qnetworkreply_p.h
src/network/access/qnetworkreplyhttpimpl.cpp
src/network/access/qnetworkreplyimpl.cpp
tests/auto/network/access/qnetworkreply/tst_qnetworkreply.cpp

index 04598fc..4539023 100644 (file)
@@ -71,6 +71,7 @@ public:
     QPointer<QNetworkAccessManager> manager;
     qint64 readBufferMaxSize;
     QElapsedTimer downloadProgressSignalChoke;
+    QElapsedTimer uploadProgressSignalChoke;
     const static int progressSignalInterval;
     QNetworkAccessManager::Operation operation;
     QNetworkReply::NetworkError errorCode;
index cf92f55..e1ec9a9 100644 (file)
@@ -888,9 +888,9 @@ void QNetworkReplyHttpImplPrivate::postRequest()
     delegate->moveToThread(thread);
     // This call automatically moves the uploadDevice too for the asynchronous case.
 
-    // Start timer for progress notifications
+    // Prepare timers for progress notifications
     downloadProgressSignalChoke.start();
-
+    uploadProgressSignalChoke.invalidate();
 
     // Send an signal to the delegate so it starts working in the other thread
     if (synchronous) {
@@ -1802,6 +1802,17 @@ void QNetworkReplyHttpImplPrivate::emitReplyUploadProgress(qint64 bytesSent, qin
     Q_Q(QNetworkReplyHttpImpl);
     if (isFinished)
         return;
+
+    //choke signal emissions, except the first and last signals which are unconditional
+    if (uploadProgressSignalChoke.isValid()) {
+        if (bytesSent != bytesTotal && uploadProgressSignalChoke.elapsed() < progressSignalInterval) {
+            return;
+        }
+        uploadProgressSignalChoke.restart();
+    } else {
+        uploadProgressSignalChoke.start();
+    }
+
     emit q->uploadProgress(bytesSent, bytesTotal);
 }
 
index 9f8a6cf..d039347 100644 (file)
@@ -144,8 +144,9 @@ void QNetworkReplyImplPrivate::_q_startOperation()
     }
 #endif
 
-    // Start timer for progress notifications
+    // Prepare timer for progress notifications
     downloadProgressSignalChoke.start();
+    uploadProgressSignalChoke.invalidate();
 
     if (backend && backend->isSynchronous()) {
         state = Finished;
@@ -550,6 +551,17 @@ void QNetworkReplyImplPrivate::emitUploadProgress(qint64 bytesSent, qint64 bytes
 {
     Q_Q(QNetworkReplyImpl);
     bytesUploaded = bytesSent;
+
+    //choke signal emissions, except the first and last signals which are unconditional
+    if (uploadProgressSignalChoke.isValid()) {
+        if (bytesSent != bytesTotal && uploadProgressSignalChoke.elapsed() < progressSignalInterval) {
+            return;
+        }
+        uploadProgressSignalChoke.restart();
+    } else {
+        uploadProgressSignalChoke.start();
+    }
+
     pauseNotificationHandling();
     emit q->uploadProgress(bytesSent, bytesTotal);
     resumeNotificationHandling();
index bc2a95d..2226f99 100644 (file)
@@ -4528,7 +4528,8 @@ void tst_QNetworkReply::ioGetFromBuiltinHttp()
 
 void tst_QNetworkReply::ioPostToHttpUploadProgress()
 {
-    QFile sourceFile(testDataDir + "/bigfile");
+    //test file must be larger than OS socket buffers (~830kB on MacOS 10.6)
+    QFile sourceFile(testDataDir + "/image1.jpg");
     QVERIFY(sourceFile.open(QIODevice::ReadOnly));
 
     // emulate a minimal http server
@@ -4553,6 +4554,7 @@ void tst_QNetworkReply::ioPostToHttpUploadProgress()
     incomingSocket->setReadBufferSize(1*1024);
     QTestEventLoop::instance().enterLoop(5);
     // some progress should have been made
+    QVERIFY(!spy.isEmpty());
     QList<QVariant> args = spy.last();
     QVERIFY(!args.isEmpty());
     QVERIFY(args.at(0).toLongLong() > 0);
@@ -4563,6 +4565,7 @@ void tst_QNetworkReply::ioPostToHttpUploadProgress()
     incomingSocket->setReadBufferSize(0);
     QTestEventLoop::instance().enterLoop(10);
     // progress should be finished
+    QVERIFY(!spy.isEmpty());
     QList<QVariant> args3 = spy.last();
     QVERIFY(!args3.isEmpty());
     // More progress than before