Add inner tests for timeout mechanism 07/35607/7
authorLukasz Wojciechowski <l.wojciechow@partner.samsung.com>
Mon, 12 Jan 2015 10:26:10 +0000 (11:26 +0100)
committerLukasz Wojciechowski <l.wojciechow@partner.samsung.com>
Fri, 20 Mar 2015 10:56:12 +0000 (11:56 +0100)
Inner tests are needed to check if timeout mechanism works correctly
on platform that tests are runned.
Risk of not working is elevated because it uses std::future::wait_for()
method, which implementation had bugs in older compilers.

Change-Id: I3eae4dcab59b1fcec0c46f870653d0ae18c4a85f

tests/CMakeLists.txt
tests/common/test_cases_timeout.cpp [new file with mode: 0644]

index 7e2f4d8..045b578 100644 (file)
@@ -26,6 +26,7 @@ PKG_CHECK_MODULES(INNER_TARGET_DEP
 #files to compile
 SET(INNER_TARGET_TEST_SOURCES
     ${PROJECT_SOURCE_DIR}/tests/inner-test.cpp
+    ${PROJECT_SOURCE_DIR}/tests/common/test_cases_timeout.cpp
     )
 
 #header directories
@@ -35,6 +36,7 @@ INCLUDE_DIRECTORIES(SYSTEM
 
 INCLUDE_DIRECTORIES(
     ${PROJECT_SOURCE_DIR}/src/framework/include/
+    ${PROJECT_SOURCE_DIR}/src/
     )
 
 #output format
@@ -43,6 +45,7 @@ ADD_EXECUTABLE(${INNER_TARGET_TEST} ${INNER_TARGET_TEST_SOURCES})
 #linker directories
 TARGET_LINK_LIBRARIES(${INNER_TARGET_TEST}
     ${INNER_TARGET_DEP_LIBRARIES}
+    tests-common
     dpl-test-framework
     )
 
diff --git a/tests/common/test_cases_timeout.cpp b/tests/common/test_cases_timeout.cpp
new file mode 100644 (file)
index 0000000..f11ad41
--- /dev/null
@@ -0,0 +1,151 @@
+/*
+ * 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);
+}