7e985abe15f5697ebafa53b9cc06ddddff08f44b
[platform/core/test/security-tests.git] / src / common / timeout.h
1 /*
2  * Copyright (c) 2015 Samsung Electronics Co., Ltd All Rights Reserved
3  *
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
7  *
8  *        http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16 /**
17  * @file        timeout.h
18  * @author      Lukasz Wojciechowski <l.wojciechow@partner.samsung.com>
19  * @brief       Definition of time limited execution of synchronous functions
20  */
21
22 #ifndef TIMEOUT_H
23 #define TIMEOUT_H
24
25 #include <chrono>
26 #include <functional>
27 #include <future>
28 #include <utility>
29
30 #include <tests_common.h>
31
32 namespace Timeout {
33
34 template <class Rep, class Period>
35 using Timeout = std::chrono::duration<Rep, Period>;
36
37 template <class Ret, class... Args>
38 using Function = std::function<Ret(Args...)>;
39
40 typedef std::function<void(void)> CancelFunction;
41
42 enum ExpectMode {
43     FINISHED,
44     TIMEOUT,
45     IGNORE,
46 };
47
48 std::ostream& operator<<(std::ostream& os, const std::future_status &status)
49 {
50     switch (status) {
51         case std::future_status::ready:
52             os << "<READY>";
53             break;
54         case std::future_status::timeout:
55             os << "<TIMEOUT>";
56             break;
57         case std::future_status::deferred:
58             os << "<DEFERRED>";
59             break;
60     }
61     os << " [" << static_cast<int>(status) << "]";
62     return os;
63 }
64
65 template <class Rep, class Period, class Ret, class... Args>
66 Ret callAndWait(const Timeout<Rep, Period> &timeout,
67                  ExpectMode expect,
68                  CancelFunction cancelFunction,
69                  Function<Ret, Args...> function,
70                  Args... args) {
71     RUNNER_ASSERT_MSG(function,
72                          "not empty function must be passed to callAndWait");
73
74     std::future<Ret> fut = std::async(std::launch::async, function, std::forward<Args>(args)...);
75     std::future_status status = fut.wait_for(timeout);
76
77     if (status == std::future_status::timeout && cancelFunction)
78         cancelFunction();
79
80     switch (expect) {
81         case FINISHED:
82             RUNNER_ASSERT_MSG(status == std::future_status::ready,
83                                  "expected future status is " << std::future_status::ready
84                                      << " received future status is " << status);
85             break;
86         case TIMEOUT:
87             RUNNER_ASSERT_MSG(status == std::future_status::timeout,
88                                  "expected future status is " << std::future_status::timeout
89                                      << " received future status is " << status);
90             break;
91         case IGNORE:
92             break;
93     }
94
95     return fut.get();
96 }
97
98 } // namespace Timeout
99
100 #endif // TIMEOUT_H