summaryrefslogtreecommitdiffstats
path: root/tests/resourcecommunicationtest.cpp
blob: 7beafc7a4b1d42dc00773bae06bd82059a8689ed (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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
#include <QtTest>

#include "resourceaccess.h"
#include "listener.h"
#include "commands.h"
#include "handshake_generated.h"

/**
 * Test that resourceaccess and listener work together.
 */
class ResourceCommunicationTest : public QObject
{
    Q_OBJECT
private slots:
    void testConnect()
    {
        const QByteArray resourceIdentifier("test");
        Listener listener(resourceIdentifier, "");
        Sink::ResourceAccess resourceAccess(resourceIdentifier, "");

        QSignalSpy spy(&resourceAccess, &Sink::ResourceAccess::ready);
        resourceAccess.open();
        QTRY_COMPARE(spy.size(), 1);
    }

    void testHandshake()
    {
        const QByteArray resourceIdentifier("test");
        Listener listener(resourceIdentifier, "");
        Sink::ResourceAccess resourceAccess(resourceIdentifier, "");
        resourceAccess.open();

        flatbuffers::FlatBufferBuilder fbb;
        auto name = fbb.CreateString("test");
        auto command = Sink::Commands::CreateHandshake(fbb, name);
        Sink::Commands::FinishHandshakeBuffer(fbb, command);
        auto result = resourceAccess.sendCommand(Sink::Commands::HandshakeCommand, fbb).exec();
        result.waitForFinished();
        QVERIFY(!result.errorCode());
    }

    void testCommandLoop()
    {
        const QByteArray resourceIdentifier("test");
        Listener listener(resourceIdentifier, "");
        Sink::ResourceAccess resourceAccess(resourceIdentifier, "");
        resourceAccess.open();

        const int count = 500;
        int complete = 0;
        int errors = 0;
        for (int i = 0; i < count; i++) {
            auto result = resourceAccess.sendCommand(Sink::Commands::PingCommand)
                                .then([&resourceAccess, &errors, &complete](const KAsync::Error &error) {
                                    complete++;
                                    if (error) {
                                        qWarning() << error.errorMessage;
                                        errors++;
                                    }
                                })
                                .exec();
        }
        QTRY_COMPARE(complete, count);
        QVERIFY(!errors);
    }

    void testResourceAccessReuse()
    {
        qDebug();
        const QByteArray resourceIdentifier("test");
        Listener listener(resourceIdentifier, "");
        Sink::ResourceAccess resourceAccess(resourceIdentifier, "");
        resourceAccess.open();

        const int count = 10;
        int complete = 0;
        int errors = 0;
        for (int i = 0; i < count; i++) {
            resourceAccess.sendCommand(Sink::Commands::PingCommand)
                .then([&resourceAccess, &errors, &complete](const KAsync::Error &error) {
                    complete++;
                    if (error) {
                        qWarning() << error.errorMessage;
                        errors++;
                    }
                    resourceAccess.close();
                    resourceAccess.open();
                })
                .exec()
                .waitForFinished();
        }
        QTRY_COMPARE(complete, count);
        QVERIFY(!errors);
    }

    void testAccessFactory()
    {
        const QByteArray resourceIdentifier("test");
        Listener listener(resourceIdentifier, "");
        QWeakPointer<Sink::ResourceAccess> weakRef;
        QTime time;
        time.start();
        {
            auto resourceAccess = Sink::ResourceAccessFactory::instance().getAccess(resourceIdentifier, "");
            weakRef = resourceAccess.toWeakRef();
            resourceAccess->open();
            resourceAccess->sendCommand(Sink::Commands::PingCommand).then([resourceAccess]() { qDebug() << "Ping complete";  }).exec();
        }
        QVERIFY(weakRef.toStrongRef());
        QTRY_VERIFY(!weakRef.toStrongRef());
        qDebug() << "time.elapsed " << time.elapsed();
        QVERIFY(time.elapsed() < 3500);
        QVERIFY(time.elapsed() > 2500);
    }
};

QTEST_MAIN(ResourceCommunicationTest)
#include "resourcecommunicationtest.moc"