Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix coroutine being resumed on a wrong thread after timeout #215

Merged
merged 2 commits into from
Mar 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion qcoro/core/qcorosignal.h
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ class QCoroSignalBase {
[this, awaitingCoroutine]() {
QObject::disconnect(mConn);
awaitingCoroutine.resume();
});
}, Qt::DirectConnection); // force coro to be resumed on our thread, not mObj's thread
mTimeoutTimer->start();
}
}
Expand Down
22 changes: 22 additions & 0 deletions tests/qcorosignal.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include "qcoro/core/qcorosignal.h"

#include <QTimer>
#include <QThread>

using namespace std::chrono_literals;

Expand Down Expand Up @@ -36,6 +37,8 @@ class SignalTest : public QObject {
void privateVoid(QPrivateSignal);
void privateSingleArg(const QString &, QPrivateSignal);
void privateMultiArg(const QString &, int, QObject *, QPrivateSignal);

void signalThatsNeverEmitted();
};

class MultiSignalTest : public SignalTest {
Expand Down Expand Up @@ -394,6 +397,24 @@ class QCoroSignalTest : public QCoro::TestObject<QCoroSignalTest> {
QCORO_COMPARE(count, 10);
}

QCoro::Task<> testSignalEmitterOnDifferentThread_coro(QCoro::TestContext) {
SignalTest test;
QThread thread;
test.moveToThread(&thread);
thread.start();

co_await qCoro(&test, &SignalTest::voidSignal);
// Make sure we are resumed on our thread
QCORO_COMPARE(QThread::currentThread(), qApp->thread());

co_await qCoro(&test, &SignalTest::signalThatsNeverEmitted, 20ms);
// Make sure we are resumed on our thread after the timeout
QCORO_COMPARE(QThread::currentThread(), qApp->thread());

thread.quit();
thread.wait();
}

private Q_SLOTS:
addTest(Triggers)
addTest(ReturnsValue)
Expand All @@ -420,6 +441,7 @@ private Q_SLOTS:
addTest(SignalListenerQPrivateSignalVoid)
addTest(SignalListenerQPrivateSignalValue)
addTest(SignalListenerQPrivateSignalTuple)
addTest(SignalEmitterOnDifferentThread)
};

QTEST_GUILESS_MAIN(QCoroSignalTest)
Expand Down
Loading