Implemented test suite that tests the connection and disconnection process.
This commit is contained in:
parent
59c4beff21
commit
32f01b33cb
|
|
@ -1,7 +1,6 @@
|
|||
#ifndef QREDIS_CLIENT_H
|
||||
#define QREDIS_CLIENT_H
|
||||
|
||||
#include <QHostAddress>
|
||||
#include <QObject>
|
||||
#include <QScopedPointer>
|
||||
|
||||
|
|
@ -34,13 +33,13 @@ namespace QRedis
|
|||
|
||||
/**
|
||||
* @brief Attempts to connect to the specified Redis server
|
||||
* @param address the address of the Redis server
|
||||
* @param hostName the hostname of the Redis server
|
||||
* @param port the port that the Redis server is listening on
|
||||
*
|
||||
* If the connection was successful, the connected() signal will be
|
||||
* emitted.
|
||||
*/
|
||||
void connectToHost(const QHostAddress & address, quint16 port = 6379);
|
||||
void connectToHost(const QString & hostName, quint16 port = 6379);
|
||||
|
||||
/**
|
||||
* @brief Disconnects from the Redis server
|
||||
|
|
@ -54,7 +53,23 @@ namespace QRedis
|
|||
*/
|
||||
Request * sendCommand(const QString & command);
|
||||
|
||||
signals:
|
||||
/**
|
||||
* @brief Waits for the socket to finish connecting
|
||||
* @param msecs the amount of time in milliseconds to wait
|
||||
* @return true if the connection was completed
|
||||
*
|
||||
* Note: to wait indefinitely, pass a value of -1.
|
||||
*/
|
||||
bool waitForConnected(int msecs = 30000);
|
||||
|
||||
/**
|
||||
* @brief Waits for the socket to finish disconnecting
|
||||
* @param msecs the amount of time in milliseconds to wait
|
||||
* @return true if the disconnection was completed
|
||||
*/
|
||||
bool waitForDisconnected(int msecs = 30000);
|
||||
|
||||
Q_SIGNALS:
|
||||
|
||||
/**
|
||||
* @brief Emitted when the client establishes a connection with the Redis server
|
||||
|
|
|
|||
|
|
@ -32,7 +32,7 @@ namespace QRedis
|
|||
*/
|
||||
virtual ~Request();
|
||||
|
||||
signals:
|
||||
Q_SIGNALS:
|
||||
|
||||
/**
|
||||
* @brief Emitted when a bulk reply is received
|
||||
|
|
|
|||
|
|
@ -78,9 +78,9 @@ Client::~Client()
|
|||
{
|
||||
}
|
||||
|
||||
void Client::connectToHost(const QHostAddress & address, quint16 port)
|
||||
void Client::connectToHost(const QString & hostName, quint16 port)
|
||||
{
|
||||
d->socket.connectToHost(address, port);
|
||||
d->socket.connectToHost(hostName, port);
|
||||
}
|
||||
|
||||
void Client::disconnectFromHost()
|
||||
|
|
@ -96,3 +96,13 @@ Request * Client::sendCommand(const QString & command)
|
|||
d->queue.enqueue(request);
|
||||
return request;
|
||||
}
|
||||
|
||||
bool Client::waitForConnected(int msecs)
|
||||
{
|
||||
return d->socket.waitForConnected(msecs);
|
||||
}
|
||||
|
||||
bool Client::waitForDisconnected(int msecs)
|
||||
{
|
||||
return d->socket.waitForDisconnected(msecs);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -28,7 +28,7 @@ namespace QRedis
|
|||
QQueue<Request *> queue;
|
||||
QByteArray buffer;
|
||||
|
||||
public slots:
|
||||
public Q_SLOTS:
|
||||
|
||||
void reset();
|
||||
void readReply();
|
||||
|
|
|
|||
|
|
@ -1,12 +1,25 @@
|
|||
# The QtTest libraries are required for building the tests.
|
||||
find_package(Qt5Test REQUIRED)
|
||||
|
||||
# Specify the source files for the tests.
|
||||
set(QREDIS_TESTS_SRC
|
||||
main.cpp
|
||||
testclient.cpp)
|
||||
|
||||
# Specify the files that need to be MOC'd.
|
||||
qt5_wrap_cpp(QREDIS_TESTS_MOC
|
||||
testclient.h)
|
||||
|
||||
# Create the test executable.
|
||||
add_executable(qredis-tests
|
||||
main.cpp)
|
||||
${QREDIS_TESTS_SRC}
|
||||
${QREDIS_TESTS_MOC})
|
||||
|
||||
# Specify the Qt modules the test executable links against.
|
||||
qt5_use_modules(qredis-tests Core Test)
|
||||
|
||||
# Naturally, we also link against the QRedis library.
|
||||
target_link_libraries(qredis-tests qredis)
|
||||
|
||||
# Register the target as a test.
|
||||
add_test(tests qredis-tests)
|
||||
|
|
|
|||
|
|
@ -1,6 +1,13 @@
|
|||
#include <iostream>
|
||||
#include <QCoreApplication>
|
||||
#include <QTest>
|
||||
|
||||
int main(int argc, char ** argv)
|
||||
#include "testclient.h"
|
||||
|
||||
int main(int argc, char * argv[])
|
||||
{
|
||||
return 0;
|
||||
QCoreApplication app(argc, argv);
|
||||
Q_UNUSED(app)
|
||||
|
||||
TestClient test;
|
||||
return QTest::qExec(&test);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,15 @@
|
|||
#include <QTest>
|
||||
|
||||
#include "testclient.h"
|
||||
|
||||
void TestClient::initTestCase()
|
||||
{
|
||||
client.connectToHost("localhost");
|
||||
QVERIFY(client.waitForConnected());
|
||||
}
|
||||
|
||||
void TestClient::cleanupTestCase()
|
||||
{
|
||||
client.disconnectFromHost();
|
||||
QVERIFY(client.waitForDisconnected());
|
||||
}
|
||||
|
|
@ -0,0 +1,22 @@
|
|||
#ifndef TEST_CLIENT_H
|
||||
#define TEST_CLIENT_H
|
||||
|
||||
#include <QObject>
|
||||
|
||||
#include <qredis/client.h>
|
||||
|
||||
class TestClient : public QObject
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
public Q_SLOTS:
|
||||
|
||||
void initTestCase();
|
||||
void cleanupTestCase();
|
||||
|
||||
private:
|
||||
|
||||
QRedis::Client client;
|
||||
};
|
||||
|
||||
#endif // TEST_CLIENT_H
|
||||
Loading…
Reference in New Issue