13 #ifdef LIBCOTASK_MACRO_ENABLED
15 static int g_test_coroutine_task_manager_status = 0;
16 class test_context_task_manager_action :
public cotask::impl::task_action_impl {
18 int operator()(
void *) {
19 ++g_test_coroutine_task_manager_status;
25 ++g_test_coroutine_task_manager_status;
31 CASE_TEST(coroutine_task_manager, tickspec_t) {
32 cotask::detail::tickspec_t l;
33 cotask::detail::tickspec_t r;
63 CASE_TEST(coroutine_task_manager, task_timer_node) {
64 cotask::detail::task_timer_node<cotask::task<>::id_type> l;
65 cotask::detail::task_timer_node<cotask::task<>::id_type> r;
67 l.expired_time.tv_sec = 123;
68 l.expired_time.tv_nsec = 456;
71 r.expired_time.tv_sec = 123;
72 r.expired_time.tv_nsec = 456;
87 r.expired_time.tv_nsec = 45;
95 r.expired_time.tv_nsec = 456;
103 r.expired_time.tv_nsec = 4567;
112 CASE_TEST(coroutine_task_manager, add_and_timeout) {
114 task_ptr_type co_task = cotask::task<>::create(test_context_task_manager_action());
115 task_ptr_type co_another_task = cotask::task<>::create(test_context_task_manager_action());
117 typedef cotask::task_manager<cotask::task<> >
mgr_t;
118 mgr_t::ptr_t
task_mgr = mgr_t::create();
121 g_test_coroutine_task_manager_status = 0;
124 task_mgr->add_task(co_another_task);
139 CASE_EXPECT_EQ(8, (
int)
task_mgr->get_container().find(co_task->get_id())->second.timer_node->expired_time.tv_sec);
156 task_mgr->start(co_another_task->get_id());
161 task_mgr->resume(co_another_task->get_id());
175 CASE_TEST(coroutine_task_manager, kill) {
177 task_ptr_type co_task = cotask::task<>::create(test_context_task_manager_action());
178 task_ptr_type co_another_task = cotask::task<>::create(test_context_task_manager_action());
180 typedef cotask::task_manager<cotask::task<> >
mgr_t;
181 mgr_t::ptr_t
task_mgr = mgr_t::create();
184 g_test_coroutine_task_manager_status = 0;
188 task_mgr->add_task(co_another_task, 10, 0);
196 task_mgr->cancel(co_another_task->get_id());
207 CASE_TEST(coroutine_task_manager, duplicated_checkpoints) {
209 task_ptr_type co_task = cotask::task<>::create(test_context_task_manager_action());
211 typedef cotask::task_manager<cotask::task<> >
mgr_t;
212 mgr_t::ptr_t
task_mgr = mgr_t::create();
215 g_test_coroutine_task_manager_status = 0;
234 CASE_TEST(coroutine_task_manager, update_timeout) {
236 task_ptr_type co_task = cotask::task<>::create(test_context_task_manager_action());
238 typedef cotask::task_manager<cotask::task<> >
mgr_t;
239 mgr_t::ptr_t
task_mgr = mgr_t::create();
242 g_test_coroutine_task_manager_status = 0;
267 class test_context_task_manager_action_protect_this_task :
public cotask::impl::task_action_impl {
269 int operator()(
void *) {
270 int use_count =
static_cast<int>(cotask::this_task::get<cotask::task<> >()->use_count());
273 use_count =
static_cast<int>(cotask::this_task::get<cotask::task<> >()->use_count());
279 ++g_test_coroutine_task_manager_status;
284 CASE_TEST(coroutine_task_manager, protect_this_task) {
288 typedef cotask::task_manager<cotask::task<> >
mgr_t;
289 mgr_t::ptr_t
task_mgr = mgr_t::create();
291 g_test_coroutine_task_manager_status = 0;
292 task_ptr_type co_task = cotask::task<>::create(test_context_task_manager_action_protect_this_task());
293 cotask::task<>::id_t id_finished = co_task->get_id();
296 co_task = cotask::task<>::create(test_context_task_manager_action_protect_this_task());
297 cotask::task<>::id_t id_unfinished = co_task->get_id();
300 co_task = cotask::task<>::create(test_context_task_manager_action_protect_this_task());
301 cotask::task<>::id_t id_removed = co_task->get_id();
324 static LIBCOPP_COPP_NAMESPACE_ID::util::lock::atomic_int_type<int> g_test_coroutine_task_manager_atomic;
326 static constexpr
const int test_context_task_manager_action_mt_run_times = 10000;
327 enum { test_context_task_manager_action_mt_thread_num = 1000 };
329 struct test_context_task_manager_action_mt_thread :
public cotask::impl::task_action_impl {
331 int operator()(
void *run_count_p) {
333 int *run_count =
reinterpret_cast<int *
>(run_count_p);
335 while (*run_count < test_context_task_manager_action_mt_run_times) {
337 ++g_test_coroutine_task_manager_atomic;
341 run_count =
reinterpret_cast<int *
>(run_count_p);
347 struct test_context_task_manager_mt_thread_runner {
348 typedef cotask::task_manager<cotask::task<> >
mgr_t;
351 test_context_task_manager_mt_thread_runner(mgr_t::ptr_t mgr) : run_count(0),
task_mgr(mgr) {}
357 cotask::task<>::create(test_context_task_manager_action_mt_thread(), 16 * 1024);
358 cotask::task<>::id_t task_id = co_task->get_id();
361 task_mgr->start(task_id, &run_count);
363 while (
false == co_task->is_completed()) {
364 task_mgr->resume(task_id, &run_count);
367 CASE_EXPECT_EQ(test_context_task_manager_action_mt_run_times, run_count);
372 CASE_TEST(coroutine_task_manager, create_and_run_mt) {
373 typedef cotask::task_manager<cotask::task<> >
mgr_t;
374 mgr_t::ptr_t
task_mgr = mgr_t::create();
376 g_test_coroutine_task_manager_atomic.store(0);
378 std::unique_ptr<std::thread> thds[test_context_task_manager_action_mt_thread_num];
379 for (
int i = 0; i < test_context_task_manager_action_mt_thread_num; ++i) {
380 thds[i].reset(
new std::thread(test_context_task_manager_mt_thread_runner(
task_mgr)));
383 for (
int i = 0; i < test_context_task_manager_action_mt_thread_num; ++i) {
387 CASE_EXPECT_EQ(test_context_task_manager_action_mt_run_times * test_context_task_manager_action_mt_thread_num,
388 g_test_coroutine_task_manager_atomic.load());
391 # if defined(LIBCOTASK_MACRO_AUTO_CLEANUP_MANAGER) && LIBCOTASK_MACRO_AUTO_CLEANUP_MANAGER
392 CASE_TEST(coroutine_task_manager, auto_cleanup_for_manager) {
394 task_ptr_type co_task = cotask::task<>::create(test_context_task_manager_action());
396 typedef cotask::task_manager<cotask::task<> >
mgr_t;
397 mgr_t::ptr_t task_mgr1 = mgr_t::create();
398 mgr_t::ptr_t task_mgr2 = mgr_t::create();
414 # if defined(LIBCOPP_MACRO_ENABLE_STD_EXCEPTION_PTR) && LIBCOPP_MACRO_ENABLE_STD_EXCEPTION_PTR
415 class test_context_task_manager_action_with_exception :
public cotask::impl::task_action_impl {
417 int operator()(
void *) {
418 ++g_test_coroutine_task_manager_status;
422 ++g_test_coroutine_task_manager_status;
430 CASE_TEST(coroutine_task_manager, exception_safe) {
432 task_ptr_type co_task = cotask::task<>::create(test_context_task_manager_action_with_exception());
434 typedef cotask::task_manager<cotask::task<> >
mgr_t;
435 mgr_t::ptr_t task_mgr1 = mgr_t::create();
436 mgr_t::ptr_t task_mgr2 = mgr_t::create();
444 int check_status = g_test_coroutine_task_manager_status;
448 }
catch (
const std::exception &e) {
449 CASE_MSG_INFO() <<
"Catch a exception: " << e.what() << std::endl;
452 CASE_EXPECT_EQ(check_status + 2, g_test_coroutine_task_manager_status);
virtual int yield(void **priv_data)=0
CASE_TEST(coroutine_task_manager, disabled)
@ COPP_EC_SUCCESS
COPP_EC_SUCCESS.
@ COPP_EC_TASK_IS_EXITING
COPP_EC_TASK_IS_EXITING.
@ COPP_EC_TASK_ALREADY_IN_ANOTHER_MANAGER
COPP_EC_TASK_ALREADY_IN_ANOTHER_MANAGER.
LIBCOPP_COTASK_API impl::task_impl * get_task() LIBCOPP_MACRO_NOEXCEPT
get current running task
cotask::task_manager< my_task_t > mgr_t
my_task_t::ptr_t task_ptr_type
#define CASE_EXPECT_FALSE(c)
#define CASE_EXPECT_EQ(l, r)
#define CASE_EXPECT_NE(l, r)
#define CASE_EXPECT_TRUE(c)