1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510
|
/****************************************************************************
**
** Copyright (C) 2016 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of the test suite of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:GPL-EXCEPT$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3 as published by the Free Software
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
**
** $QT_END_LICENSE$
**
****************************************************************************/
#include <QtTest/QtTest>
#include "../qsqldatabase/tst_databases.h"
#include <QtCore>
#include <QtSql>
#include "qdebug.h"
const QString qtest(qTableName("qtest", __FILE__, QSqlDatabase()));
// set this define if Oracle is built with threading support
//#define QOCI_THREADED
class tst_QSqlThread : public QObject
{
Q_OBJECT
public:
tst_QSqlThread();
virtual ~tst_QSqlThread();
void dropTestTables();
void createTestTables();
void recreateTestTables();
void repopulateTestTables();
void generic_data(const QString &engine=QString());
tst_Databases dbs;
public slots:
void initTestCase();
void cleanupTestCase();
void init();
void cleanup();
protected slots:
void threadFinished() {
++threadFinishedCount;
qDebug("Thread finished, total finished: %d", threadFinishedCount);
}
private slots:
void simpleThreading_data() { generic_data(); }
void simpleThreading();
void readWriteThreading_data() { generic_data(); }
void readWriteThreading();
void readFromSingleConnection_data() { generic_data(); }
void readFromSingleConnection();
void readWriteFromSingleConnection_data() { generic_data(); }
void readWriteFromSingleConnection();
void preparedReadWriteFromSingleConnection_data() { generic_data(); }
void preparedReadWriteFromSingleConnection();
void transactionsFromSingleConnection_data() { generic_data(); }
void transactionsFromSingleConnection();
private:
int threadFinishedCount;
};
static QAtomicInt counter;
class QtTestSqlThread : public QThread
{
Q_OBJECT
public:
QtTestSqlThread(const QSqlDatabase &aDb, QObject *parent = 0)
: QThread(parent), sourceDb(aDb) {}
void runHelper(const QString &dbName)
{
QSqlDatabase db = QSqlDatabase::cloneDatabase(sourceDb, dbName);
QVERIFY_SQL(db, open());
int sum = 0;
QSqlQuery q("select id from " + qtest, db);
QVERIFY_SQL(q, isActive());
while (q.next())
sum += q.value(0).toInt();
QCOMPARE(sum, 6);
q.clear();
}
void run()
{
QString dbName = QString("QThreadDb%1").arg((size_t)currentThreadId());
runHelper(dbName);
QSqlDatabase::database(dbName).close();
QSqlDatabase::removeDatabase(dbName);
}
private:
QSqlDatabase sourceDb;
};
enum { ProdConIterations = 10 };
class SqlProducer: public QThread
{
Q_OBJECT
public:
SqlProducer(const QSqlDatabase &aDb, QObject *parent = 0)
: QThread(parent), sourceDb(aDb) {}
void runHelper(const QString &dbName)
{
QSqlDatabase db = QSqlDatabase::cloneDatabase(sourceDb, dbName);
QVERIFY_SQL(db, open());
QSqlQuery q(db);
QVERIFY_SQL(q, prepare("insert into " + qtest + " values (?, ?, ?)"));
int id = 10;
for (int i = 0; i < ProdConIterations; ++i) {
q.bindValue(0, ++id);
q.bindValue(1, "threaddy");
q.bindValue(2, 10);
QVERIFY_SQL(q, exec());
QThread::yieldCurrentThread();
}
}
void run()
{
QString dbName = QString("Producer%1").arg((size_t)currentThreadId());
runHelper(dbName);
QSqlDatabase::database(dbName).close();
QSqlDatabase::removeDatabase(dbName);
}
private:
QSqlDatabase sourceDb;
};
class SqlConsumer: public QThread
{
Q_OBJECT
public:
SqlConsumer(const QSqlDatabase &aDb, QObject *parent = 0)
: QThread(parent), sourceDb(aDb) {}
void runHelper(const QString &dbName)
{
QSqlDatabase db = QSqlDatabase::cloneDatabase(sourceDb, dbName);
QVERIFY_SQL(db, open());
QSqlQuery q1(db), q2(db);
QVERIFY_SQL(q2, prepare("delete from " + qtest + " where id = :id"));
for (int i = 0; i < ProdConIterations; ++i) {
QVERIFY_SQL(q1, exec("select max(id) from " + qtest));
q1.first();
q2.bindValue(":id", q1.value(0));
q1.clear();
QVERIFY_SQL(q2, exec());
QThread::yieldCurrentThread();
}
}
void run()
{
QString dbName = QString("Consumer%1").arg((size_t)currentThreadId());
runHelper(dbName);
QSqlDatabase::database(dbName).close();
QSqlDatabase::removeDatabase(dbName);
}
private:
QSqlDatabase sourceDb;
};
class SqlThread: public QThread
{
Q_OBJECT
public:
enum Mode { SimpleReading, PreparedReading, SimpleWriting, PreparedWriting };
SqlThread(Mode m, const QSqlDatabase &db, QObject *parent = 0)
: QThread(parent), sourceDb(db), mode(m) {}
void run()
{
QSqlDatabase &db = sourceDb;
switch (mode) {
case SimpleReading: {
// Executes a Query for reading, iterates over the first 4 results
QSqlQuery q(sourceDb);
for (int j = 0; j < ProdConIterations; ++j) {
QVERIFY_SQL(q, exec("select id,name from " + qtest + " order by id"));
for (int i = 1; i < 4; ++i) {
QVERIFY_SQL(q, next());
QCOMPARE(q.value(0).toInt(), i);
}
}
break; }
case SimpleWriting: {
// Executes a query for writing (appends a new row)
QSqlQuery q(sourceDb);
for (int j = 0; j < ProdConIterations; ++j) {
QVERIFY_SQL(q, exec(QString("insert into " + qtest
+ " (id, name) values(%1, '%2')")
.arg(counter.fetchAndAddRelaxed(1)).arg("Robert")));
}
break; }
case PreparedReading: {
// Prepares a query for reading and iterates over the results
QSqlQuery q(sourceDb);
QVERIFY_SQL(q, prepare("select id, name from " + qtest + " where id = ?"));
for (int j = 0; j < ProdConIterations; ++j) {
q.addBindValue(j % 3 + 1);
QVERIFY_SQL(q, exec());
QVERIFY_SQL(q, next());
QCOMPARE(q.value(0).toInt(), j % 3 + 1);
}
break; }
case PreparedWriting: {
QSqlQuery q(sourceDb);
QVERIFY_SQL(q, prepare("insert into " + qtest + " (id, name) "
"values(?, ?)"));
for (int i = 0; i < ProdConIterations; ++i) {
q.addBindValue(counter.fetchAndAddRelaxed(1));
q.addBindValue("Robert");
QVERIFY_SQL(q, exec());
}
break; }
}
}
private:
QSqlDatabase sourceDb;
Mode mode;
};
tst_QSqlThread::tst_QSqlThread()
: threadFinishedCount(0)
{
}
tst_QSqlThread::~tst_QSqlThread()
{
}
void tst_QSqlThread::generic_data(const QString& engine)
{
if ( dbs.fillTestTable(engine) == 0 ) {
if(engine.isEmpty())
QSKIP( "No database drivers are available in this Qt configuration");
else
QSKIP( (QString("No database drivers of type %1 are available in this Qt configuration").arg(engine)).toLocal8Bit());
}
}
void tst_QSqlThread::dropTestTables()
{
for (int i = 0; i < dbs.dbNames.count(); ++i) {
QSqlDatabase db = QSqlDatabase::database(dbs.dbNames.at(i));
QSqlQuery q(db);
tst_Databases::safeDropTables(db, QStringList() << qtest << qTableName("qtest2", __FILE__, db) << qTableName("emptytable", __FILE__, db));
}
}
void tst_QSqlThread::createTestTables()
{
for (int i = 0; i < dbs.dbNames.count(); ++i) {
QSqlDatabase db = QSqlDatabase::database(dbs.dbNames.at(i));
QSqlQuery q(db);
QVERIFY_SQL(q, exec("create table " + qtest
+ "(id int NOT NULL primary key, name varchar(20), title int)"));
QVERIFY_SQL(q, exec("create table " + qTableName("qtest2", __FILE__, db)
+ "(id int NOT NULL primary key, title varchar(20))"));
QVERIFY_SQL(q, exec("create table " + qTableName("emptytable", __FILE__, db)
+ "(id int NOT NULL primary key)"));
}
}
void tst_QSqlThread::repopulateTestTables()
{
for (int i = 0; i < dbs.dbNames.count(); ++i) {
QSqlDatabase db = QSqlDatabase::database(dbs.dbNames.at(i));
QSqlQuery q(db);
QVERIFY_SQL(q, exec("delete from " + qtest));
QVERIFY_SQL(q, exec("insert into " + qtest + " values(1, 'harry', 1)"));
QVERIFY_SQL(q, exec("insert into " + qtest + " values(2, 'trond', 2)"));
QVERIFY_SQL(q, exec("insert into " + qtest + " values(3, 'vohi', 3)"));
QVERIFY_SQL(q, exec("delete from " + qTableName("qtest2", __FILE__, db)));
QVERIFY_SQL(q, exec("insert into " + qTableName("qtest2", __FILE__, db) + " values(1, 'herr')"));
QVERIFY_SQL(q, exec("insert into " + qTableName("qtest2", __FILE__, db) + " values(2, 'mister')"));
}
}
void tst_QSqlThread::recreateTestTables()
{
dropTestTables();
createTestTables();
repopulateTestTables();
}
void tst_QSqlThread::initTestCase()
{
QVERIFY(dbs.open());
recreateTestTables();
}
void tst_QSqlThread::cleanupTestCase()
{
dropTestTables();
dbs.close();
}
void tst_QSqlThread::init()
{
threadFinishedCount = 0;
counter.storeRelaxed(4);
}
void tst_QSqlThread::cleanup()
{
// repopulateTestTables();
}
// This test creates two threads that clone their db connection and read
// from it
void tst_QSqlThread::simpleThreading()
{
QFETCH(QString, dbName);
QSqlDatabase db = QSqlDatabase::database(dbName);
CHECK_DATABASE(db);
if (db.databaseName() == ":memory:")
QSKIP("does not work with in-memory databases");
QtTestSqlThread t1(db);
QtTestSqlThread t2(db);
connect(&t1, SIGNAL(finished()), this, SLOT(threadFinished()), Qt::QueuedConnection);
connect(&t2, SIGNAL(finished()), this, SLOT(threadFinished()), Qt::QueuedConnection);
t1.start();
t2.start();
QTRY_VERIFY(threadFinishedCount >= 2);
}
// This test creates two threads that clone their db connection and read
// or write
void tst_QSqlThread::readWriteThreading()
{
QFETCH(QString, dbName);
QSqlDatabase db = QSqlDatabase::database(dbName);
CHECK_DATABASE(db);
if (db.databaseName() == ":memory:")
QSKIP("does not work with in-memory databases");
else if (tst_Databases::isMSAccess(db))
QSKIP("does not work with MS Access databases");
SqlProducer producer(db);
SqlConsumer consumer(db);
connect(&producer, SIGNAL(finished()), this, SLOT(threadFinished()), Qt::QueuedConnection);
connect(&consumer, SIGNAL(finished()), this, SLOT(threadFinished()), Qt::QueuedConnection);
producer.start();
consumer.start();
QTRY_VERIFY_WITH_TIMEOUT(threadFinishedCount >= 2, 10000);
}
#ifdef QOCI_THREADED
// run with n threads in parallel. Change this constant to hammer the poor DB server even more
static const int maxThreadCount = 4;
#endif
void tst_QSqlThread::readFromSingleConnection()
{
#ifdef QOCI_THREADED
QFETCH(QString, dbName);
QSqlDatabase db = QSqlDatabase::database(dbName);
CHECK_DATABASE(db);
if (db.databaseName() == ":memory:")
QSKIP("does not work with in-memory databases");
QObject cleanupHelper; // make sure the threads die when we exit the scope
for (int i = 0; i < maxThreadCount; ++i) {
SqlThread *reader = new SqlThread(SqlThread::SimpleReading, db, &cleanupHelper);
connect(reader, SIGNAL(finished()), this, SLOT(threadFinished()), Qt::QueuedConnection);
reader->start();
}
QTRY_VERIFY(threadFinishedCount >= maxThreadCount);
#endif
}
void tst_QSqlThread::readWriteFromSingleConnection()
{
#ifdef QOCI_THREADED
QFETCH(QString, dbName);
QSqlDatabase db = QSqlDatabase::database(dbName);
CHECK_DATABASE(db);
if (db.databaseName() == ":memory:")
QSKIP("does not work with in-memory databases");
QObject cleanupHelper;
for (int i = 0; i < maxThreadCount; ++i) {
SqlThread *reader = new SqlThread(SqlThread::SimpleReading, db, &cleanupHelper);
connect(reader, SIGNAL(finished()), this, SLOT(threadFinished()), Qt::QueuedConnection);
reader->start();
SqlThread *writer = new SqlThread(SqlThread::SimpleWriting, db, &cleanupHelper);
connect(writer, SIGNAL(finished()), this, SLOT(threadFinished()), Qt::QueuedConnection);
writer->start();
}
QTRY_VERIFY(threadFinishedCount >= maxThreadCount * 2);
#endif
}
void tst_QSqlThread::preparedReadWriteFromSingleConnection()
{
#ifdef QOCI_THREADED
QFETCH(QString, dbName);
QSqlDatabase db = QSqlDatabase::database(dbName);
CHECK_DATABASE(db);
if (db.databaseName() == ":memory:")
QSKIP("does not work with in-memory databases");
QObject cleanupHelper;
for (int i = 0; i < maxThreadCount; ++i) {
SqlThread *reader = new SqlThread(SqlThread::PreparedReading, db, &cleanupHelper);
connect(reader, SIGNAL(finished()), this, SLOT(threadFinished()), Qt::QueuedConnection);
reader->start();
SqlThread *writer = new SqlThread(SqlThread::PreparedWriting, db, &cleanupHelper);
connect(writer, SIGNAL(finished()), this, SLOT(threadFinished()), Qt::QueuedConnection);
writer->start();
}
QTRY_VERIFY(threadFinishedCount >= maxThreadCount * 2);
#endif
}
void tst_QSqlThread::transactionsFromSingleConnection()
{
#ifdef QOCI_THREADED
QFETCH(QString, dbName);
QSqlDatabase db = QSqlDatabase::database(dbName);
CHECK_DATABASE(db);
if (db.databaseName() == ":memory:")
QSKIP("does not work with in-memory databases");
// start and commit a transaction
QVERIFY_SQL(db, db.transaction());
preparedReadWriteFromSingleConnection(); // read and write from multiple threads
if (QTest::currentTestFailed())
return;
QVERIFY_SQL(db, db.commit());
// reset test environment
threadFinishedCount = 0;
// start and roll back a transaction
QVERIFY_SQL(db, db.transaction());
preparedReadWriteFromSingleConnection(); // read and write from multiple threads
if (QTest::currentTestFailed())
return;
QVERIFY_SQL(db, db.rollback());
#endif
}
QTEST_MAIN(tst_QSqlThread)
#include "tst_qsqlthread.moc"
|