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 b42b4fb
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 34 deletions.
63 changes: 41 additions & 22 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 );
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<QVariant> 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<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;
}

33 changes: 21 additions & 12 deletions app/test/testmerginapi.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 {}
Expand All @@ -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;
};

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

0 comments on commit b42b4fb

Please sign in to comment.