Skip to content

Commit

Permalink
tests adjustments and fixing
Browse files Browse the repository at this point in the history
  • Loading branch information
VitorVieiraZ committed Nov 6, 2024
1 parent 9ed8bb4 commit 9e029b1
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 21 deletions.
59 changes: 39 additions & 20 deletions app/test/testmerginapi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
// 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 ) );
// Wait for individual downloads to start
QVERIFY( spyDownloadStarted.wait( TestUtils::SHORT_REPLY ) );
QCOMPARE( spyDownloadStarted.count(), 1 );

// Now simulate network failure
mockManager->setShouldFail( true );

// Wait for sync to finish
QSignalSpy syncFinishedSpy( mApi, &MerginApi::syncProjectFinished );
QVERIFY( syncFinishedSpy.wait( TestUtils::LONG_REPLY ) );
// Wait for retries
QVERIFY( spyRetried.wait( TestUtils::LONG_REPLY ) );
QVERIFY( spyRetried.count() >= 1 ); // Should have at least one retry

// Verify we got retry signals
QVERIFY( retrySpy.count() > 0 );
QList<QVariant> retryArgs = spyRetried.takeFirst();
QCOMPARE( retryArgs.at( 0 ).toString(), MerginApi::getFullProjectName( mWorkspaceName, projectName ) );
QVERIFY( retryArgs.at( 1 ).toInt() >= 1 );

// Cleanup
mApi->setNetworkManager( new QNetworkAccessManager( mApi ) );
deleteRemoteProjectNow( mApi, projectNamespace, projectName );
// 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<QVariant> 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;
}

11 changes: 10 additions & 1 deletion app/test/testmerginapi.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class MockReply : public QNetworkReply
public:
explicit MockReply( QObject *parent = nullptr ) : QNetworkReply( parent )
{
QNetworkReply::setError( QNetworkReply::UnknownNetworkError, "Mock network failure" );
QNetworkReply::setError( QNetworkReply::TimeoutError, "Mock network failure" );
QMetaObject::invokeMethod( this, "finished", Qt::QueuedConnection );
}

Expand All @@ -46,6 +46,15 @@ class MockNetworkManager : public QNetworkAccessManager
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 )
Expand Down
2 changes: 2 additions & 0 deletions core/merginapi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2441,6 +2441,8 @@ void MerginApi::startProjectPull( const QString &projectFullName )
}
else
{
emit downloadItemsStarted();

while ( transaction.replyPullItems.count() < 5 && !transaction.downloadQueue.isEmpty() )
{
downloadNextItem( projectFullName );
Expand Down
1 change: 1 addition & 0 deletions core/merginapi.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
Expand Down

1 comment on commit 9e029b1

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

Please sign in to comment.