summaryrefslogtreecommitdiffstats
path: root/tests/modelinteractivitytest.cpp
blob: d13cdf73a48eb05082237659f1fe23ef4961667a (plain)
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
#include <QtTest>

#include <QString>
#include <iostream>

#include "dummyresource/resourcefactory.h"
#include "store.h"
#include "resourcecontrol.h"
#include "commands.h"
#include "resourceconfig.h"
#include "log.h"
#include "modelresult.h"
#include "test.h"

static int blockingTime;

class TimeMeasuringApplication : public QCoreApplication
{
    QElapsedTimer t;

public:
    TimeMeasuringApplication(int &argc, char **argv) : QCoreApplication(argc, argv)
    {
    }
    virtual ~TimeMeasuringApplication()
    {
    }

    virtual bool notify(QObject *receiver, QEvent *event)
    {
        t.start();
        const bool ret = QCoreApplication::notify(receiver, event);
        if (t.elapsed() > 1)
            std::cout
                << QString("processing event type %1 for object %2 took %3ms").arg((int)event->type()).arg("" /* receiver->objectName().toLocal8Bit().data()*/).arg((int)t.elapsed()).toStdString()
                << std::endl;
        blockingTime += t.elapsed();
        return ret;
    }
};

/**
 * Ensure that queries don't block the system for an extended period of time.
 *
 * This is done by ensuring that the event loop is never blocked.
 */
class ModelinteractivityTest : public QObject
{
    Q_OBJECT
private slots:
    void initTestCase()
    {
        Sink::Test::initTest();
        ResourceConfig::addResource("org.kde.dummy.instance1", "org.kde.dummy");
        Sink::Store::removeDataFromDisk(QByteArray("org.kde.dummy.instance1")).exec().waitForFinished();
    }

    void cleanup()
    {
        Sink::Store::removeDataFromDisk(QByteArray("org.kde.dummy.instance1")).exec().waitForFinished();
    }

    void init()
    {
    }

    void testSingle()
    {
        // Setup
        {
            Sink::ApplicationDomain::Mail mail("org.kde.dummy.instance1");
            for (int i = 0; i < 1000; i++) {
                Sink::Store::create<Sink::ApplicationDomain::Mail>(mail).exec().waitForFinished();
            }
        }

        Sink::Query query;
        query.resources << "org.kde.dummy.instance1";
        query.liveQuery = true;

        Sink::ResourceControl::flushMessageQueue(query.resources).exec().waitForFinished();

        // Test
        QTime time;
        time.start();
        auto model = Sink::Store::loadModel<Sink::ApplicationDomain::Mail>(query);
        blockingTime += time.elapsed();
        QTRY_VERIFY(model->data(QModelIndex(), Sink::Store::ChildrenFetchedRole).toBool());
        // Never block longer than 10 ms
        QVERIFY2(blockingTime < 10, QString("Total blocking time: %1").arg(blockingTime).toLatin1().data());
    }
};

int main(int argc, char *argv[])
{
    blockingTime = 0;
    TimeMeasuringApplication app(argc, argv);
    ModelinteractivityTest tc;
    return QTest::qExec(&tc, argc, argv);
}

#include "modelinteractivitytest.moc"