2 * Copyright (c) 2014 Samsung Electronics Co., Ltd All Rights Reserved
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 #include <cynara_test_client_async_request_monitor.h>
19 #include <dpl/test/test_runner.h>
23 namespace CynaraTestClientAsync {
25 RequestMonitor::~RequestMonitor() noexcept(false)
27 bool oops = std::uncaught_exception();
29 for (auto ent : m_requests)
31 RUNNER_ERROR_MSG("There was no callback for request with:"
32 << "id = " << ent.first << ","
33 << "expectedResponse = " << ent.second.m_expectedResponse << ","
34 << "expectedCause = " << ent.second.m_expectedCause << ".");
36 RUNNER_ASSERT_MSG(m_requests.empty(),
37 m_requests.size() << "requests does not receive callback.");
41 RUNNER_ERROR_MSG("Error: more exceptions thrown while releasing"
42 " CynaraTestAsync::RequestMonitor.");
46 void RequestMonitor::registerRequest(cynara_check_id id, const RequestEntity &request)
48 auto p = m_requests.insert({id, request});
49 RUNNER_ASSERT_MSG(p.second,
50 "Request with id = " << p.first->first << " already exists.");
53 void RequestMonitor::updateResponse(cynara_check_id checkId, cynara_async_call_cause cause,
54 int response, void *data)
56 RequestMonitor *monitor = reinterpret_cast<RequestMonitor*>(data);
58 RUNNER_FAIL_MSG("Bad user data (nullptr) in response callback.");
62 auto it = monitor->m_requests.find(checkId);
63 if (it == monitor->m_requests.end()) {
64 RUNNER_FAIL_MSG("Received unexpected callback for request:"
65 << "id = " << checkId << ","
66 << "response = " << response << ","
67 << "cause = " << cause << ".");
71 //save request data and remove request from monitored requests
72 auto expectedResponse = it->second.m_expectedResponse;
73 auto expectedCause = it->second.m_expectedCause;
74 auto userFunction = it->second.m_userFunction;
75 monitor->m_requests.erase(it);
77 RUNNER_ASSERT_MSG(cause == expectedCause,
78 "Unexpected cause in response callback:"
79 << "id = " << checkId << ","
80 << "received response = " << response << ","
81 << "expected response = " << expectedResponse << ","
82 << "received cause = " << cause << ","
83 << "expected cause = " << expectedCause << ".");
85 if (cause == CYNARA_CALL_CAUSE_ANSWER)
87 RUNNER_ASSERT_MSG(response == expectedResponse,
88 "Unexpected response in response callback:"
89 << "id = " << checkId << ","
90 << "received response = " << response << ","
91 << "expected response = " << expectedResponse << ","
92 << "received cause = " << cause << ","
93 << "expected cause = " << expectedCause << ".");
100 }// namespace CynaraTestClientAsync