summaryrefslogtreecommitdiffstats
path: root/tests/auto
diff options
context:
space:
mode:
authorSona Kurazyan <sona.kurazyan@qt.io>2020-10-16 12:25:13 +0200
committerSona Kurazyan <sona.kurazyan@qt.io>2020-10-26 14:27:02 +0100
commit4c793e6353ece51d4c04373f54e13d540b45195e (patch)
tree6aa9367b84ec9ace22dfe6e9a2cde4172ac3a379 /tests/auto
parent1aa3459d0a534473582806b0988d26533bc7e16a (diff)
Store std::exception_ptr in QUnhandledException
For historical reasons Qt Concurrent reports QUnhandledException in case if an exception that is not derived from QException is thrown from a worker thread. Changing this behavior may not be a good idea, since the existing user code may rely on it. Changed QUnhandledException to wrap the std::exception_ptr to the actual exception, so that the users can obtain the information about the thrown exception if needed. [ChangeLog][QtCore][QUnhandledException] Improved QUnhandledException to store the std::exception_ptr to the actual exception thrown from a QtCocnurrent worker thread. Change-Id: I30e7c1d3e01aff6e1ed9938c421da0a888f12066 Reviewed-by: Fabian Kosmale <fabian.kosmale@qt.io>
Diffstat (limited to 'tests/auto')
-rw-r--r--tests/auto/concurrent/qtconcurrentrun/tst_qtconcurrentrun.cpp20
-rw-r--r--tests/auto/concurrent/qtconcurrentthreadengine/tst_qtconcurrentthreadengine.cpp42
2 files changed, 50 insertions, 12 deletions
diff --git a/tests/auto/concurrent/qtconcurrentrun/tst_qtconcurrentrun.cpp b/tests/auto/concurrent/qtconcurrentrun/tst_qtconcurrentrun.cpp
index e4b728f950..ae1edb54a5 100644
--- a/tests/auto/concurrent/qtconcurrentrun/tst_qtconcurrentrun.cpp
+++ b/tests/auto/concurrent/qtconcurrentrun/tst_qtconcurrentrun.cpp
@@ -48,6 +48,7 @@ private slots:
void recursive();
#ifndef QT_NO_EXCEPTIONS
void exceptions();
+ void unhandledException();
#endif
void functor();
void lambda();
@@ -890,6 +891,25 @@ void tst_QtConcurrentRun::exceptions()
QVERIFY2(caught, "did not get exception");
}
+
+void tst_QtConcurrentRun::unhandledException()
+{
+ struct Exception {};
+ bool caught = false;
+ try {
+ auto f = QtConcurrent::run([] { throw Exception {}; });
+ f.waitForFinished();
+ } catch (const QUnhandledException &e) {
+ try {
+ if (e.exception())
+ std::rethrow_exception(e.exception());
+ } catch (const Exception &) {
+ caught = true;
+ }
+ }
+
+ QVERIFY(caught);
+}
#endif
// Compiler supports decltype
diff --git a/tests/auto/concurrent/qtconcurrentthreadengine/tst_qtconcurrentthreadengine.cpp b/tests/auto/concurrent/qtconcurrentthreadengine/tst_qtconcurrentthreadengine.cpp
index 6825be19e6..f5ddf4560e 100644
--- a/tests/auto/concurrent/qtconcurrentthreadengine/tst_qtconcurrentthreadengine.cpp
+++ b/tests/auto/concurrent/qtconcurrentthreadengine/tst_qtconcurrentthreadengine.cpp
@@ -430,10 +430,10 @@ public:
QThread *blockThread;
};
-class UnrelatedExceptionThrower : public ThreadEngine<void>
+class IntExceptionThrower : public ThreadEngine<void>
{
public:
- UnrelatedExceptionThrower(QThread *blockThread = nullptr)
+ IntExceptionThrower(QThread *blockThread = nullptr)
: ThreadEngine(QThreadPool::globalInstance())
{
this->blockThread = blockThread;
@@ -480,7 +480,7 @@ void tst_QtConcurrentThreadEngine::exceptions()
{
bool caught = false;
try {
- QtConcurrentExceptionThrower e(0);
+ QtConcurrentExceptionThrower e(nullptr);
e.startBlocking();
} catch (const QException &) {
caught = true;
@@ -492,11 +492,17 @@ void tst_QtConcurrentThreadEngine::exceptions()
{
bool caught = false;
try {
- UnrelatedExceptionThrower *e = new UnrelatedExceptionThrower();
+ IntExceptionThrower *e = new IntExceptionThrower();
QFuture<void> f = e->startAsynchronously();
f.waitForFinished();
- } catch (const QUnhandledException &) {
- caught = true;
+ } catch (const QUnhandledException &ex) {
+ // Make sure the exception info is not lost
+ try {
+ if (ex.exception())
+ std::rethrow_exception(ex.exception());
+ } catch (int) {
+ caught = true;
+ }
}
QVERIFY2(caught, "did not get exception");
}
@@ -506,10 +512,16 @@ void tst_QtConcurrentThreadEngine::exceptions()
{
bool caught = false;
try {
- UnrelatedExceptionThrower e(QThread::currentThread());
+ IntExceptionThrower e(QThread::currentThread());
e.startBlocking();
- } catch (const QUnhandledException &) {
- caught = true;
+ } catch (const QUnhandledException &ex) {
+ // Make sure the exception info is not lost
+ try {
+ if (ex.exception())
+ std::rethrow_exception(ex.exception());
+ } catch (int) {
+ caught = true;
+ }
}
QVERIFY2(caught, "did not get exception");
}
@@ -518,10 +530,16 @@ void tst_QtConcurrentThreadEngine::exceptions()
{
bool caught = false;
try {
- UnrelatedExceptionThrower e(0);
+ IntExceptionThrower e(nullptr);
e.startBlocking();
- } catch (const QUnhandledException &) {
- caught = true;
+ } catch (const QUnhandledException &ex) {
+ // Make sure the exception info is not lost
+ try {
+ if (ex.exception())
+ std::rethrow_exception(ex.exception());
+ } catch (int) {
+ caught = true;
+ }
}
QVERIFY2(caught, "did not get exception");
}