From cd816d4b6ac4358a92dbda906288ba6d969fc1cd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?M=C3=A5rten=20Nordheim?= Date: Fri, 26 Apr 2019 12:56:23 +0200 Subject: Add setAutoDeleteReplies to QNetworkAccessManager Following the introduction of AutoDeleteReplyOnFinishAttribute to QNetworkRequest it seems natural to make it easy to enable for all replies created with the current QNetworkAccessManager. [ChangeLog][QtNetwork][QNetworkAccessManager] Added setAutoDeleteReplies to QNetworkAccessManager to enable the AutoDeleteReplyOnFinishAttribute attribute for all QNetworkRequests that are passed to QNetworkAccessManager. Change-Id: I7f96dd1fc9a899328e89732be17780b4e710c2a2 Reviewed-by: Edward Welbourne Reviewed-by: Markus Goetz (Woboq GmbH) Reviewed-by: Timur Pocheptsov --- .../access/qnetworkreply/tst_qnetworkreply.cpp | 91 ++++++++++++++++++++++ 1 file changed, 91 insertions(+) (limited to 'tests/auto/network/access/qnetworkreply/tst_qnetworkreply.cpp') diff --git a/tests/auto/network/access/qnetworkreply/tst_qnetworkreply.cpp b/tests/auto/network/access/qnetworkreply/tst_qnetworkreply.cpp index 5e3018366c..43739be4a3 100644 --- a/tests/auto/network/access/qnetworkreply/tst_qnetworkreply.cpp +++ b/tests/auto/network/access/qnetworkreply/tst_qnetworkreply.cpp @@ -505,6 +505,8 @@ private Q_SLOTS: void autoDeleteRepliesAttribute_data(); void autoDeleteRepliesAttribute(); + void autoDeleteReplies_data(); + void autoDeleteReplies(); // NOTE: This test must be last! void parentingRepliesToTheApp(); @@ -9241,6 +9243,95 @@ void tst_QNetworkReply::autoDeleteRepliesAttribute() } } +void tst_QNetworkReply::autoDeleteReplies_data() +{ + autoDeleteRepliesAttribute_data(); +} + +void tst_QNetworkReply::autoDeleteReplies() +{ + QFETCH(QUrl, destination); + manager.setAutoDeleteReplies(true); + auto cleanup = qScopeGuard([this] { manager.setAutoDeleteReplies(false); }); + { + // Get + QNetworkRequest request(destination); + QNetworkReply *reply = manager.get(request); + QSignalSpy finishedSpy(reply, &QNetworkReply::finished); + QSignalSpy destroyedSpy(reply, &QObject::destroyed); + QVERIFY(finishedSpy.wait()); + QCOMPARE(destroyedSpy.count(), 0); + QVERIFY(destroyedSpy.wait()); + } + { + // Post + QNetworkRequest request(destination); + QNetworkReply *reply = manager.post(request, QByteArrayLiteral("datastring")); + QSignalSpy finishedSpy(reply, &QNetworkReply::finished); + QSignalSpy destroyedSpy(reply, &QObject::destroyed); + QVERIFY(finishedSpy.wait()); + QCOMPARE(destroyedSpy.count(), 0); + QVERIFY(destroyedSpy.wait()); + } + // Here we repeat the test, but override the auto-deletion in the QNetworkRequest + // We need two calls to processEvents to test that the QNetworkReply doesn't get deleted. + // The first call executes a metacall event which adds the deleteLater meta event which + // would be executed in the second call. But that shouldn't happen in this case. + { + // Get + QNetworkRequest request(destination); + request.setAttribute(QNetworkRequest::AutoDeleteReplyOnFinishAttribute, false); + QScopedPointer reply(manager.get(request)); + QSignalSpy finishedSpy(reply.data(), &QNetworkReply::finished); + QSignalSpy destroyedSpy(reply.data(), &QObject::destroyed); + QVERIFY(finishedSpy.wait()); + QCOMPARE(destroyedSpy.count(), 0); + QCoreApplication::processEvents(); + QCoreApplication::processEvents(); + QCOMPARE(destroyedSpy.count(), 0); + } + { + // Post + QNetworkRequest request(destination); + request.setAttribute(QNetworkRequest::AutoDeleteReplyOnFinishAttribute, false); + QScopedPointer reply(manager.post(request, QByteArrayLiteral("datastring"))); + QSignalSpy finishedSpy(reply.data(), &QNetworkReply::finished); + QSignalSpy destroyedSpy(reply.data(), &QObject::destroyed); + QVERIFY(finishedSpy.wait()); + QCOMPARE(destroyedSpy.count(), 0); + QCoreApplication::processEvents(); + QCoreApplication::processEvents(); + QCOMPARE(destroyedSpy.count(), 0); + } + // Now we repeat the test with autoDeleteReplies set to false + cleanup.dismiss(); + manager.setAutoDeleteReplies(false); + { + // Get + QNetworkRequest request(destination); + QScopedPointer reply(manager.get(request)); + QSignalSpy finishedSpy(reply.data(), &QNetworkReply::finished); + QSignalSpy destroyedSpy(reply.data(), &QObject::destroyed); + QVERIFY(finishedSpy.wait()); + QCOMPARE(destroyedSpy.count(), 0); + QCoreApplication::processEvents(); + QCoreApplication::processEvents(); + QCOMPARE(destroyedSpy.count(), 0); + } + { + // Post + QNetworkRequest request(destination); + QScopedPointer reply(manager.post(request, QByteArrayLiteral("datastring"))); + QSignalSpy finishedSpy(reply.data(), &QNetworkReply::finished); + QSignalSpy destroyedSpy(reply.data(), &QObject::destroyed); + QVERIFY(finishedSpy.wait()); + QCOMPARE(destroyedSpy.count(), 0); + QCoreApplication::processEvents(); + QCoreApplication::processEvents(); + QCOMPARE(destroyedSpy.count(), 0); + } +} + // NOTE: This test must be last testcase in tst_qnetworkreply! void tst_QNetworkReply::parentingRepliesToTheApp() { -- cgit v1.2.3