Skip to content
Permalink
Browse files

Add threading test

  • Loading branch information
nyalldawson committed Jun 28, 2020
1 parent 5b35f5b commit 7b2dc6e5612e19d0c2907b3b4f169aac812d255e
Showing with 51 additions and 0 deletions.
  1. +51 −0 tests/src/core/testqgsruntimeprofiler.cpp
@@ -26,6 +26,7 @@ class TestQgsRuntimeProfiler: public QObject
void initTestCase();
void cleanupTestCase();
void testGroups();
void threading();

};

@@ -88,5 +89,55 @@ void TestQgsRuntimeProfiler::testGroups()
QCOMPARE( profiler.childGroups( QString(), QStringLiteral( "group 2" ) ), QStringList() << QStringLiteral( "task 2" ) );
}



class ProfileInThread : public QThread
{

public :
ProfileInThread( QgsRuntimeProfiler *mainProfiler )
: mMainProfiler( mainProfiler )
{}

void run() override
{
QgsScopedRuntimeProfile profile( QStringLiteral( "in thread" ), QStringLiteral( "bg" ) );
QVERIFY( mMainProfiler != QgsApplication::profiler() );
}

private:
QgsRuntimeProfiler *mMainProfiler = nullptr;


};

void TestQgsRuntimeProfiler::threading()
{
// test that profiling which occurs in a background thread is bubbled up to the main thread runtime profiler
QgsApplication::profiler()->clear();
QCOMPARE( QgsApplication::profiler()->rowCount(), 0 );

QThread *thread = new ProfileInThread( QgsApplication::profiler() );
{
QgsScopedRuntimeProfile profile( QStringLiteral( "launch thread" ), QStringLiteral( "main" ) );

QSignalSpy spy( QgsApplication::profiler(), &QgsRuntimeProfiler::groupAdded );
thread->start();
thread->exit();

spy.wait();
QCOMPARE( spy.count(), 1 );
QCOMPARE( spy.at( 0 ).at( 0 ).toString(), QStringLiteral( "bg" ) );
}

QCOMPARE( QgsApplication::profiler()->rowCount(), 2 );
int row1 = QgsApplication::profiler()->data( QgsApplication::profiler()->index( 0, 0 ) ).toString() == QLatin1String( "launch thread" ) ? 0 : 1;
QCOMPARE( QgsApplication::profiler()->data( QgsApplication::profiler()->index( row1, 0 ) ).toString(), QStringLiteral( "launch thread" ) );
QCOMPARE( QgsApplication::profiler()->data( QgsApplication::profiler()->index( row1, 0 ), QgsRuntimeProfilerNode::Group ).toString(), QStringLiteral( "main" ) );
QCOMPARE( QgsApplication::profiler()->data( QgsApplication::profiler()->index( row1 == 0 ? 1 : 0, 0 ) ).toString(), QStringLiteral( "in thread" ) );
QCOMPARE( QgsApplication::profiler()->data( QgsApplication::profiler()->index( row1 == 0 ? 1 : 0, 0 ), QgsRuntimeProfilerNode::Group ).toString(), QStringLiteral( "bg" ) );
}


QGSTEST_MAIN( TestQgsRuntimeProfiler )
#include "testqgsruntimeprofiler.moc"

0 comments on commit 7b2dc6e

Please sign in to comment.
You can’t perform that action at this time.