diff options
author | Christian Ehrlicher <ch.ehrlicher@gmx.de> | 2023-02-12 20:09:16 +0100 |
---|---|---|
committer | Christian Ehrlicher <ch.ehrlicher@gmx.de> | 2024-04-25 08:27:21 +0200 |
commit | b4c63b89dfe136d0579bf1b6422c4d878cdd74ab (patch) | |
tree | 6b45d551222abfe9d742674fd13c9d4903d8c8c4 /tests | |
parent | 46ad7fe966f6c191ff2123e95c3b6373f7b24287 (diff) |
QSqlDatabase: add moveToThread()/currentThread()
Add QSqlDatabase::moveToThread() to be able to move the driver instance
to another thread.
[ChangeLog][Sql][QSqLDatabase] QSqlDatabase gained two new functions
moveToThread() and currentThread() to be able to use it in another
thread than the one it was created in.
Fixes: QTBUG-39957
Change-Id: I9cb51358f73a3a2fa72813bfdbe059279d388bd7
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
Diffstat (limited to 'tests')
-rw-r--r-- | tests/auto/sql/kernel/qsqldatabase/tst_qsqldatabase.cpp | 29 |
1 files changed, 29 insertions, 0 deletions
diff --git a/tests/auto/sql/kernel/qsqldatabase/tst_qsqldatabase.cpp b/tests/auto/sql/kernel/qsqldatabase/tst_qsqldatabase.cpp index 1b762abc68..19afacf6f9 100644 --- a/tests/auto/sql/kernel/qsqldatabase/tst_qsqldatabase.cpp +++ b/tests/auto/sql/kernel/qsqldatabase/tst_qsqldatabase.cpp @@ -104,6 +104,8 @@ private slots: void infinityAndNan(); void multipleThreads_data() { generic_data(); } void multipleThreads(); + void moveToThread_data() { generic_data(); } + void moveToThread(); void db2_valueCacheUpdate_data() { generic_data("QDB2"); } void db2_valueCacheUpdate(); @@ -2335,5 +2337,32 @@ void tst_QSqlDatabase::multipleThreads() QTRY_VERIFY(t.isFinished()); } +void tst_QSqlDatabase::moveToThread() +{ + QFETCH(QString, dbName); + QSqlDatabase db = QSqlDatabase::database(dbName); + auto clonedDb = QSqlDatabase::cloneDatabase(db, "clonedDb"); + auto mainThread = QThread::currentThread(); + CHECK_DATABASE(db); + QCOMPARE(db.currentThread(), mainThread); + QCOMPARE(clonedDb.currentThread(), mainThread); + std::unique_ptr<QThread> t(QThread::create([&] { + db.moveToThread(mainThread); + QThread::currentThread()->exit(); + })); + db.moveToThread(t.get()); + QCOMPARE(db.currentThread(), t.get()); + QCOMPARE(clonedDb.currentThread(), mainThread); + t->start(); + QTRY_VERIFY(t->isRunning()); + QTRY_VERIFY(t->wait(30000)); + QCOMPARE(db.currentThread(), mainThread); + QCOMPARE(clonedDb.currentThread(), mainThread); + db = QSqlDatabase(); + clonedDb = QSqlDatabase(); + QSqlDatabase::removeDatabase("clonedDb"); +} + + QTEST_MAIN(tst_QSqlDatabase) #include "tst_qsqldatabase.moc" |