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 * @author Lukasz Wojciechowski <l.wojciechow@partner.samsung.com>
19 * @brief Definition of time limited execution of synchronous functions
28 #include <type_traits>
31 #include <tests_common.h>
35 template <class Rep, class Period>
36 using Timeout = std::chrono::duration<Rep, Period>;
38 typedef std::function<void(void)> CancelFunction;
46 std::ostream& operator<<(std::ostream& os, const std::future_status &status)
49 case std::future_status::ready:
52 case std::future_status::timeout:
55 case std::future_status::deferred:
59 os << " [" << static_cast<int>(status) << "]";
63 template <class Rep, class Period, class F, class... Args>
64 typename std::result_of<F(Args...)>::type
65 callAndWait(const Timeout<Rep, Period> &timeout,
67 CancelFunction cancelFunction,
71 auto fut = std::async(std::launch::async, function, std::forward<Args>(args)...);
72 std::future_status status = fut.wait_for(timeout);
74 if (status == std::future_status::timeout && cancelFunction)
79 RUNNER_ASSERT_MSG(status == std::future_status::ready,
80 "expected future status is " << std::future_status::ready
81 << " received future status is " << status);
84 RUNNER_ASSERT_MSG(status == std::future_status::timeout,
85 "expected future status is " << std::future_status::timeout
86 << " received future status is " << status);
95 } // namespace Timeout