Warning, file /pim/sink/tests/modelinteractivitytest.cpp was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).

0001 #include <QTest>
0002 
0003 #include <QString>
0004 #include <iostream>
0005 
0006 #include "store.h"
0007 #include "resourcecontrol.h"
0008 #include "commands.h"
0009 #include "resourceconfig.h"
0010 #include "log.h"
0011 #include "modelresult.h"
0012 #include "test.h"
0013 
0014 static int blockingTime;
0015 
0016 class TimeMeasuringApplication : public QCoreApplication
0017 {
0018     QElapsedTimer t;
0019 
0020 public:
0021     TimeMeasuringApplication(int &argc, char **argv) : QCoreApplication(argc, argv)
0022     {
0023     }
0024     ~TimeMeasuringApplication() override
0025     {
0026     }
0027 
0028     bool notify(QObject *receiver, QEvent *event) override
0029     {
0030         t.start();
0031         auto receiverName = receiver->metaObject()->className();
0032         const bool ret = QCoreApplication::notify(receiver, event);
0033         if (t.elapsed() > 1) {
0034             std::cout
0035                 << QString("processing event type %1 for object %2 took %3ms").arg((int)event->type()).arg(receiverName).arg((int)t.elapsed()).toStdString()
0036                 << std::endl;
0037         }
0038         blockingTime += t.elapsed();
0039         return ret;
0040     }
0041 };
0042 
0043 /**
0044  * Ensure that queries don't block the system for an extended period of time.
0045  *
0046  * This is done by ensuring that the event loop is never blocked.
0047  */
0048 class ModelinteractivityTest : public QObject
0049 {
0050     Q_OBJECT
0051 private slots:
0052     void initTestCase()
0053     {
0054         Sink::Test::initTest();
0055         ResourceConfig::addResource("sink.dummy.instance1", "sink.dummy");
0056         VERIFYEXEC(Sink::Store::removeDataFromDisk(QByteArray("sink.dummy.instance1")));
0057     }
0058 
0059     void cleanup()
0060     {
0061         VERIFYEXEC(Sink::Store::removeDataFromDisk(QByteArray("sink.dummy.instance1")));
0062     }
0063 
0064     void init()
0065     {
0066     }
0067 
0068     void testSingle()
0069     {
0070         // Setup
0071         {
0072             Sink::ApplicationDomain::Event event("sink.dummy.instance1");
0073             for (int i = 0; i < 1000; i++) {
0074                 Sink::Store::create<Sink::ApplicationDomain::Event>(event).exec().waitForFinished();
0075             }
0076         }
0077 
0078         Sink::Query query;
0079         query.resourceFilter("sink.dummy.instance1");
0080         query.setFlags(Sink::Query::LiveQuery);
0081 
0082         VERIFYEXEC(Sink::ResourceControl::flushMessageQueue(QByteArrayList() << "sink.dummy.instance1"));
0083 
0084         // Test
0085         QTime time;
0086         time.start();
0087         auto model = Sink::Store::loadModel<Sink::ApplicationDomain::Event>(query);
0088         blockingTime += time.elapsed();
0089         QTRY_VERIFY(model->data(QModelIndex(), Sink::Store::ChildrenFetchedRole).toBool());
0090         if (blockingTime > 10) {
0091             QWARN(QString("Total blocking longer than expected time (10ms): %1").arg(blockingTime).toLatin1().data());
0092         }
0093     }
0094 };
0095 
0096 int main(int argc, char *argv[])
0097 {
0098     blockingTime = 0;
0099     TimeMeasuringApplication app(argc, argv);
0100     ModelinteractivityTest tc;
0101     return QTest::qExec(&tc, argc, argv);
0102 }
0103 
0104 #include "modelinteractivitytest.moc"