Implemented test suite that tests the connection and disconnection process.

This commit is contained in:
Nathan Osman 2013-07-01 21:45:04 -07:00
parent 59c4beff21
commit 32f01b33cb
8 changed files with 94 additions and 12 deletions

View File

@ -1,7 +1,6 @@
#ifndef QREDIS_CLIENT_H #ifndef QREDIS_CLIENT_H
#define QREDIS_CLIENT_H #define QREDIS_CLIENT_H
#include <QHostAddress>
#include <QObject> #include <QObject>
#include <QScopedPointer> #include <QScopedPointer>
@ -34,13 +33,13 @@ namespace QRedis
/** /**
* @brief Attempts to connect to the specified Redis server * @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 * @param port the port that the Redis server is listening on
* *
* If the connection was successful, the connected() signal will be * If the connection was successful, the connected() signal will be
* emitted. * emitted.
*/ */
void connectToHost(const QHostAddress & address, quint16 port = 6379); void connectToHost(const QString & hostName, quint16 port = 6379);
/** /**
* @brief Disconnects from the Redis server * @brief Disconnects from the Redis server
@ -54,7 +53,23 @@ namespace QRedis
*/ */
Request * sendCommand(const QString & command); 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 * @brief Emitted when the client establishes a connection with the Redis server

View File

@ -32,7 +32,7 @@ namespace QRedis
*/ */
virtual ~Request(); virtual ~Request();
signals: Q_SIGNALS:
/** /**
* @brief Emitted when a bulk reply is received * @brief Emitted when a bulk reply is received

View File

@ -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() void Client::disconnectFromHost()
@ -96,3 +96,13 @@ Request * Client::sendCommand(const QString & command)
d->queue.enqueue(request); d->queue.enqueue(request);
return request; return request;
} }
bool Client::waitForConnected(int msecs)
{
return d->socket.waitForConnected(msecs);
}
bool Client::waitForDisconnected(int msecs)
{
return d->socket.waitForDisconnected(msecs);
}

View File

@ -28,7 +28,7 @@ namespace QRedis
QQueue<Request *> queue; QQueue<Request *> queue;
QByteArray buffer; QByteArray buffer;
public slots: public Q_SLOTS:
void reset(); void reset();
void readReply(); void readReply();

View File

@ -1,12 +1,25 @@
# The QtTest libraries are required for building the tests. # The QtTest libraries are required for building the tests.
find_package(Qt5Test REQUIRED) 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. # Create the test executable.
add_executable(qredis-tests add_executable(qredis-tests
main.cpp) ${QREDIS_TESTS_SRC}
${QREDIS_TESTS_MOC})
# Specify the Qt modules the test executable links against. # Specify the Qt modules the test executable links against.
qt5_use_modules(qredis-tests Core Test) qt5_use_modules(qredis-tests Core Test)
# Naturally, we also link against the QRedis library. # Naturally, we also link against the QRedis library.
target_link_libraries(qredis-tests qredis) target_link_libraries(qredis-tests qredis)
# Register the target as a test.
add_test(tests qredis-tests)

View File

@ -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);
} }

15
tests/testclient.cpp Normal file
View File

@ -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());
}

22
tests/testclient.h Normal file
View File

@ -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