QTcpClient 成功连接,但未连接到我的服务器。它在哪里连接?
我已经使用 Qt 的 Tcp Sockets API 成功构建了一个瘦客户端/服务器。我知道它效果很好,因为我已经通过网络发送了大量数据并对其进行了验证。然而,我的项目经理想要一套单元测试,我正在使用 Qt 的测试库来实现它们。
无论如何,我试图设置一些虚拟服务器来简单地从 QTcpSocket 接收数据,以验证单元测试中的 sendData() 方法。当我连接测试套接字时,显示它已连接,但是用于连接虚拟服务器及其虚拟套接字的插槽从未被调用!
有人能看到我在这里做错了什么吗? (我已将代码精简为测试类中似乎损坏的部分)
来自 tst_tcpcommsocket.h
#ifndef TST_TCPCOMMSOCKET_H
#define TST_TCPCOMMSOCKET_H
#include <QtCore/QString>
#include <QtTest/QtTest>
#include <iostream>
#include <QTcpSocket>
#include <QTcpServer>
class TcpCommSocketTest : public QObject
{
Q_OBJECT
private:
QTcpSocket* qTestSocket;
QTcpSocket* qDummySocket;
QTcpServer* qDummyServer;
public:
TcpCommSocketTest();
public slots:
void connectDummyServer();
private Q_SLOTS:
void initTestCase();
void sendDataTest();
void cleanupTestCase();
};
#endif // TST_TCPCOMMSOCKET_H
来自 tst_tcpcommsocket.cpp
#include "tst_tcpcommsocket.h"
void TcpCommSocketTest::connectDummyServer()
{
cout << "connection attempt" << endl;
qDummySocket = qDummyServer->nextPendingConnection();
}
void TcpCommSocketTest::initTestCase()
{
qDummySocket = NULL;
qDummyServer = new QTcpServer();
qDummyServer->listen( QHostAddress("127.0.0.1"), 9000 );
connect( qDummyServer, SIGNAL(newConnection()), SLOT(connectDummyServer()) );
qTestSocket = new QTcpSocket();
qTestSocket->connectToHost( QHostAddress("127.0.0.1"), 9000 );
QVERIFY( qTestSocket->waitForConnected( 5000 ) );
QVERIFY( qTestSocket->state() == QTcpSocket::ConnectedState );
}
void TcpCommSocketTest::sendDataTest()
{
int i=0;
QVERIFY( qDummySocket != NULL );
}
QTEST_MAIN(TcpCommSocketTest);
测试运行的输出:
********* Start testing of TcpCommSocketTest *********
Config: Using QTest library 4.7.3, Qt 4.7.3
PASS : TcpCommSocketTest::initTestCase()
FAIL! : TcpCommSocketTest::sendDataTest() 'qDummySocket != NULL' returned FALSE. ()
Loc: [-]
cleanup
PASS : TcpCommSocketTest::cleanupTestCase()
Totals: 2 passed, 1 failed, 0 skipped
I have successfully built a thin client/server using Qt's Tcp Sockets API. I know it works very well, because I have sent plenty of data over the wire and verified it. However, my project manager wants a suite of unit-tests, and I'm implementing them using Qt's Test Library.
Anyhow, I'm trying to set up some dummy server to simply receive data from a QTcpSocket to verify a sendData() method in a unit test. When I connect the test socket, is shows that it is connected, but the slot for connecting the dummy server and its dummy socket is never called!
Can anyone see what I'm doing wrong here?
( I've stripped the code down down to just the parts that seem broken in the test class )
From tst_tcpcommsocket.h
#ifndef TST_TCPCOMMSOCKET_H
#define TST_TCPCOMMSOCKET_H
#include <QtCore/QString>
#include <QtTest/QtTest>
#include <iostream>
#include <QTcpSocket>
#include <QTcpServer>
class TcpCommSocketTest : public QObject
{
Q_OBJECT
private:
QTcpSocket* qTestSocket;
QTcpSocket* qDummySocket;
QTcpServer* qDummyServer;
public:
TcpCommSocketTest();
public slots:
void connectDummyServer();
private Q_SLOTS:
void initTestCase();
void sendDataTest();
void cleanupTestCase();
};
#endif // TST_TCPCOMMSOCKET_H
From tst_tcpcommsocket.cpp
#include "tst_tcpcommsocket.h"
void TcpCommSocketTest::connectDummyServer()
{
cout << "connection attempt" << endl;
qDummySocket = qDummyServer->nextPendingConnection();
}
void TcpCommSocketTest::initTestCase()
{
qDummySocket = NULL;
qDummyServer = new QTcpServer();
qDummyServer->listen( QHostAddress("127.0.0.1"), 9000 );
connect( qDummyServer, SIGNAL(newConnection()), SLOT(connectDummyServer()) );
qTestSocket = new QTcpSocket();
qTestSocket->connectToHost( QHostAddress("127.0.0.1"), 9000 );
QVERIFY( qTestSocket->waitForConnected( 5000 ) );
QVERIFY( qTestSocket->state() == QTcpSocket::ConnectedState );
}
void TcpCommSocketTest::sendDataTest()
{
int i=0;
QVERIFY( qDummySocket != NULL );
}
QTEST_MAIN(TcpCommSocketTest);
The Test Run's output:
********* Start testing of TcpCommSocketTest *********
Config: Using QTest library 4.7.3, Qt 4.7.3
PASS : TcpCommSocketTest::initTestCase()
FAIL! : TcpCommSocketTest::sendDataTest() 'qDummySocket != NULL' returned FALSE. ()
Loc: [-]
cleanup
PASS : TcpCommSocketTest::cleanupTestCase()
Totals: 2 passed, 1 failed, 0 skipped
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
continue
While
QTEST_MAIN
creates a QApplication and runs all your tests, it does so sequentially and not with an event loop. Therefore, while your socket can indeed connect to the server (meaningqTestSocket->waitForConnected()
returnstrue
), since the app doesn't return to an event loop, theQTcpServer
's signal will not get emitted, andTcpCommSocketTest::connectDummyServer()
never gets called.Try adding a call to
qApp->processEvents()
at the end ofinitTestCase()
. It should letconnectDummyServer()
be called.您可能正在连接到之前启动的服务器实例。使用进程监视器检查以确保没有任何杂散实例。您的测试设置是否会自动启动和关闭虚拟服务器?测试可能成功启动服务器,但未能终止它。
另外,从 127.0.0.1 开始,虚拟服务器与测试代码在同一台机器上运行,对吗?
You could be connecting to an instance of the server started earlier. Poke around with a process monitor to make sure you don't have any stray instances. Does your test setup automatically kick off and shutdown the dummy server? It could be that the test is succesfully starting the server, but failing to kill it.
Also, from the 127.0.0.1, the dummy server is running on the same machine as the test code right?
验证
qDummyServer->listen()
的返回值。如果服务器确实在端口 9000 上侦听,则应返回true
。如果返回false
,则端口 9000 很可能被另一个进程阻止,并且无法侦听。在这种情况下,您的 qTestSocket 连接到其他进程而不是您自己的服务器。Verify the return value of
qDummyServer->listen()
. It should returntrue
if the server is indeed listening at port 9000. If it returnsfalse
, chances are port 9000 is blocked by another process, and it couldn't listen. In that case, yourqTestSocket
connected to that other process instead of your own server.