From 74507c00aef3dd64f0d47c023dbbe2efc360e189 Mon Sep 17 00:00:00 2001 From: Lukasz Wojciechowski Date: Fri, 17 Oct 2014 16:15:43 +0200 Subject: [PATCH] Add CynaraTestAsync::RequestMonitor class 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 | 1 + .../common/cynara_test_client_async_client.h | 2 + .../cynara_test_client_async_request_monitor.cpp | 92 ++++++++++++++++++++++ .../cynara_test_client_async_request_monitor.h | 52 ++++++++++++ 4 files changed, 147 insertions(+) create mode 100644 tests/cynara-tests/common/cynara_test_client_async_request_monitor.cpp create mode 100644 tests/cynara-tests/common/cynara_test_client_async_request_monitor.h diff --git a/tests/cynara-tests/CMakeLists.txt b/tests/cynara-tests/CMakeLists.txt index 05d9c2a..c18dc21 100644 --- a/tests/cynara-tests/CMakeLists.txt +++ b/tests/cynara-tests/CMakeLists.txt @@ -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 diff --git a/tests/cynara-tests/common/cynara_test_client_async_client.h b/tests/cynara-tests/common/cynara_test_client_async_client.h index 9c57f20..e3cecdb 100644 --- a/tests/cynara-tests/common/cynara_test_client_async_client.h +++ b/tests/cynara-tests/common/cynara_test_client_async_client.h @@ -17,6 +17,7 @@ #ifndef CYNARA_TEST_CLIENT_ASYNC_CLIENT_H #define CYNARA_TEST_CLIENT_ASYNC_CLIENT_H +#include #include #include @@ -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 index 0000000..daab147 --- /dev/null +++ b/tests/cynara-tests/common/cynara_test_client_async_request_monitor.cpp @@ -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 + +#include + +#include + +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(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 index 0000000..16d4926 --- /dev/null +++ b/tests/cynara-tests/common/cynara_test_client_async_request_monitor.h @@ -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 + +#include +#include + +namespace CynaraTestClientAsync { + +typedef std::function 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 m_requests; +}; + +}// namespace CynaraTestClientAsync + +#endif // CYNARA_TEST_CLIENT_ASYNC_REQUEST_MONITOR_H -- 2.7.4