From 1cbc875e1e85b8e0ecb835807d5470923dd56603 Mon Sep 17 00:00:00 2001 From: Lukasz Wojciechowski Date: Tue, 20 Jan 2015 15:18:37 +0100 Subject: [PATCH] Apply defer macros to callbacks in cynara async client After all cynara library functions were sorrounded with try catch macros causing all exceptions to be caught, there is no way of throwning an exception from inside of callback function. This is where defer macros mechanism can be applied. Now all exceptions will be rethrown after comming back from libcynara-client-async API functions. Change-Id: I9743557631f2b13cecddf33cb072f280ae689d0a --- .../common/cynara_test_client_async_client.cpp | 34 ++++++----- .../cynara_test_client_async_request_monitor.cpp | 68 +++++++++++----------- .../cynara_test_client_async_status_monitor.cpp | 28 ++++----- 3 files changed, 71 insertions(+), 59 deletions(-) diff --git a/src/cynara-tests/common/cynara_test_client_async_client.cpp b/src/cynara-tests/common/cynara_test_client_async_client.cpp index 3201f97..ac4e9de 100644 --- a/src/cynara-tests/common/cynara_test_client_async_client.cpp +++ b/src/cynara-tests/common/cynara_test_client_async_client.cpp @@ -52,8 +52,10 @@ CheckKey CheckData::toAdminPolicy() Client::Client(const StatusFunction &userFunction) : m_cynara(nullptr), m_statusMonitor(userFunction) { - int ret = cynara_async_initialize(&m_cynara, nullptr, StatusMonitor::updateStatus, - reinterpret_cast(&m_statusMonitor)); + int ret; + RUNNER_DEFER_SCOPE(ret = cynara_async_initialize(&m_cynara, nullptr, + StatusMonitor::updateStatus, + reinterpret_cast(&m_statusMonitor));); RUNNER_ASSERT_MSG(ret == CYNARA_API_SUCCESS, "cynara_async_initialize() failed. ret = " << ret << "."); RUNNER_ASSERT_MSG(m_cynara != nullptr, "cynara_async struct was not initialized."); @@ -65,7 +67,7 @@ Client::~Client() noexcept(false) { bool oops = std::uncaught_exception(); try { - cynara_async_finish(m_cynara); + RUNNER_DEFER_SCOPE(cynara_async_finish(m_cynara);); assertStatus(DISCONNECTED); } catch (...) { if (!oops) @@ -85,9 +87,11 @@ void Client::assertStatus(enum SocketStatus expectedStatus) void Client::checkCache(const CheckData &checkData, int expectedResult) { - int ret = cynara_async_check_cache(m_cynara, checkData.m_client.c_str(), - checkData.m_session.c_str(), checkData.m_user.c_str(), - checkData.m_privilege.c_str()); + int ret; + RUNNER_DEFER_SCOPE(ret = cynara_async_check_cache(m_cynara, checkData.m_client.c_str(), + checkData.m_session.c_str(), + checkData.m_user.c_str(), + checkData.m_privilege.c_str());); RUNNER_ASSERT_MSG(ret == expectedResult, "Cache check returned unexpected value: " << " returned value = " << ret << "," @@ -101,11 +105,14 @@ void Client::checkCache(const CheckData &checkData, int expectedResult) void Client::createRequest(const CheckData &checkData, cynara_check_id &id, const RequestEntity &callbackData, int expectedResult) { - int ret = cynara_async_create_request(m_cynara, checkData.m_client.c_str(), - checkData.m_session.c_str(), checkData.m_user.c_str(), - checkData.m_privilege.c_str(), &id, - RequestMonitor::updateResponse, - reinterpret_cast(&m_requestMonitor)); + int ret; + RUNNER_DEFER_SCOPE(ret = cynara_async_create_request(m_cynara, checkData.m_client.c_str(), + checkData.m_session.c_str(), + checkData.m_user.c_str(), + checkData.m_privilege.c_str(), &id, + RequestMonitor::updateResponse, + reinterpret_cast( + &m_requestMonitor));); if (ret == CYNARA_API_SUCCESS) m_requestMonitor.registerRequest(id, callbackData); @@ -152,7 +159,7 @@ void Client::process(int expectedResult, "Select returned positive value, when timeout was expected." << " ret = " << ret); - ret = cynara_async_process(m_cynara); + RUNNER_DEFER_SCOPE(ret = cynara_async_process(m_cynara);); RUNNER_ASSERT_MSG(ret == expectedResult, "cynara_async_process returned unexpected value: " << " returned value = " << ret << "," @@ -161,7 +168,8 @@ void Client::process(int expectedResult, void Client::cancel(cynara_check_id id, int expectedResult) { - int ret = cynara_async_cancel_request(m_cynara, id); + int ret; + RUNNER_DEFER_SCOPE(ret = cynara_async_cancel_request(m_cynara, id);); RUNNER_ASSERT_MSG(ret == expectedResult, "Cancel request returned unexpected value: " << " returned value = " << ret << "," diff --git a/src/cynara-tests/common/cynara_test_client_async_request_monitor.cpp b/src/cynara-tests/common/cynara_test_client_async_request_monitor.cpp index ee08c7e..3827797 100644 --- a/src/cynara-tests/common/cynara_test_client_async_request_monitor.cpp +++ b/src/cynara-tests/common/cynara_test_client_async_request_monitor.cpp @@ -53,48 +53,50 @@ void RequestMonitor::registerRequest(cynara_check_id id, const RequestEntity &re void RequestMonitor::updateResponse(cynara_check_id checkId, cynara_async_call_cause cause, int response, void *data) { - RequestMonitor *monitor = reinterpret_cast(data); - if (!monitor) { - RUNNER_FAIL_MSG("Bad user data (nullptr) in response callback."); - return; - } - - auto it = monitor->m_requests.find(checkId); - if (it == monitor->m_requests.end()) { - RUNNER_FAIL_MSG("Received unexpected callback for request:" - << "id = " << checkId << "," - << "response = " << response << "," - << "cause = " << cause << "."); - return; - } + RUNNER_DEFER_TRYCATCH( + RequestMonitor *monitor = reinterpret_cast(data); + if (!monitor) { + RUNNER_FAIL_MSG("Bad user data (nullptr) in response callback."); + return; + } - //save request data and remove request from monitored requests - auto expectedResponse = it->second.m_expectedResponse; - auto expectedCause = it->second.m_expectedCause; - auto userFunction = it->second.m_userFunction; - monitor->m_requests.erase(it); + auto it = monitor->m_requests.find(checkId); + if (it == monitor->m_requests.end()) { + RUNNER_FAIL_MSG("Received unexpected callback for request:" + << "id = " << checkId << "," + << "response = " << response << "," + << "cause = " << cause << "."); + return; + } - RUNNER_ASSERT_MSG(cause == expectedCause, - "Unexpected cause in response callback:" - << "id = " << checkId << "," - << "received response = " << response << "," - << "expected response = " << expectedResponse << "," - << "received cause = " << cause << "," - << "expected cause = " << expectedCause << "."); + //save request data and remove request from monitored requests + auto expectedResponse = it->second.m_expectedResponse; + auto expectedCause = it->second.m_expectedCause; + auto userFunction = it->second.m_userFunction; + monitor->m_requests.erase(it); - if (cause == CYNARA_CALL_CAUSE_ANSWER) - { - RUNNER_ASSERT_MSG(response == expectedResponse, - "Unexpected response in response callback:" + RUNNER_ASSERT_MSG(cause == expectedCause, + "Unexpected cause in response callback:" << "id = " << checkId << "," << "received response = " << response << "," << "expected response = " << expectedResponse << "," << "received cause = " << cause << "," << "expected cause = " << expectedCause << "."); - } - if (userFunction) - userFunction(); + if (cause == CYNARA_CALL_CAUSE_ANSWER) + { + RUNNER_ASSERT_MSG(response == expectedResponse, + "Unexpected response in response callback:" + << "id = " << checkId << "," + << "received response = " << response << "," + << "expected response = " << expectedResponse << "," + << "received cause = " << cause << "," + << "expected cause = " << expectedCause << "."); + } + + if (userFunction) + userFunction(); + ); } }// namespace CynaraTestClientAsync diff --git a/src/cynara-tests/common/cynara_test_client_async_status_monitor.cpp b/src/cynara-tests/common/cynara_test_client_async_status_monitor.cpp index 2a2e0ae..37e3f3c 100644 --- a/src/cynara-tests/common/cynara_test_client_async_status_monitor.cpp +++ b/src/cynara-tests/common/cynara_test_client_async_status_monitor.cpp @@ -27,21 +27,23 @@ StatusMonitor::StatusMonitor(const StatusFunction &userFunction) void StatusMonitor::updateStatus(int oldFd, int newFd, cynara_async_status status, void *data) { - StatusMonitor *monitor = reinterpret_cast(data); - if (!monitor) { - RUNNER_FAIL_MSG("Bad user data (nullptr) in status callback."); - return; - } + RUNNER_DEFER_TRYCATCH( + StatusMonitor *monitor = reinterpret_cast(data); + if (!monitor) { + RUNNER_FAIL_MSG("Bad user data (nullptr) in status callback."); + return; + } - RUNNER_ASSERT_MSG(monitor->m_fd == oldFd, - "fd value mismatch: " - << " last saved fd = " << monitor->m_fd << "," - << " callback oldFd = " << oldFd << "."); + RUNNER_ASSERT_MSG(monitor->m_fd == oldFd, + "fd value mismatch: " + << " last saved fd = " << monitor->m_fd << "," + << " callback oldFd = " << oldFd << "."); - monitor->m_fd = newFd; - monitor->m_status = status; - if (monitor->m_userFunction) - monitor->m_userFunction(oldFd, newFd, status); + monitor->m_fd = newFd; + monitor->m_status = status; + if (monitor->m_userFunction) + monitor->m_userFunction(oldFd, newFd, status); + ); } int StatusMonitor::getFd(void) const -- 2.7.4