2 * Copyright (c) 2015 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.
18 * @file test_cases_timeout.cpp
19 * @author Lukasz Wojciechowski <l.wojciechow@partner.samsung.com>
21 * @brief Inner tests for timeout mechanism
28 #include <dpl/test/test_runner.h>
29 #include <common/timeout.h>
31 RUNNER_TEST_GROUP_INIT(TIMEOUT)
33 void timeout_test_ret(int waitDuration, int functionDuration, Timeout::ExpectMode expect) {
35 auto ret = Timeout::callAndWait(std::chrono::seconds(waitDuration), expect,
36 Timeout::CancelFunction(),
37 ([fRet](int sec) -> float {
38 std::this_thread::sleep_for(std::chrono::seconds(sec));
40 }), functionDuration);
41 RUNNER_ASSERT_MSG(ret == fRet,
42 "Function returned = " << ret << " while expected value was " << fRet);
45 RUNNER_TEST(it01_expected_timeout)
47 timeout_test_ret(3, 5, Timeout::ExpectMode::TIMEOUT);
50 RUNNER_TEST(it02_unexpected_finish)
54 timeout_test_ret(3, 5, Timeout::ExpectMode::FINISHED);
55 } catch (const DPL::Test::TestException&) {
58 RUNNER_ASSERT_MSG(thrown,
59 "Test should throw DPL::Test::TestException");
62 RUNNER_TEST(it03_ignored_timeout)
64 timeout_test_ret(3, 5, Timeout::ExpectMode::IGNORE);
67 RUNNER_TEST(it04_expected_finish)
69 timeout_test_ret(5, 3, Timeout::ExpectMode::FINISHED);
72 RUNNER_TEST(it05_unexpected_timeout)
76 timeout_test_ret(5, 3, Timeout::ExpectMode::TIMEOUT);
77 } catch (const DPL::Test::TestException&) {
80 RUNNER_ASSERT_MSG(thrown,
81 "Test should throw DPL::Test::TestException");
84 RUNNER_TEST(it06_ignored_finish)
86 timeout_test_ret(5, 3, Timeout::ExpectMode::IGNORE);
89 void timeout_test_throw(int waitDuration, int functionDuration, Timeout::ExpectMode expect) {
90 std::string exceptionString("exceptionString");
93 Timeout::callAndWait(std::chrono::seconds(waitDuration), expect,
94 Timeout::CancelFunction(),
95 ([exceptionString](int sec) -> float {
96 std::this_thread::sleep_for(std::chrono::seconds(sec));
97 throw exceptionString;
98 }), functionDuration);
99 } catch (const std::string &str) {
100 RUNNER_ASSERT_MSG(str == exceptionString,
101 "Function thrown = " << str
102 << " while expected value was " << exceptionString);
105 RUNNER_ASSERT_MSG(thrown,
106 "Test should throw std::string(" << exceptionString << ")");
109 RUNNER_TEST(it07_throw_expected_timeout)
111 timeout_test_throw(3, 5, Timeout::ExpectMode::TIMEOUT);
114 RUNNER_TEST(it08_throw_unexpected_finish)
118 timeout_test_throw(3, 5, Timeout::ExpectMode::FINISHED);
119 } catch (const DPL::Test::TestException&) {
122 RUNNER_ASSERT_MSG(thrown,
123 "Test should throw DPL::Test::TestException");
126 RUNNER_TEST(it09_throw_ignored_timeout)
128 timeout_test_throw(3, 5, Timeout::ExpectMode::IGNORE);
131 RUNNER_TEST(it10_throw_expected_finish)
133 timeout_test_throw(5, 3, Timeout::ExpectMode::FINISHED);
136 RUNNER_TEST(it11_throw_unexpected_timeout)
140 timeout_test_throw(5, 3, Timeout::ExpectMode::TIMEOUT);
141 } catch (const DPL::Test::TestException&) {
144 RUNNER_ASSERT_MSG(thrown,
145 "Test should throw DPL::Test::TestException");
148 RUNNER_TEST(it12_throw_ignored_finish)
150 timeout_test_throw(5, 3, Timeout::ExpectMode::IGNORE);