信号没有被QSignalSpy捕获

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了信号没有被QSignalSpy捕获相关的知识,希望对你有一定的参考价值。

我正在为我的一个班级编写一些单元测试。但是,我很快遇到了一个问题。似乎QSignalSpy有时候没有捕捉到我的disconnected()类发出的Client信号。这是我的单元测试:

void ClientTest::test_disconnectFromHost()
{
    QTcpServer server;
    server.listen(QHostAddress::Any, 65000);

    QTcpSocket* clientSocket = new QTcpSocket();
    QSignalSpy connectedSpy(clientSocket, SIGNAL(connected()));

    clientSocket->connectToHost(QHostAddress::Any, 65000);
    if (!connectedSpy.wait()) {
        QFAIL("Client socket connection failed.");
    }

    Chat::Client client(clientSocket);
    QSignalSpy disconnectedSpy(clientSocket, SIGNAL(disconnected()));

    // Return true if disconnection succeded
    QVERIFY(client.disconnectFromHost());
    QVERIFY(disconnectedSpy.wait(500));  // RETURNS FALSE!

    // Return false if client was not connected
    QVERIFY(!client.disconnectFromHost());
    QCOMPARE(client.getLastError(), 
       Chat::Client::Error::NotConnected);

    clientSocket->connectToHost(QHostAddress::Any, 65000);
    if (!connectedSpy.wait()) {
        QFAIL("Client socket connection failed.");
    }

    QTcpSocket* serverSocket = server.nextPendingConnection();
    serverSocket->disconnectFromHost(); // RETURNS FALSE!

    // Emit disconnected if the server closed the connection
    QVERIFY(disconnectedSpy.wait(500));

    delete serverSocket;
}

以下是相关的类方法:

Client::Client(QTcpSocket* socket, QObject* parent)
    : QObject(parent)
{
    this->socket = socket;
    messageSize = -1;

    lastError = None;
    lastErrorString = "";

    connect(socket, SIGNAL(connected()), this, SLOT(onSocketConnected()));
    connect(socket, SIGNAL(disconnected()), this, SLOT(onSocketDisconnected()));
    connect(socket, SIGNAL(error(QAbstractSocket::SocketError)), this, SLOT(onSocketError(QAbstractSocket::SocketError)));
    connect(socket, SIGNAL(readyRead()), this, SLOT(onDataReceived()));
}

bool Client::connectToHost(const QHostAddress& serverAddress, quint16 serverPort)
{
    if (socket->state() != QTcpSocket::SocketState::UnconnectedState) {
        lastError = AlreadyConnected;
        return false;
    }

    messageSize = -1;

    socket->connectToHost(serverAddress, serverPort);
    return true;
}

bool Client::disconnectFromHost()
{
    if (socket->state() != QTcpSocket::SocketState::ConnectedState) {
        lastError = NotConnected;
        return false;
    }

    socket->disconnectFromHost();
    return true;
}

void Client::onSocketConnected()
{
    emit connected();
}

void Client::onSocketDisconnected()
{
    emit disconnected();
}

[...]

有没有人解释为什么QSignalSpy.wait()方法返回false,即使通过调试单元测试,我知道发出了disconnected()信号?

谢谢!

答案

最后,问题是QSignalSpy会在调用QSignalSpy :: wait()之前捕获发出的信号。因此,QSignalSpy :: wait()总是导致超时并返回false。

我已经通过实现两个辅助方法来验证QSignalSpy的计数,然后等待计数不正确。然后我在单元测试中使用这些方法。

namespace SignalUtils {

bool verifySignal(QSignalSpy& spy, int expectedCount);
bool verifyNoSignal(QSignalSpy& spy, int expectedCount);

}

bool SignalUtils::verifySignal(QSignalSpy& spy, int expectedCount)
{
    return spy.count() == expectedCount || spy.wait(1000);
}

bool SignalUtils::verifyNoSignal(QSignalSpy& spy, int expectedCount)
{
    return spy.count() == expectedCount && !spy.wait(1000);
}

以上是关于信号没有被QSignalSpy捕获的主要内容,如果未能解决你的问题,请参考以下文章

信号没有被其他类捕获

linux可重入异步信号安全和线程安全

Kill

发送 stderr/stdout 消息到函数并捕获退出信号

Q_PROPERTY 发出的信号如何被捕获?

linux可重入异步信号安全和线程安全