Wrap cynara_async_process() for test purposes 17/29017/16
authorLukasz Wojciechowski <l.wojciechow@partner.samsung.com>
Mon, 20 Oct 2014 09:14:18 +0000 (11:14 +0200)
committerLukasz Wojciechowski <l.wojciechow@partner.samsung.com>
Sat, 6 Dec 2014 01:03:24 +0000 (02:03 +0100)
Wrap checks connections status, then waits on proper select statement.
After select returns, cynara_async_process() is called and expected
result is checked.

Change-Id: Icc286662b5ba7e0f4f9b51d0dd58e7ab1d17336a

tests/cynara-tests/common/cynara_test_client_async_client.cpp
tests/cynara-tests/common/cynara_test_client_async_client.h

index 3d0f0d8..e90f5eb 100644 (file)
@@ -21,6 +21,7 @@
 #include <cynara-client-async.h>
 
 #include <exception>
+#include <unistd.h>
 
 namespace CynaraTestClientAsync {
 
@@ -113,4 +114,44 @@ void Client::createRequest(const CheckData &checkData, cynara_check_id &id,
                              << " privilege = " << checkData.m_privilege << ".");
 }
 
+void Client::process(int expectedResult,
+                     enum TimeoutExpectation timeoutExpectation,
+                     time_t timeoutSeconds) {
+    if (m_statusMonitor.getStatus() == DISCONNECTED)
+        return;
+
+    int fd = m_statusMonitor.getFd();
+    fd_set fds;
+    timeval tv;
+    FD_ZERO(&fds);
+    FD_SET(fd, &fds);
+    tv.tv_sec = timeoutSeconds;
+    tv.tv_usec = 0;
+
+    int ret;
+    if (m_statusMonitor.getStatus() == READ)
+        ret = TEMP_FAILURE_RETRY(select(fd + 1, &fds, NULL, NULL, &tv));
+    else
+        ret = TEMP_FAILURE_RETRY(select(fd + 1, &fds, &fds, NULL, &tv));
+
+    if (ret == 0) {
+        RUNNER_ASSERT_ERRNO_MSG(timeoutExpectation != EXPECT_NO_TIMEOUT,
+                                   "Unexpected select timeout."
+                                   << " ret = " << ret);
+        return;
+    }
+    RUNNER_ASSERT_ERRNO_MSG(ret > 0,
+                               "Select returned error:"
+                               << " ret = " << ret);
+    RUNNER_ASSERT_ERRNO_MSG(timeoutExpectation != EXPECT_TIMEOUT,
+                               "Select returned positive value, when timeout was expected."
+                               << " ret = " << ret);
+
+    ret = cynara_async_process(m_cynara);
+    RUNNER_ASSERT_MSG(ret == expectedResult,
+                         "cynara_async_process returned unexpected value: "
+                             << " returned value = " << ret << ","
+                             << " expected value = " << expectedResult << ".");
+}
+
 }// namespace CynaraTestClientAsync
index f5da908..afa549c 100644 (file)
@@ -23,6 +23,7 @@
 #include <cynara-client-async.h>
 
 #include <string>
+#include <sys/types.h>
 
 namespace CynaraTestClientAsync {
 
@@ -40,6 +41,12 @@ struct CheckData
 class Client
 {
 public:
+    enum TimeoutExpectation {
+        EXPECT_TIMEOUT,
+        EXPECT_NO_TIMEOUT,
+        IGNORE_TIMEOUT,
+    };
+
     Client();
     ~Client() noexcept(false);
 
@@ -47,6 +54,9 @@ public:
     void checkCache(const CheckData &checkData, int expectedResult);
     void createRequest(const CheckData &checkData, cynara_check_id &id,
                        const RequestEntity &callbackData, int expectedResult = CYNARA_API_SUCCESS);
+    void process(int expectedResult = CYNARA_API_SUCCESS,
+                 enum TimeoutExpectation timeoutExpectation = EXPECT_NO_TIMEOUT,
+                 time_t timeoutSeconds = 3);
 
 private:
     struct cynara_async *m_cynara;