From b42b4fbbe56ed64249b1988896b611043bcd6f13 Mon Sep 17 00:00:00 2001 From: VitorVieiraZ Date: Wed, 6 Nov 2024 14:54:54 -0300 Subject: [PATCH] tests adjustments and fixing --- app/test/testmerginapi.cpp | 63 +++++++++++++++++++++++++------------- app/test/testmerginapi.h | 33 ++++++++++++-------- core/merginapi.cpp | 2 ++ core/merginapi.h | 1 + 4 files changed, 65 insertions(+), 34 deletions(-) diff --git a/app/test/testmerginapi.cpp b/app/test/testmerginapi.cpp index 1ba8ff113..db21737c9 100644 --- a/app/test/testmerginapi.cpp +++ b/app/test/testmerginapi.cpp @@ -2945,34 +2945,53 @@ void TestMerginApi::testParseVersion() void TestMerginApi::testDownloadWithNetworkError() { - QString projectName = "testDownloadWithNetworkError"; - QString projectNamespace = mWorkspaceName; + // Store original network manager to restore it later + QNetworkAccessManager *originalManager = mApi->networkManager(); - // Create a test project on server - createRemoteProject( mApiExtra, projectNamespace, projectName, mTestDataPath + "/" + TEST_PROJECT_NAME + "/" ); + // create a project and do initial setup + QString projectName = "testDownloadWithNetworkError"; + createRemoteProject( mApiExtra, mWorkspaceName, projectName, mTestDataPath + "/" + TEST_PROJECT_NAME + "/" ); - // Create mock network manager - MockNetworkManager *mockManager = new MockNetworkManager( mApi ); - mApi->setNetworkManager( mockManager ); + // Set up mock network manager that will fail after first chunk + MockNetworkManager *mockManager = new MockNetworkManager(); + mApi->setNetworkManager( mockManager ); - // Track retry signals - QSignalSpy retrySpy( mApi, &MerginApi::downloadItemRetried ); + QSignalSpy spyDownloadStarted( mApi, &MerginApi::downloadItemsStarted ); + QSignalSpy spyRetried( mApi, &MerginApi::downloadItemRetried ); + QSignalSpy spyFinished( mApi, &MerginApi::syncProjectFinished ); - // Start download and make network fail after project info - mApi->pullProject( projectNamespace, projectName ); + // Start download + mApi->pullProject( mWorkspaceName, projectName ); - QSignalSpy pullStartedSpy( mApi, &MerginApi::pullFilesStarted ); - QVERIFY( pullStartedSpy.wait( TestUtils::SHORT_REPLY ) ); - mockManager->setShouldFail( true ); + // Wait for individual downloads to start + QVERIFY( spyDownloadStarted.wait( TestUtils::SHORT_REPLY ) ); + QCOMPARE( spyDownloadStarted.count(), 1 ); - // Wait for sync to finish - QSignalSpy syncFinishedSpy( mApi, &MerginApi::syncProjectFinished ); - QVERIFY( syncFinishedSpy.wait( TestUtils::LONG_REPLY ) ); + // Now simulate network failure + mockManager->setShouldFail( true ); - // Verify we got retry signals - QVERIFY( retrySpy.count() > 0 ); + // Wait for retries + QVERIFY( spyRetried.wait( TestUtils::LONG_REPLY ) ); + QVERIFY( spyRetried.count() >= 1 ); // Should have at least one retry - // Cleanup - mApi->setNetworkManager( new QNetworkAccessManager( mApi ) ); - deleteRemoteProjectNow( mApi, projectNamespace, projectName ); + QList retryArgs = spyRetried.takeFirst(); + QCOMPARE( retryArgs.at( 0 ).toString(), MerginApi::getFullProjectName( mWorkspaceName, projectName ) ); + QVERIFY( retryArgs.at( 1 ).toInt() >= 1 ); + + // Restore normal network behavior to let download finish + mockManager->setShouldFail( false ); + + // Wait for download to finish + QVERIFY( spyFinished.wait( TestUtils::LONG_REPLY ) ); + QCOMPARE( spyFinished.count(), 1 ); + + QList finishArgs = spyFinished.takeFirst(); + QCOMPARE( finishArgs.at( 0 ).toString(), MerginApi::getFullProjectName( mWorkspaceName, projectName ) ); + QVERIFY( finishArgs.at( 1 ).toBool() ); // Should finish successfully + + // Clean up and restore original network manager + deleteLocalProject( mApi, mWorkspaceName, projectName ); + mApi->setNetworkManager( originalManager ); + delete mockManager; } + diff --git a/app/test/testmerginapi.h b/app/test/testmerginapi.h index 1f3ea30db..883ec78a2 100644 --- a/app/test/testmerginapi.h +++ b/app/test/testmerginapi.h @@ -23,11 +23,11 @@ class MockReply : public QNetworkReply { - public: +public: explicit MockReply( QObject *parent = nullptr ) : QNetworkReply( parent ) { - QNetworkReply::setError( QNetworkReply::UnknownNetworkError, "Mock network failure" ); - QMetaObject::invokeMethod( this, "finished", Qt::QueuedConnection ); + QNetworkReply::setError( QNetworkReply::TimeoutError, "Mock network failure" ); + QMetaObject::invokeMethod( this, "finished", Qt::QueuedConnection ); } void abort() override {} @@ -37,25 +37,34 @@ class MockReply : public QNetworkReply class MockNetworkManager : public QNetworkAccessManager { - public: +public: explicit MockNetworkManager( QObject *parent = nullptr ) - : QNetworkAccessManager( parent ) - , mShouldFail( false ) + : QNetworkAccessManager( parent ) + , mShouldFail( false ) {} void setShouldFail( bool shouldFail ) { mShouldFail = shouldFail; } bool shouldFail() const { return mShouldFail; } + QNetworkReply *get( const QNetworkRequest &request ) + { + if ( mShouldFail ) + { + return new MockReply( this ); + } + return QNetworkAccessManager::get( request ); + } + QNetworkReply *post( const QNetworkRequest &request, const QByteArray &data ) { - if ( mShouldFail ) - { - return new MockReply( this ); - } - return QNetworkAccessManager::post( request, data ); + if ( mShouldFail ) + { + return new MockReply( this ); + } + return QNetworkAccessManager::post( request, data ); } - private: +private: bool mShouldFail; }; diff --git a/core/merginapi.cpp b/core/merginapi.cpp index 4b0822840..398407d6f 100644 --- a/core/merginapi.cpp +++ b/core/merginapi.cpp @@ -2441,6 +2441,8 @@ void MerginApi::startProjectPull( const QString &projectFullName ) } else { + emit downloadItemsStarted(); + while ( transaction.replyPullItems.count() < 5 && !transaction.downloadQueue.isEmpty() ) { downloadNextItem( projectFullName ); diff --git a/core/merginapi.h b/core/merginapi.h index b415bebff..6089a7fd3 100644 --- a/core/merginapi.h +++ b/core/merginapi.h @@ -668,6 +668,7 @@ class MerginApi: public QObject void networkManagerChanged(); void downloadItemRetried( const QString &projectFullName, int retryCount ); + void downloadItemsStarted(); private slots: void listProjectsReplyFinished( QString requestId );