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
30 #include <tests_common.h>
34 template <class Rep, class Period>
35 using Timeout = std::chrono::duration<Rep, Period>;
37 template <class Ret, class... Args>
38 using Function = std::function<Ret(Args...)>;
40 typedef std::function<void(void)> CancelFunction;
48 std::ostream& operator<<(std::ostream& os, const std::future_status &status)
51 case std::future_status::ready:
54 case std::future_status::timeout:
57 case std::future_status::deferred:
61 os << " [" << static_cast<int>(status) << "]";
65 template <class Rep, class Period, class Ret, class... Args>
66 Ret callAndWait(const Timeout<Rep, Period> &timeout,
68 CancelFunction cancelFunction,
69 Function<Ret, Args...> function,
71 RUNNER_ASSERT_MSG(function,
72 "not empty function must be passed to callAndWait");
74 std::future<Ret> fut = std::async(std::launch::async, function, std::forward<Args>(args)...);
75 std::future_status status = fut.wait_for(timeout);
77 if (status == std::future_status::timeout && cancelFunction)
82 RUNNER_ASSERT_MSG(status == std::future_status::ready,
83 "expected future status is " << std::future_status::ready
84 << " received future status is " << status);
87 RUNNER_ASSERT_MSG(status == std::future_status::timeout,
88 "expected future status is " << std::future_status::timeout
89 << " received future status is " << status);
98 } // namespace Timeout