summaryrefslogtreecommitdiff
path: root/src/tests/ecore/ecore_test_promise.c
diff options
context:
space:
mode:
authorFelipe Magno de Almeida <felipe@expertisesolutions.com.br>2016-04-01 22:24:19 -0300
committerCedric Bail <cedric@osg.samsung.com>2016-04-05 11:35:12 -0700
commitf534fb8943de67a259234a066a7555e9dd131dc6 (patch)
tree5e77d820abe3657bc2f0915b2934acb5af87c354 /src/tests/ecore/ecore_test_promise.c
parent887608e146b8e3952974182b2cc5a7009a711db5 (diff)
ecore: add tests for promises with ecore_thread_promise_run
Signed-off-by: Cedric Bail <cedric@osg.samsung.com>
Diffstat (limited to '')
-rw-r--r--src/tests/ecore/ecore_test_promise.c403
1 files changed, 403 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..7db219c5f1
--- /dev/null
+++ b/src/tests/ecore/ecore_test_promise.c
@@ -0,0 +1,403 @@
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, Eina_Promise_Owner* promise, Ecore_Thread* thread EINA_UNUSED)
10{
11 eina_promise_owner_value_set(promise, NULL, NULL);
12}
13
14void promise_callback(void* data EINA_UNUSED, void* value EINA_UNUSED)
15{
16 ecore_main_loop_quit();
17}
18
19START_TEST(ecore_test_promise)
20{
21 Eina_Promise* promise;
22
23 ecore_init();
24
25 ecore_thread_promise_run(&promised_thread, NULL, NULL, 0, &promise);
26 eina_promise_then(promise, &promise_callback, NULL, NULL);
27
28 ecore_main_loop_begin();
29
30 ecore_shutdown();
31}
32END_TEST
33
34void promise_error_thread(const void* data EINA_UNUSED, Eina_Promise_Owner* promise, Ecore_Thread* thread EINA_UNUSED)
35{
36 eina_promise_owner_error_set(promise, EINA_ERROR_OUT_OF_MEMORY);
37}
38
39void promise_error_callback(void* data EINA_UNUSED, Eina_Error const* error)
40{
41 ck_assert(*error == EINA_ERROR_OUT_OF_MEMORY);
42 ecore_main_loop_quit();
43}
44
45START_TEST(ecore_test_promise_error)
46{
47 Eina_Promise* promise;
48
49 ecore_init();
50
51 ecore_thread_promise_run(&promise_error_thread, NULL, NULL, 0, &promise);
52 eina_promise_then(promise, NULL, &promise_error_callback, NULL);
53
54 ecore_main_loop_begin();
55
56 ecore_shutdown();
57}
58END_TEST
59
60START_TEST(ecore_test_promise_all)
61{
62 Eina_Promise* promise;
63 Eina_Promise* first[2] = { NULL, NULL };
64
65 ecore_init();
66
67 ecore_thread_promise_run(&promised_thread, NULL, NULL, 0, &first[0]);
68 promise = eina_promise_all(eina_carray_iterator_new((void**)&first[0]));
69 eina_promise_then(promise, &promise_callback, NULL, NULL);
70
71 ecore_main_loop_begin();
72
73 ecore_shutdown();
74}
75END_TEST
76
77void promise_callback2(void* data, void* value EINA_UNUSED)
78{
79 if(++(*(int*)data) == 2)
80 ecore_main_loop_quit();
81}
82
83START_TEST(ecore_test_promise_all_then_then)
84{
85 Eina_Promise* promise;
86 Eina_Promise* first[2] = { NULL, NULL };
87
88 ecore_init();
89
90 int i = 0;
91
92 ecore_thread_promise_run(&promised_thread, NULL, NULL, 0, &first[0]);
93 eina_promise_then(first[0], &promise_callback2, NULL, &i);
94 promise = eina_promise_all(eina_carray_iterator_new((void**)&first[0]));
95 eina_promise_then(promise, &promise_callback2, NULL, &i);
96
97 ecore_main_loop_begin();
98
99 ecore_shutdown();
100}
101END_TEST
102
103struct sync_data
104{
105 Eina_Lock lock;
106 Eina_Condition cond;
107 Eina_Bool var;
108};
109
110void promised_exit_thread(struct sync_data* data EINA_UNUSED, Eina_Promise_Owner* promise, Ecore_Thread* thread EINA_UNUSED)
111{
112 eina_promise_owner_value_set(promise, NULL, NULL);
113 eina_lock_take(&data->lock);
114 data->var = EINA_TRUE;
115 eina_condition_broadcast(&data->cond);
116 eina_lock_release(&data->lock);
117}
118
119static void _ecore_test_promise_then_after_thread_finished_main_cb()
120{
121 Eina_Promise* promise;
122 Ecore_Thread* thread;
123 struct sync_data data;
124
125 data.var = EINA_FALSE;
126 eina_lock_new(&data.lock);
127 eina_condition_new(&data.cond, &data.lock);
128
129 thread = ecore_thread_promise_run((Ecore_Thread_Promise_Cb)&promised_exit_thread, NULL, &data, 0, &promise);
130
131 eina_lock_take(&data.lock);
132 while(!data.var)
133 {
134 eina_condition_wait(&data.cond);
135 }
136 eina_lock_release(&data.lock);
137 eina_promise_then(promise, &promise_callback, NULL, NULL);
138
139 while(ecore_thread_wait(thread, 1.0) != EINA_TRUE); // join thread
140}
141
142START_TEST(ecore_test_promise_then_after_thread_finished)
143{
144 ecore_init();
145
146 ecore_job_add(&_ecore_test_promise_then_after_thread_finished_main_cb, NULL);
147 ecore_main_loop_begin();
148
149 ecore_shutdown();
150}
151END_TEST
152
153static void _ecore_test_promise_then_after_thread_finished_all_main_cb()
154{
155 Eina_Promise* first[] = {NULL, NULL};
156 Ecore_Thread* thread;
157 Eina_Promise* promise;
158 struct sync_data data;
159
160 data.var = EINA_FALSE;
161 eina_lock_new(&data.lock);
162 eina_condition_new(&data.cond, &data.lock);
163
164 thread = ecore_thread_promise_run((Ecore_Thread_Promise_Cb)&promised_exit_thread, NULL, &data, 0, &first[0]);
165 promise = eina_promise_all(eina_carray_iterator_new((void**)&first[0]));
166
167 eina_lock_take(&data.lock);
168 while(!data.var)
169 {
170 eina_condition_wait(&data.cond);
171 }
172 eina_lock_release(&data.lock);
173 eina_promise_then(promise, &promise_callback, NULL, NULL);
174
175 while(ecore_thread_wait(thread, 1.0) != EINA_TRUE); // join thread
176}
177
178START_TEST(ecore_test_promise_then_after_thread_finished_all)
179{
180 ecore_init();
181
182 ecore_job_add(&_ecore_test_promise_then_after_thread_finished_all_main_cb, NULL);
183 ecore_main_loop_begin();
184
185 ecore_shutdown();
186}
187END_TEST
188
189void promised_block_thread(const void* data EINA_UNUSED, Eina_Promise_Owner* promise, Ecore_Thread* thread EINA_UNUSED)
190{
191 struct timespec v = {.tv_sec = 1, .tv_nsec = 0}, rem;
192 if(nanosleep(&v, &rem) == -1 && errno == EINTR)
193 do
194 {
195 v = rem;
196 }
197 while(nanosleep(&v, &rem) == -1 && errno == EINTR);
198
199 int r = 10;
200 eina_promise_owner_value_set(promise, &r, NULL);
201}
202
203static void
204_ecore_test_promise_normal_lifetime_cb(void* data EINA_UNUSED, void* value EINA_UNUSED)
205{
206 ecore_main_loop_quit();
207}
208
209static void
210_ecore_test_promise_normal_lifetime_value_set_cb(void* promise_owner)
211{
212 eina_promise_owner_value_set((Eina_Promise_Owner*)promise_owner, NULL, NULL);
213}
214
215START_TEST(ecore_test_promise_normal_lifetime)
216{
217 Eina_Promise_Owner* promise_owner;
218 Eina_Promise* promise;
219
220 ecore_init();
221
222 promise_owner = eina_promise_default_add(0);
223
224 promise = eina_promise_owner_promise_get(promise_owner);
225
226 eina_promise_then(promise, &_ecore_test_promise_normal_lifetime_cb, NULL, NULL);
227 ecore_job_add(_ecore_test_promise_normal_lifetime_value_set_cb, promise_owner);
228
229 ecore_main_loop_begin();
230
231 ecore_shutdown();
232}
233END_TEST
234
235START_TEST(ecore_test_promise_normal_lifetime_all)
236{
237 Eina_Promise_Owner* promise_owner;
238 Eina_Promise* first[2] = {NULL, NULL};
239 Eina_Promise* promise;
240
241 ecore_init();
242
243 promise_owner = eina_promise_default_add(0);
244 first[0] = eina_promise_owner_promise_get(promise_owner);
245 promise = eina_promise_all(eina_carray_iterator_new((void**)&first[0]));
246
247 eina_promise_then(promise, &_ecore_test_promise_normal_lifetime_cb, NULL, NULL);
248 ecore_job_add(_ecore_test_promise_normal_lifetime_value_set_cb, promise_owner);
249
250 ecore_main_loop_begin();
251
252 ecore_shutdown();
253}
254END_TEST
255
256static void
257_ecore_test_promise_immediate_set_lifetime_cb(void* data EINA_UNUSED, void* value EINA_UNUSED)
258{
259 ecore_main_loop_quit();
260}
261
262START_TEST(ecore_test_promise_immediate_set_lifetime)
263{
264 Eina_Promise_Owner* owner;
265 Eina_Promise* promise;
266
267 ecore_init();
268
269 owner = eina_promise_default_add(0);
270 promise = eina_promise_owner_promise_get(owner);
271
272 eina_promise_owner_value_set(owner, NULL, NULL);
273 eina_promise_then(promise, &_ecore_test_promise_immediate_set_lifetime_cb, NULL, NULL);
274
275 ecore_main_loop_begin();
276
277 ecore_shutdown();
278}
279END_TEST
280
281START_TEST(ecore_test_promise_immediate_set_lifetime_all)
282{
283 Eina_Promise_Owner* owner;
284 Eina_Promise* first[2] = {NULL, NULL};
285 Eina_Promise* promise;
286
287 ecore_init();
288
289 owner = eina_promise_default_add(0);
290 first[0] = eina_promise_owner_promise_get(owner);
291 promise = eina_promise_all(eina_carray_iterator_new((void**)&first[0]));
292
293 eina_promise_owner_value_set(owner, NULL, NULL);
294 eina_promise_then(promise, &_ecore_test_promise_immediate_set_lifetime_cb, NULL, NULL);
295
296 ecore_main_loop_begin();
297
298 ecore_shutdown();
299}
300END_TEST
301
302struct _condition_var
303{
304 Eina_Lock lock;
305 Eina_Condition condvar;
306 Eina_Bool boolean;
307};
308typedef struct _condition_var _condition_var;
309
310static void promise_cancel_thread(const void* data, Eina_Promise_Owner* promise EINA_UNUSED, Ecore_Thread* thread EINA_UNUSED)
311{
312 _condition_var* v = (void*)data;
313
314 eina_lock_take(&v->lock);
315 while(!v->boolean)
316 eina_condition_wait(&v->condvar);
317 eina_lock_release(&v->lock);
318}
319
320static void _cancel_callback(const void* data, Eina_Promise_Owner* promise EINA_UNUSED, Ecore_Thread* thread EINA_UNUSED)
321{
322 _condition_var* v = (void*)data;
323
324 eina_lock_take(&v->lock);
325 v->boolean = EINA_TRUE;
326 eina_condition_broadcast(&v->condvar);
327 eina_lock_release(&v->lock);
328}
329
330static void _cancel_promise_callback(void* data EINA_UNUSED, Eina_Error const* value)
331{
332 ck_assert(!!value);
333 ecore_main_loop_quit();
334}
335
336START_TEST(ecore_test_promise_cancel_promise)
337{
338 _condition_var v = {.boolean = EINA_FALSE};
339 Eina_Promise* promise;
340
341 ecore_init();
342
343 eina_lock_new(&v.lock);
344 eina_condition_new(&v.condvar, &v.lock);
345
346 ecore_thread_promise_run(&promise_cancel_thread, &_cancel_callback, &v, 0, &promise);
347 eina_promise_then(promise, NULL, &_cancel_promise_callback, NULL);
348
349 eina_promise_cancel(promise);
350
351 ecore_main_loop_begin();
352
353
354 eina_condition_free(&v.condvar);
355 eina_lock_free(&v.lock);
356 ecore_shutdown();
357}
358END_TEST
359
360static void promise_progress_thread(const void* data EINA_UNUSED,
361 Eina_Promise_Owner* promise, Ecore_Thread* thread EINA_UNUSED)
362{
363 void* v = (void*)1;
364 eina_promise_owner_progress(promise, v);
365}
366
367static void _progress_callback(void* data EINA_UNUSED, void* value)
368{
369 ck_assert(value == (void*)1);
370 ecore_main_loop_quit();
371}
372
373START_TEST(ecore_test_promise_progress_promise)
374{
375 Eina_Promise* promise;
376
377 ecore_init();
378
379 ecore_thread_promise_run(&promise_progress_thread, NULL, NULL, 0, &promise);
380
381 eina_promise_progress_cb_add(promise, &_progress_callback, NULL);
382
383 ecore_main_loop_begin();
384
385 ecore_shutdown();
386}
387END_TEST
388
389void ecore_test_ecore_promise(TCase *tc EINA_UNUSED)
390{
391 tcase_add_test(tc, ecore_test_promise);
392 tcase_add_test(tc, ecore_test_promise_error);
393 tcase_add_test(tc, ecore_test_promise_all);
394 tcase_add_test(tc, ecore_test_promise_all_then_then);
395 tcase_add_test(tc, ecore_test_promise_then_after_thread_finished);
396 tcase_add_test(tc, ecore_test_promise_then_after_thread_finished_all);
397 tcase_add_test(tc, ecore_test_promise_normal_lifetime);
398 tcase_add_test(tc, ecore_test_promise_normal_lifetime_all);
399 tcase_add_test(tc, ecore_test_promise_immediate_set_lifetime);
400 tcase_add_test(tc, ecore_test_promise_immediate_set_lifetime_all);
401 tcase_add_test(tc, ecore_test_promise_cancel_promise);
402 tcase_add_test(tc, ecore_test_promise_progress_promise);
403}