Skip to content

Commit

Permalink
new test testDownloadWithNetworkErrorRecovery
Browse files Browse the repository at this point in the history
VitorVieiraZ committed Nov 22, 2024
1 parent 273c569 commit 95afeb6
Showing 2 changed files with 73 additions and 0 deletions.
72 changes: 72 additions & 0 deletions app/test/testmerginapi.cpp
Original file line number Diff line number Diff line change
@@ -3010,3 +3010,75 @@ void TestMerginApi::testDownloadWithNetworkError()
}
}

void TestMerginApi::testDownloadWithNetworkErrorRecovery()
{
// Store original manager
QNetworkAccessManager *originalManager = mApi->networkManager();

QString projectName = "testDownloadRetryRecovery";
createRemoteProject( mApiExtra, mWorkspaceName, projectName, mTestDataPath + "/" + TEST_PROJECT_NAME + "/" );

// Create mock manager - initially not failing
MockNetworkManager *failingManager = new MockNetworkManager( this );
mApi->setNetworkManager( failingManager );

// Create signal spies
QSignalSpy startSpy( mApi, &MerginApi::downloadItemsStarted );
QSignalSpy retrySpy( mApi, &MerginApi::downloadItemRetried );
QSignalSpy finishSpy( mApi, &MerginApi::syncProjectFinished );

// Counter to track retry attempts
int retryCount = 0;
QNetworkReply::NetworkError networkError = QNetworkReply::TimeoutError;

// Reset network after two retries
connect( mApi, &MerginApi::downloadItemRetried, this, [&retryCount, failingManager, this]()
{
retryCount++;
if ( retryCount == 2 )
{
failingManager->setShouldFail( false );
disconnect( mApi, &MerginApi::downloadItemsStarted, nullptr, nullptr );
disconnect( mApi, &MerginApi::downloadItemRetried, nullptr, nullptr );
}
} );

// Trigger network error when download starts
connect( mApi, &MerginApi::downloadItemsStarted, this, [failingManager, networkError]()
{
failingManager->setShouldFail( true, networkError );
} );

mApi->pullProject( mWorkspaceName, projectName );

// Verify a transaction was created
QCOMPARE( mApi->transactions().count(), 1 );

// Wait for download to start, retry twice, and then complete successfully
QVERIFY( startSpy.wait( TestUtils::LONG_REPLY ) );
QVERIFY( finishSpy.wait( TestUtils::LONG_REPLY ) );

// Verify signals were emitted
QVERIFY( startSpy.count() > 0 );
QCOMPARE( retrySpy.count(), 2 ); // Should have exactly 2 retries
QCOMPARE( finishSpy.count(), 1 );

// Verify sync succeeded
QList<QVariant> arguments = finishSpy.takeFirst();
QVERIFY( arguments.at( 1 ).toBool() );

// Verify local project was created successfully
LocalProject localProject = mApi->localProjectsManager().projectFromMerginName( mWorkspaceName, projectName );
QVERIFY( localProject.isValid() );

// Verify project files were downloaded correctly
QString projectDir = mApi->projectsPath() + "/" + projectName;
QStringList projectFiles = QDir( projectDir ).entryList( QDir::Files );
QVERIFY( projectFiles.count() > 0 );
QVERIFY( projectFiles.contains( "project.qgs" ) );

// Clean up
mApi->setNetworkManager( originalManager );
delete failingManager;
}

1 change: 1 addition & 0 deletions app/test/testmerginapi.h
Original file line number Diff line number Diff line change
@@ -108,6 +108,7 @@ class TestMerginApi: public QObject
void testListProjectsByName();
void testDownloadProject();
void testDownloadWithNetworkError();
void testDownloadWithNetworkErrorRecovery();
void testDownloadProjectSpecChars();
void testCancelDownloadProject();
void testCreateProjectTwice();

1 comment on commit 95afeb6

@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.695911 just submitted!

Please sign in to comment.