Add CynaraTestAsync::RequestMonitor class 65/28965/7
authorLukasz Wojciechowski <l.wojciechow@partner.samsung.com>
Fri, 17 Oct 2014 14:15:43 +0000 (16:15 +0200)
committerLukasz Wojciechowski <l.wojciechow@partner.samsung.com>
Thu, 30 Oct 2014 10:58:47 +0000 (11:58 +0100)
Added class shall be used for monitoring all requests, matching
callbacks and checking expected causes and responses in callbacks.

Change-Id: I0ddb2d8848d80950d430f768510c6466144b399d

tests/cynara-tests/CMakeLists.txt
tests/cynara-tests/common/cynara_test_client_async_client.h
tests/cynara-tests/common/cynara_test_client_async_request_monitor.cpp [new file with mode: 0644]
tests/cynara-tests/common/cynara_test_client_async_request_monitor.h [new file with mode: 0644]

index 05d9c2a..c18dc21 100644 (file)
@@ -18,6 +18,7 @@ SET(CYNARA_TARGET_TEST_SOURCES
     ${PROJECT_SOURCE_DIR}/tests/cynara-tests/common/cynara_test_admin.cpp
     ${PROJECT_SOURCE_DIR}/tests/cynara-tests/common/cynara_test_client.cpp
     ${PROJECT_SOURCE_DIR}/tests/cynara-tests/common/cynara_test_client_async_client.cpp
+    ${PROJECT_SOURCE_DIR}/tests/cynara-tests/common/cynara_test_client_async_request_monitor.cpp
     ${PROJECT_SOURCE_DIR}/tests/cynara-tests/common/cynara_test_client_async_status_monitor.cpp
     ${PROJECT_SOURCE_DIR}/tests/cynara-tests/common/cynara_test_commons.cpp
     ${PROJECT_SOURCE_DIR}/tests/cynara-tests/common/cynara_test_env.cpp
index 9c57f20..e3cecdb 100644 (file)
@@ -17,6 +17,7 @@
 #ifndef CYNARA_TEST_CLIENT_ASYNC_CLIENT_H
 #define CYNARA_TEST_CLIENT_ASYNC_CLIENT_H
 
+#include <cynara_test_client_async_request_monitor.h>
 #include <cynara_test_client_async_status_monitor.h>
 
 #include <cynara-client-async.h>
@@ -35,6 +36,7 @@ private:
     struct cynara_async *m_cynara;
 
     StatusMonitor m_statusMonitor;
+    RequestMonitor m_requestMonitor;
 };
 
 }// namespace CynaraTestClientAsync
diff --git a/tests/cynara-tests/common/cynara_test_client_async_request_monitor.cpp b/tests/cynara-tests/common/cynara_test_client_async_request_monitor.cpp
new file mode 100644 (file)
index 0000000..daab147
--- /dev/null
@@ -0,0 +1,92 @@
+/*
+ * Copyright (c) 2014 Samsung Electronics Co., Ltd All Rights Reserved
+ *
+ *    Licensed under the Apache License, Version 2.0 (the "License");
+ *    you may not use this file except in compliance with the License.
+ *    You may obtain a copy of the License at
+ *
+ *        http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *    Unless required by applicable law or agreed to in writing, software
+ *    distributed under the License is distributed on an "AS IS" BASIS,
+ *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *    See the License for the specific language governing permissions and
+ *    limitations under the License.
+ */
+
+#include <cynara_test_client_async_request_monitor.h>
+
+#include <dpl/test/test_runner.h>
+
+#include <exception>
+
+namespace CynaraTestClientAsync {
+
+RequestMonitor::~RequestMonitor() noexcept(false)
+{
+    bool oops = std::uncaught_exception();
+    try {
+        for (auto ent : m_requests)
+        {
+            RUNNER_ERROR_MSG("There was no callback for request with:"
+                          << "id = " << ent.first << ","
+                          << "expectedResponse = " << ent.second.m_expectedResponse << ","
+                          << "expectedCause = " << ent.second.m_expectedCause << ".");
+        }
+        RUNNER_ASSERT_MSG(m_requests.empty(),
+                             m_requests.size() << "requests does not receive callback.");
+    } catch (...) {
+        if (!oops)
+            throw;
+        RUNNER_ERROR_MSG("Error: more exceptions thrown while releasing"
+                             " CynaraTestAsync::RequestMonitor.");
+    }
+}
+
+void RequestMonitor::registerRequest(cynara_check_id id, const RequestEntity &request)
+{
+    auto p = m_requests.insert({id, request});
+    RUNNER_ASSERT_MSG(p.second,
+                          "Request with id = " << p.first->first << " already exists.");
+}
+
+void RequestMonitor::updateResponse(cynara_check_id checkId, cynara_async_call_cause cause,
+                                    int response, void *data)
+{
+    RequestMonitor *monitor = reinterpret_cast<RequestMonitor*>(data);
+    RUNNER_ASSERT_MSG(monitor != nullptr,
+                         "Bad user data (nullptr) in response callback.");
+
+    auto it = monitor->m_requests.find(checkId);
+    RUNNER_ASSERT_MSG(it != monitor->m_requests.end(),
+                         "Received unexpected callback for request:"
+                            << "id = " << checkId << ","
+                            << "response = " << response << ","
+                            << "cause = " << cause << ".");
+
+    RUNNER_ASSERT_MSG(cause == it->second.m_expectedCause,
+                         "Unexpected cause in response callback:"
+                            << "id = " << checkId << ","
+                            << "received response = " << response << ","
+                            << "expected response = " << it->second.m_expectedResponse << ","
+                            << "received cause = " << cause << ","
+                            << "expected cause = " << it->second.m_expectedCause << ".");
+
+    if (cause == CYNARA_CALL_CAUSE_ANSWER)
+    {
+        RUNNER_ASSERT_MSG(response == it->second.m_expectedResponse,
+                             "Unexpected response in response callback:"
+                                << "id = " << checkId << ","
+                                << "received response = " << response << ","
+                                << "expected response = " << it->second.m_expectedResponse << ","
+                                << "received cause = " << cause << ","
+                                << "expected cause = " << it->second.m_expectedCause << ".");
+    }
+
+    if (it->second.m_userFunction)
+        it->second.m_userFunction();
+
+    monitor->m_requests.erase(it);
+}
+
+}// namespace CynaraTestClientAsync
diff --git a/tests/cynara-tests/common/cynara_test_client_async_request_monitor.h b/tests/cynara-tests/common/cynara_test_client_async_request_monitor.h
new file mode 100644 (file)
index 0000000..16d4926
--- /dev/null
@@ -0,0 +1,52 @@
+/*
+ * Copyright (c) 2014 Samsung Electronics Co., Ltd All Rights Reserved
+ *
+ *    Licensed under the Apache License, Version 2.0 (the "License");
+ *    you may not use this file except in compliance with the License.
+ *    You may obtain a copy of the License at
+ *
+ *        http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *    Unless required by applicable law or agreed to in writing, software
+ *    distributed under the License is distributed on an "AS IS" BASIS,
+ *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *    See the License for the specific language governing permissions and
+ *    limitations under the License.
+ */
+
+#ifndef CYNARA_TEST_CLIENT_ASYNC_REQUEST_MONITOR_H
+#define CYNARA_TEST_CLIENT_ASYNC_REQUEST_MONITOR_H
+
+#include <cynara-client-async.h>
+
+#include <functional>
+#include <unordered_map>
+
+namespace CynaraTestClientAsync {
+
+typedef std::function<void(void)> RequestFunction;
+
+struct RequestEntity
+{
+    RequestFunction m_userFunction;
+    int m_expectedResponse;
+    cynara_async_call_cause m_expectedCause;
+};
+
+class RequestMonitor
+{
+public:
+    ~RequestMonitor() noexcept(false);
+
+    void registerRequest(cynara_check_id id, const RequestEntity &request);
+
+    static void updateResponse(cynara_check_id checkId, cynara_async_call_cause cause, int response,
+                               void *data);
+
+private:
+    std::unordered_map<cynara_check_id, RequestEntity> m_requests;
+};
+
+}// namespace CynaraTestClientAsync
+
+#endif // CYNARA_TEST_CLIENT_ASYNC_REQUEST_MONITOR_H