libcopp  1.2.0
coroutine_context_base_test.cpp
Go to the documentation of this file.
1 #include <cstdio>
2 #include <cstring>
3 #include <iostream>
4 
5 #include "frame/test_macros.h"
7 
8 
10 
12 
14 public:
17  int operator()(void *) {
18  ++call_times;
20  copp::this_coroutine::get<test_context_base_coroutine_context_test_type>()->yield();
21 
23  return 0;
24  }
25 };
26 
27 CASE_TEST(coroutine, context_base) {
28  unsigned char *stack_buff = new unsigned char[128 * 1024];
32 
34  {
35  copp::stack_context test_move_alloc;
36 
37  copp::allocator::stack_allocator_memory alloc_created(stack_buff, 128 * 1024);
38  copp::allocator::stack_allocator_memory alloc(alloc_created);
39 
40  alloc_created.allocate(test_move_alloc, 64 * 1024);
41  CASE_EXPECT_EQ(NULL, test_move_alloc.sp);
42 
44  runner.call_times = 0;
45 
46  CASE_EXPECT_TRUE(!!co);
47 
49 
50  co->start();
51 
54  co->resume();
55 
58 
59  CASE_EXPECT_EQ(::copp::COPP_EC_NOT_READY, co->resume());
61  }
62 
63  {
65  copp::stack_context test_move_alloc;
66 
68  alloc_created.attach(stack_buff, 128 * 1024);
70  alloc = alloc_created;
71 
72  alloc_created.allocate(test_move_alloc, 64 * 1024);
73  CASE_EXPECT_EQ(NULL, test_move_alloc.sp);
74 
75 
77  runner.call_times = 0;
78 
79  CASE_EXPECT_TRUE(!!co);
80 
82 
83  co->start();
84 
87  co->resume();
88 
91 
92  CASE_EXPECT_EQ(::copp::COPP_EC_NOT_READY, co->resume());
94  }
95 
96  delete[] stack_buff;
97 }
98 
99 
100 CASE_TEST(coroutine, shared_runner) {
101  const int stack_len = 128 * 1024;
102  unsigned char *stack_buff = new unsigned char[4 * stack_len];
106 
108  runner.call_times = 0;
109 
110  {
112  for (int i = 0; i < 4; ++i) {
113  copp::allocator::stack_allocator_memory alloc(stack_buff + i * stack_len, stack_len);
115  co[i]->start();
116  }
117 
119 
120  for (int i = 0; i < 4; ++i) {
121  co[i]->resume();
122  }
123 
125 
126  CASE_EXPECT_EQ(runner.call_times, 4);
127  }
128 
129  delete[] stack_buff;
130 }
131 
132 CASE_TEST(coroutine, coroutine_context_container_create_failed) {
133  unsigned char *stack_buff = new unsigned char[128 * 1024];
134 
136  {
137  copp::stack_context test_move_alloc;
138 
139  copp::allocator::stack_allocator_memory alloc_created(stack_buff, 128 * 1024);
140  copp::allocator::stack_allocator_memory alloc(alloc_created);
141 
142  alloc_created.allocate(test_move_alloc, 64 * 1024);
143  CASE_EXPECT_EQ(NULL, test_move_alloc.sp);
144 
146  test_context_base_coroutine_context_test_type::create(&runner, alloc, 32 * 1024, 16 * 1024, 16 * 1024);
147  CASE_EXPECT_TRUE(!co);
148  }
149 
150  delete[] stack_buff;
151 }
152 
153 
154 CASE_TEST(coroutine, coroutine_context_create_failed) {
155  unsigned char *stack_buff = new unsigned char[128 * 1024];
156 
157  copp::coroutine_context * placement_new_addr = reinterpret_cast<copp::coroutine_context *>(stack_buff + 112 * 1024);
159  {
161  copp::stack_context callee_stack;
162  callee_stack.sp = reinterpret_cast<void *>(stack_buff + 120 * 1024);
163  callee_stack.size = 120 * 1024;
164 
165  CASE_EXPECT_EQ(copp::COPP_EC_ARGS_ERROR, copp::coroutine_context::create(NULL, callback, callee_stack, 4096, 4096));
166  }
167 
168  {
170  copp::stack_context callee_stack;
171  callee_stack.sp = reinterpret_cast<void *>(stack_buff + 120 * 1024);
172  callee_stack.size = 120 * 1024;
173 
174  CASE_EXPECT_EQ(copp::COPP_EC_ARGS_ERROR, copp::coroutine_context::create(placement_new_addr, callback, callee_stack, 4096, 4095));
175  }
176 
177  {
179  copp::stack_context callee_stack;
180  callee_stack.sp = reinterpret_cast<void *>(stack_buff + 120 * 1024);
181  callee_stack.size = 120 * 1024;
182 
183  CASE_EXPECT_EQ(copp::COPP_EC_ARGS_ERROR, copp::coroutine_context::create(placement_new_addr, callback, callee_stack, 4095, 4096));
184  }
185 
186  {
188  copp::stack_context callee_stack;
189  callee_stack.sp = NULL;
190  callee_stack.size = 120 * 1024;
191 
192  CASE_EXPECT_EQ(copp::COPP_EC_ARGS_ERROR, copp::coroutine_context::create(placement_new_addr, callback, callee_stack, 4096, 4096));
193  }
194 
195  {
197  copp::stack_context callee_stack;
198  callee_stack.sp = reinterpret_cast<void *>(stack_buff + 120 * 1024);
199  callee_stack.size = 8192;
200 
201  CASE_EXPECT_EQ(copp::COPP_EC_ARGS_ERROR, copp::coroutine_context::create(placement_new_addr, callback, callee_stack, 4096, 4096));
202  }
203 
204  {
206  copp::stack_context callee_stack;
207  callee_stack.sp = reinterpret_cast<void *>(stack_buff + 120 * 1024);
208  callee_stack.size = 120 * 1024;
209 
210  copp::coroutine_context *placement_invalid_addr = reinterpret_cast<copp::coroutine_context *>(stack_buff + 116 * 1024);
212  copp::coroutine_context::create(placement_invalid_addr, callback, callee_stack, 4096, 4096));
213  }
214 
215  delete[] stack_buff;
216 }
std::function< int(void *)> callback_t
COPP_EC_ALREADY_EXIST.
Definition: errno.h:29
static int create(coroutine_context *p, callback_t &runner, const stack_context &callee_stack, size_t coroutine_size, size_t private_buffer_size) UTIL_CONFIG_NOEXCEPT
create coroutine context at stack context callee_
memory allocator this allocator will return address of specified memory section
void allocate(stack_context &ctx, std::size_t size) UTIL_CONFIG_NOEXCEPT
COPP_EC_NOT_RUNNING.
Definition: errno.h:25
static ptr_t create(const callback_t &runner, allocator_type &alloc, size_t stack_sz=0, size_t private_buffer_size=0, size_t coroutine_size=0) UTIL_CONFIG_NOEXCEPT
create and init coroutine with specify runner and specify stack size
COPP_EC_NOT_READY.
Definition: errno.h:24
#define CASE_EXPECT_EQ(l, r)
Definition: test_macros.h:62
#define CASE_EXPECT_TRUE(c)
Definition: test_macros.h:60
void * sp
stack size
Definition: stack_context.h:14
CASE_TEST(coroutine, context_base)
copp::coroutine_context_container< copp::allocator::stack_allocator_memory > test_context_base_coroutine_context_test_type
static int g_test_coroutine_base_status
#define COPP_MACRO_STD_MOVE(x)
Definition: features.h:171
int yield(void **priv_data=UTIL_CONFIG_NULLPTR)
yield current coroutine
coroutine container contain stack context, stack allocator and runtime fcontext
void attach(void *start_ptr, std::size_t max_size) UTIL_CONFIG_NOEXCEPT
base type of all coroutine context
COPP_EC_ARGS_ERROR.
Definition: errno.h:30