File indexing completed on 2024-11-10 04:40:18

0001 /*
0002     SPDX-FileCopyrightText: 2019 David Faure <faure@kde.org>
0003 
0004     SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #include <QObject>
0008 #include <QSqlQuery>
0009 #include <QTest>
0010 
0011 #include "storage/dbdeadlockcatcher.h"
0012 
0013 #include "aktest.h"
0014 
0015 using namespace Akonadi::Server;
0016 
0017 class DbDeadlockCatcherTest : public QObject
0018 {
0019     Q_OBJECT
0020 
0021 private:
0022     int m_myFuncCalled = 0;
0023     void myFunc(int maxRecursion)
0024     {
0025         ++m_myFuncCalled;
0026         if (m_myFuncCalled <= maxRecursion) {
0027             throw DbDeadlockException(QSqlQuery());
0028         }
0029     }
0030 
0031 private Q_SLOTS:
0032     void testRecurseOnce()
0033     {
0034         m_myFuncCalled = 0;
0035         DbDeadlockCatcher catcher([this]() {
0036             myFunc(1);
0037         });
0038         QCOMPARE(m_myFuncCalled, 2);
0039     }
0040 
0041     void testRecurseTwice()
0042     {
0043         m_myFuncCalled = 0;
0044         DbDeadlockCatcher catcher([this]() {
0045             myFunc(2);
0046         });
0047         QCOMPARE(m_myFuncCalled, 3);
0048     }
0049 
0050     void testHitRecursionLimit()
0051     {
0052         m_myFuncCalled = 0;
0053         QVERIFY_EXCEPTION_THROWN(DbDeadlockCatcher catcher([this]() {
0054                                      myFunc(10);
0055                                  }),
0056                                  DbDeadlockException);
0057         QCOMPARE(m_myFuncCalled, 6);
0058     }
0059 };
0060 
0061 AKTEST_MAIN(DbDeadlockCatcherTest)
0062 
0063 #include "dbdeadlockcatchertest.moc"