summaryrefslogtreecommitdiff
path: root/src/tests/ecore/ecore_test_promise.c
diff options
context:
space:
mode:
authorFelipe Magno de Almeida <felipe@expertisesolutions.com.br>2016-03-06 17:39:20 -0300
committerFelipe Magno de Almeida <felipe@expertisesolutions.com.br>2016-03-06 17:55:33 -0300
commitf9ba80ab33e0b94dad7ec103e6d261a644f7835f (patch)
tree9b730dbf2ba46e47b6b2b7b49c622a021bff1a1c /src/tests/ecore/ecore_test_promise.c
parentd723990955289a5ca62605262f4bc4cd0aa8d25f (diff)
ecore: Create Promises
Add a promise object that allows Eolian interface to include promises as a way to have asynchronous value return and composibility. The usage is like this in a .eo file: class Foo { methods { bar { params { promise: Promise<int>; } } } } Which will create the following API interface: void foo_bar(Ecore_Promise** promise); and the equivalent declaration for implementation. However, the API function will instantiate the Promise for the user and the implementer of the class.
Diffstat (limited to '')
-rw-r--r--src/tests/ecore/ecore_test_promise.c364
1 files changed, 364 insertions, 0 deletions
diff --git a/src/tests/ecore/ecore_test_promise.c b/src/tests/ecore/ecore_test_promise.c
new file mode 100644
index 0000000000..0f003fd669
--- /dev/null
+++ b/src/tests/ecore/ecore_test_promise.c
@@ -0,0 +1,364 @@
1#ifdef HAVE_CONFIG_H
2# include <config.h>
3#endif
4
5#include <Ecore.h>
6#include "ecore_suite.h"
7#include <time.h>
8
9void promised_thread(const void* data EINA_UNUSED, Ecore_Promise* promise)
10{
11 fprintf(stderr, "%s:%d %s\n", __FILE__, __LINE__, __func__); fflush(stderr);
12 ecore_promise_value_set(promise, NULL);
13}
14
15void promise_callback(void* data EINA_UNUSED, void* value EINA_UNUSED)
16{
17 fprintf(stderr, "%s:%d %s\n", __FILE__, __LINE__, __func__); fflush(stderr);
18 ecore_main_loop_quit();
19}
20
21START_TEST(ecore_test_promise)
22{
23 ecore_init();
24 fprintf(stderr, "%s:%d %s ---------- BEGIN test\n", __FILE__, __LINE__, __func__); fflush(stderr);
25
26 fprintf(stderr, "%s:%d %s\n", __FILE__, __LINE__, __func__); fflush(stderr);
27 Ecore_Promise* promise = ecore_promise_thread_run(&promised_thread, NULL, 0);
28 ecore_promise_then(promise, &promise_callback, NULL);
29 fprintf(stderr, "%s:%d %s\n", __FILE__, __LINE__, __func__); fflush(stderr);
30
31 ecore_main_loop_begin();
32
33 fprintf(stderr, "%s:%d %s ---------- END test\n", __FILE__, __LINE__, __func__); fflush(stderr);
34 ecore_shutdown();
35}
36END_TEST
37
38void promise_error_thread(const void* data EINA_UNUSED, Ecore_Promise* promise)
39{
40 fprintf(stderr, "%s:%d %s\n", __FILE__, __LINE__, __func__); fflush(stderr);
41 ecore_promise_error_set(promise, EINA_ERROR_OUT_OF_MEMORY);
42}
43
44void promise_error_callback(void* data EINA_UNUSED, void* value EINA_UNUSED)
45{
46 fprintf(stderr, "%s:%d %s\n", __FILE__, __LINE__, __func__); fflush(stderr);
47 ecore_main_loop_quit();
48}
49
50START_TEST(ecore_test_promise_error)
51{
52 ecore_init();
53 fprintf(stderr, "%s:%d %s ---------- BEGIN test\n", __FILE__, __LINE__, __func__); fflush(stderr);
54
55 fprintf(stderr, "%s:%d %s\n", __FILE__, __LINE__, __func__); fflush(stderr);
56 Ecore_Promise* promise = ecore_promise_thread_run(&promise_error_thread, NULL, 0);
57 ecore_promise_then(promise, &promise_error_callback, NULL);
58 fprintf(stderr, "%s:%d %s\n", __FILE__, __LINE__, __func__); fflush(stderr);
59
60 ecore_main_loop_begin();
61
62 fprintf(stderr, "%s:%d %s ---------- END test\n", __FILE__, __LINE__, __func__); fflush(stderr);
63 ecore_shutdown();
64}
65END_TEST
66
67START_TEST(ecore_test_promise_all)
68{
69 ecore_init();
70 fprintf(stderr, "%s:%d %s ---------- BEGIN test\n", __FILE__, __LINE__, __func__); fflush(stderr);
71
72 fprintf(stderr, "%s:%d %s\n", __FILE__, __LINE__, __func__); fflush(stderr);
73 Ecore_Promise* first[2] = { ecore_promise_thread_run(&promised_thread, NULL, 0), NULL };
74 Ecore_Promise* promise = ecore_promise_all(eina_carray_iterator_new((void**)&first[0]));
75 ecore_promise_then(promise, &promise_callback, NULL);
76 fprintf(stderr, "%s:%d %s\n", __FILE__, __LINE__, __func__); fflush(stderr);
77
78 ecore_main_loop_begin();
79
80 fprintf(stderr, "%s:%d %s ---------- END test\n", __FILE__, __LINE__, __func__); fflush(stderr);
81 ecore_shutdown();
82}
83END_TEST
84
85void promise_callback2(void* data, void* value EINA_UNUSED)
86{
87 fprintf(stderr, "%s:%d %s\n", __FILE__, __LINE__, __func__); fflush(stderr);
88 if(++(*(int*)data) == 2)
89 ecore_main_loop_quit();
90}
91
92START_TEST(ecore_test_promise_all_then_then)
93{
94 ecore_init();
95 fprintf(stderr, "%s:%d %s ---------- BEGIN test\n", __FILE__, __LINE__, __func__); fflush(stderr);
96
97 int i = 0;
98
99 fprintf(stderr, "%s:%d %s\n", __FILE__, __LINE__, __func__); fflush(stderr);
100 Ecore_Promise* first[2] = { ecore_promise_thread_run(&promised_thread, NULL, 0), NULL };
101 ecore_promise_then(first[0], &promise_callback2, &i);
102 Ecore_Promise* promise = ecore_promise_all(eina_carray_iterator_new((void**)&first[0]));
103 ecore_promise_then(promise, &promise_callback2, &i);
104 fprintf(stderr, "%s:%d %s\n", __FILE__, __LINE__, __func__); fflush(stderr);
105
106 ecore_main_loop_begin();
107
108 fprintf(stderr, "%s:%d %s ---------- END test\n", __FILE__, __LINE__, __func__); fflush(stderr);
109 ecore_shutdown();
110}
111END_TEST
112
113struct sync_data
114{
115 Eina_Lock lock;
116 Eina_Condition cond;
117 Eina_Bool var;
118};
119
120void promised_exit_thread(struct sync_data* data EINA_UNUSED, Ecore_Promise* promise)
121{
122 fprintf(stderr, "%s:%d %s\n", __FILE__, __LINE__, __func__); fflush(stderr);
123 ecore_promise_value_set(promise, NULL);
124 eina_lock_take(&data->lock);
125 data->var = EINA_TRUE;
126 eina_condition_broadcast(&data->cond);
127 eina_lock_release(&data->lock);
128}
129
130static void _ecore_test_promise_then_after_thread_finished_main_cb()
131{
132 struct sync_data data;
133 data.var = EINA_FALSE;
134 eina_lock_new(&data.lock);
135 eina_condition_new(&data.cond, &data.lock);
136
137 Ecore_Promise* promise = ecore_promise_thread_run((Ecore_Promise_Thread_Cb)&promised_exit_thread, &data, 0);
138
139 eina_lock_take(&data.lock);
140 while(!data.var)
141 {
142 eina_condition_wait(&data.cond);
143 }
144 eina_lock_release(&data.lock);
145 ecore_promise_then(promise, &promise_callback, NULL);
146}
147
148START_TEST(ecore_test_promise_then_after_thread_finished)
149{
150 ecore_init();
151 fprintf(stderr, "%s:%d %s ---------- BEGIN test\n", __FILE__, __LINE__, __func__); fflush(stderr);
152
153 ecore_job_add(&_ecore_test_promise_then_after_thread_finished_main_cb, NULL);
154 ecore_main_loop_begin();
155
156 fprintf(stderr, "%s:%d %s ---------- END test\n", __FILE__, __LINE__, __func__); fflush(stderr);
157 ecore_shutdown();
158}
159END_TEST
160
161static void _ecore_test_promise_then_after_thread_finished_all_main_cb()
162{
163 struct sync_data data;
164 data.var = EINA_FALSE;
165 eina_lock_new(&data.lock);
166 eina_condition_new(&data.cond, &data.lock);
167
168 Ecore_Promise* first[] = {ecore_promise_thread_run((Ecore_Promise_Thread_Cb)&promised_exit_thread, &data, 0), NULL};
169 Ecore_Promise* promise = ecore_promise_all(eina_carray_iterator_new((void**)&first[0]));
170
171 eina_lock_take(&data.lock);
172 while(!data.var)
173 {
174 eina_condition_wait(&data.cond);
175 }
176 eina_lock_release(&data.lock);
177 ecore_promise_then(promise, &promise_callback, NULL);
178}
179
180START_TEST(ecore_test_promise_then_after_thread_finished_all)
181{
182 ecore_init();
183 fprintf(stderr, "%s:%d %s ---------- BEGIN test\n", __FILE__, __LINE__, __func__); fflush(stderr);
184
185 ecore_job_add(&_ecore_test_promise_then_after_thread_finished_all_main_cb, NULL);
186 ecore_main_loop_begin();
187
188 fprintf(stderr, "%s:%d %s ---------- END test\n", __FILE__, __LINE__, __func__); fflush(stderr);
189 ecore_shutdown();
190}
191END_TEST
192
193void promised_block_thread(const void* data EINA_UNUSED, Ecore_Promise* promise)
194{
195 struct timespec v = {.tv_sec = 1, .tv_nsec = 0}, rem;
196 if(nanosleep(&v, &rem) == -1 && errno == EINTR)
197 do
198 {
199 v = rem;
200 }
201 while(nanosleep(&v, &rem) == -1 && errno == EINTR);
202
203 int r = 10;
204 ecore_promise_value_set(promise, &r);
205}
206
207static void
208_ecore_test_promise_blocking_get_quit_cb(void* data EINA_UNUSED)
209{
210 ecore_main_loop_quit();
211}
212
213static void
214_ecore_test_promise_blocking_get_main_cb(void* data EINA_UNUSED)
215{
216 Ecore_Promise* promise = ecore_promise_thread_run(&promised_block_thread, NULL, sizeof(int));
217 const void* value = ecore_promise_value_get(promise);
218 ck_assert(*(int*)value == 10);
219
220 ecore_job_add(&_ecore_test_promise_blocking_get_quit_cb, NULL);
221}
222
223START_TEST(ecore_test_promise_blocking_get)
224{
225 ecore_init();
226 fprintf(stderr, "%s:%d %s ---------- BEGIN test\n", __FILE__, __LINE__, __func__); fflush(stderr);
227
228 ecore_job_add(&_ecore_test_promise_blocking_get_main_cb, NULL);
229 ecore_main_loop_begin();
230
231 fprintf(stderr, "%s:%d %s ---------- END test\n", __FILE__, __LINE__, __func__); fflush(stderr);
232 ecore_shutdown();
233}
234END_TEST
235
236static void
237_ecore_test_promise_blocking_get_all_value_get_cb(Ecore_Promise* promise, Ecore_Thread* thread EINA_UNUSED)
238{
239 Eina_Iterator** iterator = ecore_promise_value_get(promise);
240 int* v;
241 ck_assert(eina_iterator_next(*iterator, (void**)&v));
242 ck_assert(*v == 10);
243 ecore_main_loop_quit();
244}
245
246static void
247_ecore_test_promise_blocking_get_all_main_cb(void* data EINA_UNUSED)
248{
249 Ecore_Promise* first[2] = {ecore_promise_thread_run(&promised_block_thread, NULL, sizeof(int)), NULL};
250 Ecore_Promise* promise = ecore_promise_all(eina_carray_iterator_new((void**)&first[0]));
251
252 ecore_thread_run((Ecore_Thread_Cb)&_ecore_test_promise_blocking_get_all_value_get_cb, NULL, NULL, promise);
253}
254
255START_TEST(ecore_test_promise_blocking_get_all)
256{
257 ecore_init();
258 fprintf(stderr, "%s:%d %s ---------- BEGIN test\n", __FILE__, __LINE__, __func__); fflush(stderr);
259
260 ecore_job_add(&_ecore_test_promise_blocking_get_all_main_cb, NULL);
261 ecore_main_loop_begin();
262
263 fprintf(stderr, "%s:%d %s ---------- END test\n", __FILE__, __LINE__, __func__); fflush(stderr);
264 ecore_shutdown();
265}
266END_TEST
267
268static void
269_ecore_test_promise_normal_lifetime_cb(void* data EINA_UNUSED, void* value EINA_UNUSED)
270{
271 ecore_main_loop_quit();
272}
273
274START_TEST(ecore_test_promise_normal_lifetime)
275{
276 ecore_init();
277 fprintf(stderr, "%s:%d %s ---------- BEGIN test\n", __FILE__, __LINE__, __func__); fflush(stderr);
278
279 Ecore_Promise* promise = ecore_promise_add(0);
280
281 ecore_promise_then(promise, &_ecore_test_promise_normal_lifetime_cb, NULL);
282 ecore_promise_value_set(promise, NULL);
283
284 ecore_main_loop_begin();
285
286 fprintf(stderr, "%s:%d %s ---------- END test\n", __FILE__, __LINE__, __func__); fflush(stderr);
287 ecore_shutdown();
288}
289END_TEST
290
291START_TEST(ecore_test_promise_normal_lifetime_all)
292{
293 ecore_init();
294 fprintf(stderr, "%s:%d %s ---------- BEGIN test\n", __FILE__, __LINE__, __func__); fflush(stderr);
295
296 Ecore_Promise* first[2] = {ecore_promise_add(0), NULL};
297 Ecore_Promise* promise = ecore_promise_all(eina_carray_iterator_new((void**)&first[0]));
298
299 ecore_promise_then(promise, &_ecore_test_promise_normal_lifetime_cb, NULL);
300 ecore_promise_value_set(promise, NULL);
301
302 ecore_main_loop_begin();
303
304 fprintf(stderr, "%s:%d %s ---------- END test\n", __FILE__, __LINE__, __func__); fflush(stderr);
305 ecore_shutdown();
306}
307END_TEST
308
309static void
310_ecore_test_promise_immediate_set_lifetime_cb(void* data EINA_UNUSED, void* value EINA_UNUSED)
311{
312 ecore_main_loop_quit();
313}
314
315START_TEST(ecore_test_promise_immediate_set_lifetime)
316{
317 ecore_init();
318 fprintf(stderr, "%s:%d %s ---------- BEGIN test\n", __FILE__, __LINE__, __func__); fflush(stderr);
319
320 Ecore_Promise* promise = ecore_promise_add(0);
321
322 ecore_promise_value_set(promise, NULL);
323 ecore_promise_then(promise, &_ecore_test_promise_immediate_set_lifetime_cb, NULL);
324
325 ecore_main_loop_begin();
326
327 fprintf(stderr, "%s:%d %s ---------- END test\n", __FILE__, __LINE__, __func__); fflush(stderr);
328 ecore_shutdown();
329}
330END_TEST
331
332START_TEST(ecore_test_promise_immediate_set_lifetime_all)
333{
334 ecore_init();
335 fprintf(stderr, "%s:%d %s ---------- BEGIN test\n", __FILE__, __LINE__, __func__); fflush(stderr);
336
337 Ecore_Promise* first[2] = {ecore_promise_add(0), NULL};
338 Ecore_Promise* promise = ecore_promise_all(eina_carray_iterator_new((void**)&first[0]));
339
340 ecore_promise_value_set(first[0], NULL);
341 ecore_promise_then(promise, &_ecore_test_promise_immediate_set_lifetime_cb, NULL);
342
343 ecore_main_loop_begin();
344
345 fprintf(stderr, "%s:%d %s ---------- END test\n", __FILE__, __LINE__, __func__); fflush(stderr);
346 ecore_shutdown();
347}
348END_TEST
349
350void ecore_test_ecore_promise(TCase *tc EINA_UNUSED)
351{
352 tcase_add_test(tc, ecore_test_promise);
353 tcase_add_test(tc, ecore_test_promise_error);
354 tcase_add_test(tc, ecore_test_promise_all);
355 tcase_add_test(tc, ecore_test_promise_all_then_then);
356 tcase_add_test(tc, ecore_test_promise_then_after_thread_finished);
357 tcase_add_test(tc, ecore_test_promise_then_after_thread_finished_all);
358 tcase_add_test(tc, ecore_test_promise_blocking_get);
359 tcase_add_test(tc, ecore_test_promise_blocking_get_all);
360 tcase_add_test(tc, ecore_test_promise_normal_lifetime);
361 tcase_add_test(tc, ecore_test_promise_normal_lifetime_all);
362 tcase_add_test(tc, ecore_test_promise_immediate_set_lifetime);
363 tcase_add_test(tc, ecore_test_promise_immediate_set_lifetime_all);
364}