File indexing completed on 2025-02-16 04:37:45

0001 /*
0002     SPDX-FileCopyrightText: 2010 Joris Guisson <joris.guisson@gmail.com>
0003 
0004     SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 
0007 #include <QObject>
0008 #include <QRandomGenerator>
0009 #include <QtTest>
0010 
0011 #include <util/functions.h>
0012 #include <util/log.h>
0013 #include <util/resourcemanager.h>
0014 
0015 using namespace bt;
0016 
0017 static bt::Resource *last_acquired = nullptr;
0018 
0019 class TestResource : public bt::Resource
0020 {
0021 public:
0022     TestResource(ResourceManager *rman, const QString &group)
0023         : Resource(rman, group)
0024         , acq(false)
0025     {
0026     }
0027 
0028     void acquired() override
0029     {
0030         Out(SYS_GEN | LOG_DEBUG) << "Resource of " << groupName() << " acquired" << endl;
0031         acq = true;
0032         last_acquired = this;
0033     }
0034 
0035     bool acq;
0036 };
0037 
0038 class ResourceManagerTest : public QObject
0039 {
0040     Q_OBJECT
0041 public:
0042 private Q_SLOTS:
0043     void initTestCase()
0044     {
0045         bt::InitLog("resourcemanagertest.log");
0046     }
0047 
0048     void cleanupTestCase()
0049     {
0050     }
0051 
0052     void testSingleClass()
0053     {
0054         Out(SYS_GEN | LOG_DEBUG) << "testSingleClass" << endl;
0055         ResourceManager rm(4);
0056 
0057         QList<TestResource *> tr;
0058         for (int i = 0; i < 8; i++) {
0059             TestResource *r = new TestResource(&rm, "test");
0060             tr.append(r);
0061             rm.add(r);
0062             // The first 4 should get acquired
0063             QVERIFY(r->acq == (i < 4));
0064         }
0065 
0066         for (int i = 0; i < 4; i++) {
0067             delete tr.takeFirst();
0068             QVERIFY(tr.at(3)->acq); // The next availabe one should now be acquired
0069         }
0070         qDeleteAll(tr);
0071     }
0072 
0073     void testMultiClass()
0074     {
0075         Out(SYS_GEN | LOG_DEBUG) << "testMultiClass" << endl;
0076         ResourceManager rm(4);
0077         const char *classes[4] = {"aaa", "bbb", "ccc", "ddd"};
0078 
0079         // 4 resources for each class
0080         QList<TestResource *> tr;
0081         for (int i = 0; i < 16; i++) {
0082             TestResource *r = new TestResource(&rm, classes[i % 4]);
0083             tr.append(r);
0084             rm.add(r);
0085             // The first 4 should get acquired
0086             QVERIFY(r->acq == (i < 4));
0087         }
0088 
0089         QString last_group;
0090         for (int i = 0; i < 12; i++) {
0091             Resource *r = tr.takeFirst();
0092             QString g = r->groupName();
0093             delete r;
0094             QVERIFY(tr.at(3)->acq); // The next availabe one should now be acquired
0095             QVERIFY(g != last_group);
0096             last_group = g;
0097         }
0098         qDeleteAll(tr);
0099     }
0100 
0101     void testFullyRandom()
0102     {
0103         Out(SYS_GEN | LOG_DEBUG) << "testFullyRandom" << endl;
0104         ResourceManager rm(4);
0105         const char *classes[4] = {"aaa", "bbb", "ccc", "ddd"};
0106 
0107         // A random amount of resources for each class
0108         QList<TestResource *> tr;
0109 
0110         Uint32 num_acquired = 0;
0111         for (int i = 0; i < 500; i++) {
0112             TestResource *r = new TestResource(&rm, classes[QRandomGenerator::global()->bounded(4)]);
0113             tr.append(r);
0114             rm.add(r);
0115             if (r->acq)
0116                 num_acquired++;
0117         }
0118 
0119         QVERIFY(num_acquired == 4);
0120 
0121         QString last_acquired_group;
0122         for (int i = 0; i < 496; i++) {
0123             tr.removeAll((TestResource *)last_acquired);
0124             delete last_acquired;
0125             QVERIFY(last_acquired);
0126             if (last_acquired->groupName() == last_acquired_group) {
0127                 // This is only possible if there are no other groups which are still pending
0128                 for (TestResource *r : std::as_const(tr))
0129                     if (!r->acq)
0130                         QVERIFY(r->groupName() == last_acquired_group);
0131             }
0132             last_acquired_group = last_acquired->groupName();
0133         }
0134         qDeleteAll(tr);
0135     }
0136 };
0137 
0138 QTEST_MAIN(ResourceManagerTest)
0139 
0140 #include "resourcemanagertest.moc"