Skip to content

Commit

Permalink
new test version
Browse files Browse the repository at this point in the history
  • Loading branch information
VitorVieiraZ committed Nov 21, 2024
1 parent eddef13 commit 4d5cd9c
Showing 1 changed file with 48 additions and 31 deletions.
79 changes: 48 additions & 31 deletions app/test/testmerginapi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<QNetworkReply::NetworkError> 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<QVariant> 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;
}
}

1 comment on commit 4d5cd9c

@inputapp-bot
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

iOS - version 24.11.695611 just submitted!

Please sign in to comment.