diff --git a/include/qredis/client.h b/include/qredis/client.h index 009b33a..66776d6 100644 --- a/include/qredis/client.h +++ b/include/qredis/client.h @@ -1,7 +1,6 @@ #ifndef QREDIS_CLIENT_H #define QREDIS_CLIENT_H -#include #include #include @@ -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 diff --git a/include/qredis/request.h b/include/qredis/request.h index c35c875..7faae14 100644 --- a/include/qredis/request.h +++ b/include/qredis/request.h @@ -32,7 +32,7 @@ namespace QRedis */ virtual ~Request(); - signals: + Q_SIGNALS: /** * @brief Emitted when a bulk reply is received diff --git a/src/client.cpp b/src/client.cpp index cfa3eaf..c6851a8 100644 --- a/src/client.cpp +++ b/src/client.cpp @@ -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); +} diff --git a/src/client_p.h b/src/client_p.h index d82fdb1..6d08deb 100644 --- a/src/client_p.h +++ b/src/client_p.h @@ -28,7 +28,7 @@ namespace QRedis QQueue queue; QByteArray buffer; - public slots: + public Q_SLOTS: void reset(); void readReply(); diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index dd4a44f..ccd9c3c 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -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) diff --git a/tests/main.cpp b/tests/main.cpp index 9179de7..0aaf139 100644 --- a/tests/main.cpp +++ b/tests/main.cpp @@ -1,6 +1,13 @@ -#include +#include +#include -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); } diff --git a/tests/testclient.cpp b/tests/testclient.cpp new file mode 100644 index 0000000..15657d0 --- /dev/null +++ b/tests/testclient.cpp @@ -0,0 +1,15 @@ +#include + +#include "testclient.h" + +void TestClient::initTestCase() +{ + client.connectToHost("localhost"); + QVERIFY(client.waitForConnected()); +} + +void TestClient::cleanupTestCase() +{ + client.disconnectFromHost(); + QVERIFY(client.waitForDisconnected()); +} diff --git a/tests/testclient.h b/tests/testclient.h new file mode 100644 index 0000000..3431a8a --- /dev/null +++ b/tests/testclient.h @@ -0,0 +1,22 @@ +#ifndef TEST_CLIENT_H +#define TEST_CLIENT_H + +#include + +#include + +class TestClient : public QObject +{ + Q_OBJECT + + public Q_SLOTS: + + void initTestCase(); + void cleanupTestCase(); + + private: + + QRedis::Client client; +}; + +#endif // TEST_CLIENT_H