summaryrefslogtreecommitdiff
path: root/tests/auto/client/multithreaded/tst_multithreaded.cpp
blob: ce88a72fd53b832b299107768a3d1db1e7c99435 (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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
/****************************************************************************
**
** Copyright (C) 2018 The Qt Company Ltd.
** Copyright (C) 2020 UBports Foundataion.
** Contact: https://www.qt.io/licensing/
**
** This file is part of the test suite of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:GPL-EXCEPT$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3 as published by the Free Software
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
**
** $QT_END_LICENSE$
**
****************************************************************************/

#include <unistd.h>
#include <poll.h>

#include <wayland-client-core.h>

#include <QtGui/QScreen>
#include <QAbstractEventDispatcher>
#include <qpa/qplatformnativeinterface.h>

#include "mockcompositor.h"

using namespace MockCompositor;

/*
 * This class simulate a thread from another library which use poll() to wait
 * for data from Wayland compositor.
 */

class ExternalWaylandReaderThread : public QThread
{
public:
    ExternalWaylandReaderThread(struct wl_display *disp)
        : QThread()
        , m_disp(disp)
    {
        setObjectName(QStringLiteral("ExternalWaylandReader"));
    }

    ~ExternalWaylandReaderThread()
    {
        if (m_pipefd[1] != -1 && write(m_pipefd[1], "q", 1) == -1)
            qWarning("Failed to write to the pipe: %s.", strerror(errno));

        wait();
    }

protected:
    void run() override
    {
        // we use this pipe to make the loop exit otherwise if we simply used a flag on the loop condition, if stop() gets
        // called while poll() is blocking the thread will never quit since there are no wayland messages coming anymore.
        struct Pipe
        {
            Pipe(int *fds)
                : fds(fds)
            {
                if (::pipe(fds) != 0)
                    qWarning("Pipe creation failed. Quitting may hang.");
            }
            ~Pipe()
            {
                if (fds[0] != -1) {
                    close(fds[0]);
                    close(fds[1]);
                }
            }

            int *fds;
        } pipe(m_pipefd);

        struct wl_event_queue *a_queue = wl_display_create_queue(m_disp);
        struct pollfd fds[2] = { { wl_display_get_fd(m_disp), POLLIN, 0 },
                                 { m_pipefd[0], POLLIN, 0 } };

        while (true) {
            // No wl_proxy is assigned to this queue, thus guaranteed to be always empty.
            Q_ASSERT(wl_display_prepare_read_queue(m_disp, a_queue) == 0);
            wl_display_flush(m_disp);

            // Wakeup every 10 seconds so that if Qt blocks in _read_events(),
            // it won't last forever.
            poll(fds, /* nfds */ 2, 10000);

            if (fds[0].revents & POLLIN) {
                wl_display_read_events(m_disp);
            } else {
                wl_display_cancel_read(m_disp);
            }

            if (fds[1].revents & POLLIN) {
                char pipeIn;
                read(m_pipefd[0], &pipeIn, 1);
                if (pipeIn == 'q')
                    break;
            }
        }

        wl_event_queue_destroy(a_queue);
    }

private:
    struct wl_display *m_disp;
    int m_pipefd[2] = { -1, -1 };
};

class tst_multithreaded : public QObject, private DefaultCompositor
{
    Q_OBJECT
private slots:
    void initTestCase()
    {
        m_config.autoConfigure = true;
        m_config.autoEnter = false;
    }
    void init()
    {
        // a test case is given new simulated thread.
        QPlatformNativeInterface *native = QGuiApplication::platformNativeInterface();
        struct wl_display *wl_dpy =
                (struct wl_display *)native->nativeResourceForWindow("display", NULL);

        m_extThread.reset(new ExternalWaylandReaderThread(wl_dpy));
        m_extThread->start();
    }
    void cleanup()
    {
        QTRY_VERIFY2(isClean(), qPrintable(dirtyMessage()));
    }

    void mainThreadIsNotBlocked();

public:
    QScopedPointer<ExternalWaylandReaderThread> m_extThread;
};

void tst_multithreaded::mainThreadIsNotBlocked()
{
    QElapsedTimer timer;
    timer.start();

    QTest::qWait(100);
    QVERIFY(timer.elapsed() < 200);
}

QCOMPOSITOR_TEST_MAIN(tst_multithreaded)
#include "tst_multithreaded.moc"