mudlet/test/EventLoopPumpTest.cpp
Vadim Peretokin e3204258fe
fix: stop the test-only waitForEvent() wedging Mudlet on macOS (#9691)
#### Brief overview of PR changes/additions

- Fixes #9670 "Mudlet stops responding on macOS part way through the
package specs": the test-only `waitForEvent()` ran a nested
`QEventLoop::exec()`, which the Cocoa dispatcher services by re-entering
`-[NSApplication run]` from inside the timer callout it is already in -
so no Qt timer, including the wait's own timeout, ever fires again.
`EventLoopPump::pumpFor()` drives `processEvents()` against a deadline
instead, which activates Qt's timers on every pass.
- Lifts the macOS pending gate the package specs have carried since they
were written: ~40 specs now run on both macOS legs, green.
- Fixes four pre-existing crashes (each reproduced on `development`
under ASan) when a profile or the application is closed from a handler
delivered during a wait: profile closes are held off while a pump runs,
application shutdowns postpone rather than cancel.

#### Motivation for adding to Mudlet

macOS was the only platform not running the package specs, and the hang
wedged real CI runs.

#### Other info (issues closed, discussion etc)

Test case: #9689's harness with this fix runs the previously hanging
suite to completion (2277/0) on both macOS runners; this PR's own legs:
2370/0 on both macOS arches, 2426/0 on Linux with ASan and leak
checking.

Assisted-by: Claude:claude-opus-5
2026-08-07 06:12:30 +02:00

141 lines
4.6 KiB
C++

/***************************************************************************
* Copyright (C) 2026 by Vadim Peretokin - vadim.peretokin@mudlet.org *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, write to the *
* Free Software Foundation, Inc., *
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
***************************************************************************/
#include <QtTest/QtTest>
#include <QElapsedTimer>
#include <QTimer>
#include "EventLoopPump.h"
/*
* The macOS CI legs are what make pumpingFromInsideATimerCallback() worth
* having: that is the position a nested QEventLoop::exec() stops seeing Qt
* timers in (issue #9670), and no other platform reproduces it.
*/
class EventLoopPumpTest : public QObject
{
Q_OBJECT
private slots:
void runsOutTheClockWithNoCondition();
void makesOnePassForAZeroTimeout();
void stopsAsSoonAsTheConditionHolds();
void stopsWithoutPumpingWhenTheConditionAlreadyHolds();
void deliversATimerThatComesDueWhilePumping();
void pumpingFromInsideATimerCallback();
private:
bool mDelivered = false;
};
void EventLoopPumpTest::runsOutTheClockWithNoCondition()
{
QElapsedTimer elapsed;
elapsed.start();
QVERIFY(!EventLoopPump::pumpFor(120));
QVERIFY2(elapsed.elapsed() >= 110, qPrintable(QString::number(elapsed.elapsed())));
}
void EventLoopPumpTest::makesOnePassForAZeroTimeout()
{
bool delivered = false;
QMetaObject::invokeMethod(
this,
[&delivered]() {
delivered = true;
},
Qt::QueuedConnection);
QVERIFY(!EventLoopPump::pumpFor(0));
QVERIFY2(delivered, "a zero timeout did not deliver the already-posted event");
}
void EventLoopPumpTest::stopsAsSoonAsTheConditionHolds()
{
bool done = false;
QTimer::singleShot(50, this, [&done]() {
done = true;
});
QElapsedTimer elapsed;
elapsed.start();
QVERIFY(EventLoopPump::pumpFor(5000, [&done]() {
return done;
}));
QVERIFY2(elapsed.elapsed() < 2000, qPrintable(QString::number(elapsed.elapsed())));
}
void EventLoopPumpTest::stopsWithoutPumpingWhenTheConditionAlreadyHolds()
{
mDelivered = false;
QMetaObject::invokeMethod(
this,
[this]() {
mDelivered = true;
},
Qt::QueuedConnection);
QVERIFY(EventLoopPump::pumpFor(1000, []() {
return true;
}));
QVERIFY2(!mDelivered, "a condition that already held should not have pumped anything");
// Drain the still-queued event so it cannot turn up mid-way through the
// next test.
QVERIFY(!EventLoopPump::pumpFor(20));
QVERIFY(mDelivered);
}
void EventLoopPumpTest::deliversATimerThatComesDueWhilePumping()
{
bool fired = false;
QTimer::singleShot(40, this, [&fired]() {
fired = true;
});
QVERIFY(!EventLoopPump::pumpFor(300));
QVERIFY2(fired, "a timer that came due during the pump did not fire");
}
void EventLoopPumpTest::pumpingFromInsideATimerCallback()
{
// A regression here hangs rather than fails: nothing ever completes the
// outer callback.
bool innerFired = false;
bool firedDuringPump = false;
bool outerDone = false;
QTimer::singleShot(0, this, [&]() {
QTimer::singleShot(40, this, [&innerFired]() {
innerFired = true;
});
QVERIFY(!EventLoopPump::pumpFor(300));
firedDuringPump = innerFired;
outerDone = true;
});
QVERIFY(EventLoopPump::pumpFor(5000, [&outerDone]() {
return outerDone;
}));
QVERIFY2(firedDuringPump, "a timer did not fire while pumping from inside a timer callback");
}
QTEST_MAIN(EventLoopPumpTest)
#include "EventLoopPumpTest.moc"