--- /dev/null
+/*
+ * Copyright (c) 2015 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.
+ */
+
+/**
+ * @file test_cases_timeout.cpp
+ * @author Lukasz Wojciechowski <l.wojciechow@partner.samsung.com>
+ * @version 1.0
+ * @brief Inner tests for timeout mechanism
+ */
+
+#include <chrono>
+#include <string>
+#include <thread>
+
+#include <dpl/test/test_runner.h>
+#include <common/timeout.h>
+
+RUNNER_TEST_GROUP_INIT(TIMEOUT)
+
+void timeout_test_ret(int waitDuration, int functionDuration, Timeout::ExpectMode expect) {
+ float fRet = 3.1415;
+ auto ret = Timeout::callAndWait(std::chrono::seconds(waitDuration), expect,
+ Timeout::CancelFunction(),
+ ([fRet](int sec) -> float {
+ std::this_thread::sleep_for(std::chrono::seconds(sec));
+ return fRet;
+ }), functionDuration);
+ RUNNER_ASSERT_MSG(ret == fRet,
+ "Function returned = " << ret << " while expected value was " << fRet);
+}
+
+RUNNER_TEST(it01_expected_timeout)
+{
+ timeout_test_ret(3, 5, Timeout::ExpectMode::TIMEOUT);
+}
+
+RUNNER_TEST(it02_unexpected_finish)
+{
+ bool thrown = false;
+ try {
+ timeout_test_ret(3, 5, Timeout::ExpectMode::FINISHED);
+ } catch (const DPL::Test::TestException&) {
+ thrown = true;
+ }
+ RUNNER_ASSERT_MSG(thrown,
+ "Test should throw DPL::Test::TestException");
+}
+
+RUNNER_TEST(it03_ignored_timeout)
+{
+ timeout_test_ret(3, 5, Timeout::ExpectMode::IGNORE);
+}
+
+RUNNER_TEST(it04_expected_finish)
+{
+ timeout_test_ret(5, 3, Timeout::ExpectMode::FINISHED);
+}
+
+RUNNER_TEST(it05_unexpected_timeout)
+{
+ bool thrown = false;
+ try {
+ timeout_test_ret(5, 3, Timeout::ExpectMode::TIMEOUT);
+ } catch (const DPL::Test::TestException&) {
+ thrown = true;
+ }
+ RUNNER_ASSERT_MSG(thrown,
+ "Test should throw DPL::Test::TestException");
+}
+
+RUNNER_TEST(it06_ignored_finish)
+{
+ timeout_test_ret(5, 3, Timeout::ExpectMode::IGNORE);
+}
+
+void timeout_test_throw(int waitDuration, int functionDuration, Timeout::ExpectMode expect) {
+ std::string exceptionString("exceptionString");
+ bool thrown = false;
+ try {
+ Timeout::callAndWait(std::chrono::seconds(waitDuration), expect,
+ Timeout::CancelFunction(),
+ ([exceptionString](int sec) -> float {
+ std::this_thread::sleep_for(std::chrono::seconds(sec));
+ throw exceptionString;
+ }), functionDuration);
+ } catch (const std::string &str) {
+ RUNNER_ASSERT_MSG(str == exceptionString,
+ "Function thrown = " << str
+ << " while expected value was " << exceptionString);
+ thrown = true;
+ }
+ RUNNER_ASSERT_MSG(thrown,
+ "Test should throw std::string(" << exceptionString << ")");
+}
+
+RUNNER_TEST(it07_throw_expected_timeout)
+{
+ timeout_test_throw(3, 5, Timeout::ExpectMode::TIMEOUT);
+}
+
+RUNNER_TEST(it08_throw_unexpected_finish)
+{
+ bool thrown = false;
+ try {
+ timeout_test_throw(3, 5, Timeout::ExpectMode::FINISHED);
+ } catch (const DPL::Test::TestException&) {
+ thrown = true;
+ }
+ RUNNER_ASSERT_MSG(thrown,
+ "Test should throw DPL::Test::TestException");
+}
+
+RUNNER_TEST(it09_throw_ignored_timeout)
+{
+ timeout_test_throw(3, 5, Timeout::ExpectMode::IGNORE);
+}
+
+RUNNER_TEST(it10_throw_expected_finish)
+{
+ timeout_test_throw(5, 3, Timeout::ExpectMode::FINISHED);
+}
+
+RUNNER_TEST(it11_throw_unexpected_timeout)
+{
+ bool thrown = false;
+ try {
+ timeout_test_throw(5, 3, Timeout::ExpectMode::TIMEOUT);
+ } catch (const DPL::Test::TestException&) {
+ thrown = true;
+ }
+ RUNNER_ASSERT_MSG(thrown,
+ "Test should throw DPL::Test::TestException");
+}
+
+RUNNER_TEST(it12_throw_ignored_finish)
+{
+ timeout_test_throw(5, 3, Timeout::ExpectMode::IGNORE);
+}