17#ifdef LIBCOTASK_MACRO_ENABLED
18static int g_test_coroutine_task_channel_status = 0;
19static int g_test_coroutine_task_channel_on_finished = 0;
20class test_context_task_channel_action :
public cotask::impl::task_action_impl {
22 test_context_task_channel_action(
bool transform_error =
false) : last_value(0), transform_error_(transform_error) {}
24 copp::stackful_channel_receiver<int> create_channel() {
25 auto ret = copp::make_stackful_channel<int>();
26 sender = copp::memory::make_strong_rc<copp::stackful_channel_sender<int>>(ret.second);
27 return std::move(ret.first);
30 int operator()(
void *priv_data)
override {
31 ++g_test_coroutine_task_channel_status;
35 if (transform_error_) {
37 cotask::task<>::this_task()->await_value(create_channel(), [](copp::copp_error_code) {
return -17; });
39 last_value = cotask::task<>::this_task()->await_value(create_channel());
42 ++g_test_coroutine_task_channel_status;
47 int on_finished(cotask::impl::task_impl &)
override {
48 ++g_test_coroutine_task_channel_on_finished;
52 copp::memory::strong_rc_ptr<copp::stackful_channel_sender<int>> sender;
54 bool transform_error_;
57CASE_TEST(coroutine_task_channel, basic_int_channel) {
59 g_test_coroutine_task_channel_status = 0;
60 g_test_coroutine_task_channel_on_finished = 0;
63 task_ptr_type co_task = cotask::task<>::create(test_context_task_channel_action());
64 task_ptr_type co_another_task = cotask::task<>::create(test_context_task_channel_action(
true));
67 CASE_EXPECT_EQ(cotask::EN_TS_CREATED, co_another_task->get_status());
69 CASE_EXPECT_EQ(0, co_task->start(&g_test_coroutine_task_channel_status));
76 CASE_EXPECT_EQ(0, co_another_task->start(&g_test_coroutine_task_channel_status));
80 CASE_EXPECT_EQ(cotask::EN_TS_WAITING, co_another_task->get_status());
82 static_cast<test_context_task_channel_action *
>(co_task->get_raw_action())->sender->set_value(357);
84 CASE_EXPECT_EQ(
static_cast<test_context_task_channel_action *
>(co_task->get_raw_action())->last_value, 357);
86 static_cast<test_context_task_channel_action *
>(co_another_task->get_raw_action())->sender->reset_value();
87 static_cast<test_context_task_channel_action *
>(co_another_task->get_raw_action())->sender->set_value(579);
89 CASE_EXPECT_EQ(
static_cast<test_context_task_channel_action *
>(co_another_task->get_raw_action())->last_value, 579);
103CASE_TEST(coroutine_task_channel, transform_error_for_int_channel) {
105 g_test_coroutine_task_channel_status = 0;
106 g_test_coroutine_task_channel_on_finished = 0;
109 task_ptr_type co_task = cotask::task<>::create(test_context_task_channel_action());
110 task_ptr_type co_another_task = cotask::task<>::create(test_context_task_channel_action(
true));
113 CASE_EXPECT_EQ(cotask::EN_TS_CREATED, co_another_task->get_status());
115 CASE_EXPECT_EQ(0, co_task->start(&g_test_coroutine_task_channel_status));
122 CASE_EXPECT_EQ(0, co_another_task->start(&g_test_coroutine_task_channel_status));
126 CASE_EXPECT_EQ(cotask::EN_TS_WAITING, co_another_task->get_status());
130 CASE_EXPECT_EQ(
static_cast<test_context_task_channel_action *
>(co_task->get_raw_action())->last_value,
131 copp::COPP_EC_OPERATION_CANCLE);
133 static_cast<test_context_task_channel_action *
>(co_another_task->get_raw_action())->sender->reset_value();
134 co_another_task->kill();
137 CASE_EXPECT_EQ(
static_cast<test_context_task_channel_action *
>(co_another_task->get_raw_action())->last_value, -17);
145 CASE_EXPECT_EQ(co_another_task->get_status(), cotask::EN_TS_KILLED);
my_task_t::ptr_t task_ptr_type
#define CASE_EXPECT_FALSE(c)
#define CASE_EXPECT_EQ(l, r)
#define CASE_TEST(test_name, case_name)
#define CASE_EXPECT_TRUE(c)