summaryrefslogtreecommitdiffstats
path: root/tests/resourcecommunicationtest.cpp
blob: db8703ecbb689af927d5fc573b63729485ebd34a (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
#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<void>([&complete]() {
                    complete++;
                },
                [&errors, &complete](int error, const QString &msg) {
                    qWarning() << msg;
                    errors++;
                    complete++;
                }).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<void>([&complete]() {
                    complete++;
                },
                [&errors, &complete](int error, const QString &msg) {
                    qWarning() << msg;
                    errors++;
                    complete++;
                }).then<void>([&resourceAccess]() {
                    resourceAccess.close();
                    resourceAccess.open();
                }).exec().waitForFinished();
        }
        QTRY_COMPARE(complete, count);
        QVERIFY(!errors);
    }
};

QTEST_MAIN(ResourceCommunicationTest)
#include "resourcecommunicationtest.moc"