From 4d5cd9c05ef193b5df85eb8dd19d27eb7150172f Mon Sep 17 00:00:00 2001 From: VitorVieiraZ Date: Wed, 20 Nov 2024 23:13:50 -0300 Subject: [PATCH] new test version --- app/test/testmerginapi.cpp | 79 +++++++++++++++++++++++--------------- 1 file changed, 48 insertions(+), 31 deletions(-) diff --git a/app/test/testmerginapi.cpp b/app/test/testmerginapi.cpp index bc130331f..e75354d88 100644 --- a/app/test/testmerginapi.cpp +++ b/app/test/testmerginapi.cpp @@ -2951,45 +2951,62 @@ void TestMerginApi::testDownloadWithNetworkError() QString projectName = "testDownloadRetry"; createRemoteProject( mApiExtra, mWorkspaceName, projectName, mTestDataPath + "/" + TEST_PROJECT_NAME + "/" ); - // Create mock manager - initially not failing - MockNetworkManager *failingManager = new MockNetworkManager( this ); - mApi->setNetworkManager( failingManager ); - - QSignalSpy startSpy( mApi, &MerginApi::downloadItemsStarted ); - QSignalSpy retrySpy( mApi, &MerginApi::downloadItemRetried ); - QSignalSpy finishSpy( mApi, &MerginApi::syncProjectFinished ); + // Errors to test + QList errorsToTest = + { + QNetworkReply::TimeoutError, + QNetworkReply::NetworkSessionFailedError + }; - // First attempt - TimeoutError - connect( mApi, &MerginApi::downloadItemsStarted, this, [this, failingManager]() + foreach ( QNetworkReply::NetworkError networkError, errorsToTest ) { - failingManager->setShouldFail( true, QNetworkReply::TimeoutError ); - } ); + // Create mock manager - initially not failing + MockNetworkManager *failingManager = new MockNetworkManager( this ); + mApi->setNetworkManager( failingManager ); - mApi->pullProject( mWorkspaceName, projectName ); - QVERIFY( startSpy.wait( TestUtils::LONG_REPLY ) ); - QVERIFY( finishSpy.wait( TestUtils::LONG_REPLY ) ); + // Create signal spies + QSignalSpy startSpy( mApi, &MerginApi::downloadItemsStarted ); + QSignalSpy retrySpy( mApi, &MerginApi::downloadItemRetried ); + QSignalSpy finishSpy( mApi, &MerginApi::syncProjectFinished ); - disconnect( mApi, &MerginApi::downloadItemsStarted, this, nullptr ); + // Trigger the current network error when download starts + connect( mApi, &MerginApi::downloadItemsStarted, this, [this, failingManager, networkError]() + { + failingManager->setShouldFail( true, networkError ); + } ); - failingManager->setShouldFail( false ); + mApi->pullProject( mWorkspaceName, projectName ); - // Second attempt - TemporaryNetworkFailureError - connect( mApi, &MerginApi::downloadItemsStarted, this, [this, failingManager]() - { - failingManager->setShouldFail( true, QNetworkReply::TemporaryNetworkFailureError ); - } ); + // Verify a transaction was created + QCOMPARE( mApi->transactions().count(), 1 ); - mApi->pullProject( mWorkspaceName, projectName ); - QVERIFY( startSpy.wait( TestUtils::LONG_REPLY ) ); - QVERIFY( finishSpy.wait( TestUtils::LONG_REPLY ) ); + // Wait for download to start and then fail + QVERIFY( startSpy.wait( TestUtils::LONG_REPLY ) ); + QVERIFY( finishSpy.wait( TestUtils::LONG_REPLY ) ); - disconnect( mApi, &MerginApi::downloadItemsStarted, this, nullptr ); + // Verify signals were emitted + QVERIFY( startSpy.count() > 0 ); + QVERIFY( retrySpy.count() > 0 ); + QCOMPARE( finishSpy.count(), 1 ); - QVERIFY( startSpy.count() > 0 ); - QVERIFY( retrySpy.count() > 0 ); - QCOMPARE( finishSpy.count(), 3 ); + // Verify that MAX_RETRY_COUNT retry attempts were made + int maxRetries = TransactionStatus::MAX_RETRY_COUNT; + QCOMPARE( retrySpy.count(), maxRetries ); - // Restore original manager and clear old one - mApi->setNetworkManager( originalManager ); - delete failingManager; + // Verify sync failed + QList arguments = finishSpy.takeFirst(); + QVERIFY( !arguments.at( 1 ).toBool() ); + + // Verify no local project was created + LocalProject localProject = mApi->localProjectsManager().projectFromMerginName( mWorkspaceName, projectName ); + QVERIFY( !localProject.isValid() ); + + // Disconnect all signals + disconnect( mApi, &MerginApi::downloadItemsStarted, this, nullptr ); + + // Clean up + mApi->setNetworkManager( originalManager ); + delete failingManager; + } } +