summaryrefslogtreecommitdiff
path: root/src/tests/eina/eina_test_promise.c
diff options
context:
space:
mode:
authorLauro Moura <lauromoura@expertisesolutions.com.br>2016-06-07 14:05:15 -0300
committerFelipe Magno de Almeida <felipe@expertisesolutions.com.br>2016-06-09 19:14:14 -0300
commit3b090b808bf668f393d01c87dbfe5071615a7a72 (patch)
tree61ea5a504cd5ea72c6513c824385733cee6474ab /src/tests/eina/eina_test_promise.c
parent7b63ecf4f6d9446e221047bab257910ae7302446 (diff)
eina: Fixes on promises behavior when cancelling
- Free the cancel callbacks on promise delete - Cancelling an ended promise should be a nop - More tests to increase coverage
Diffstat (limited to '')
-rw-r--r--src/tests/eina/eina_test_promise.c241
1 files changed, 240 insertions, 1 deletions
diff --git a/src/tests/eina/eina_test_promise.c b/src/tests/eina/eina_test_promise.c
index 4629869d2c..086b6d5c73 100644
--- a/src/tests/eina/eina_test_promise.c
+++ b/src/tests/eina/eina_test_promise.c
@@ -80,6 +80,42 @@ START_TEST(eina_test_promise_normal_lifetime_all)
80} 80}
81END_TEST 81END_TEST
82 82
83static void
84_eina_test_error_cb(void *data, Eina_Error error)
85{
86 *(int *)data = error;
87}
88
89START_TEST(eina_test_promise_error_set)
90{
91 Eina_Promise_Owner* promise_owner;
92 Eina_Promise* promise;
93 int ran = 0;
94 int error = 0xdeadbeef;
95
96 eina_init();
97
98 promise_owner = eina_promise_value_add(0);
99
100 promise = eina_promise_owner_promise_get(promise_owner);
101
102 eina_promise_ref(promise);
103
104 eina_promise_then(promise, NULL, &_eina_test_error_cb, &ran);
105
106 eina_promise_owner_error_set(promise_owner, error);
107
108 ck_assert(ran == error);
109 ck_assert_int_eq(error, eina_promise_error_get(promise));
110 ck_assert(!eina_promise_pending_is(promise));
111 ck_assert(!eina_promise_owner_cancelled_is(promise_owner));
112
113 eina_promise_unref(promise);
114
115 eina_shutdown();
116}
117END_TEST
118
83START_TEST(eina_test_promise_immediate_set_lifetime) 119START_TEST(eina_test_promise_immediate_set_lifetime)
84{ 120{
85 Eina_Promise_Owner* owner; 121 Eina_Promise_Owner* owner;
@@ -208,7 +244,95 @@ START_TEST(eina_test_promise_cancel_promise)
208 // Finally free the owner 244 // Finally free the owner
209 eina_promise_owner_value_set(owner, NULL, NULL); 245 eina_promise_owner_value_set(owner, NULL, NULL);
210 246
211 ck_assert(ran); 247 eina_shutdown();
248}
249END_TEST
250
251static void
252_cancel_then_callback(void *data, void *value EINA_UNUSED)
253{
254 *(int*)data = 1;
255}
256
257static void
258_cancel_error_callback(void *data, Eina_Error error EINA_UNUSED)
259{
260 *(int*)data = -1;
261}
262
263START_TEST(eina_test_promise_cancel_finished_promise)
264{
265 Eina_Bool cancel_ran = EINA_FALSE;
266 int ran = 0;
267 Eina_Promise_Owner* owner;
268 Eina_Promise* promise;
269
270 eina_init();
271
272 owner = eina_promise_value_add(0);
273 eina_promise_owner_default_cancel_cb_add(owner, &cancel_callback, &cancel_ran, NULL);
274
275 promise = eina_promise_owner_promise_get(owner);
276
277 eina_promise_then(promise, &_cancel_then_callback, &_cancel_error_callback, &ran);
278
279 eina_promise_ref(promise);
280 eina_promise_owner_value_set(owner, NULL, NULL);
281
282 ck_assert(!cancel_ran);
283 ck_assert_int_eq(1, ran);
284 ck_assert(!eina_promise_owner_cancelled_is(owner));
285 ck_assert(!eina_promise_pending_is(promise));
286 ck_assert_int_eq(0, eina_promise_error_get(promise));
287
288 eina_promise_cancel(promise);
289
290 // The conditions should not have been changed.
291 ck_assert(!cancel_ran);
292 ck_assert_int_eq(1, ran);
293 ck_assert(!eina_promise_owner_cancelled_is(owner));
294 ck_assert(!eina_promise_pending_is(promise));
295 ck_assert_int_eq(0, eina_promise_error_get(promise));
296
297 eina_promise_unref(promise);
298
299 eina_shutdown();
300}
301END_TEST
302
303START_TEST(eina_test_promise_double_cancel_promise)
304{
305 Eina_Bool ran = EINA_FALSE, cancel_ran = EINA_FALSE;
306 Eina_Promise_Owner* owner;
307 Eina_Promise* promise;
308
309 eina_init();
310
311 owner = eina_promise_value_add(0);
312 eina_promise_owner_default_cancel_cb_add(owner, &cancel_callback, &cancel_ran, NULL);
313
314 promise = eina_promise_owner_promise_get(owner);
315
316 eina_promise_then(promise, NULL, &_cancel_promise_callback, &ran);
317
318 eina_promise_cancel(promise);
319
320 ck_assert(cancel_ran && ran);
321 ck_assert(eina_promise_owner_cancelled_is(owner));
322 ck_assert(!eina_promise_pending_is(promise));
323 ck_assert_int_eq(EINA_ERROR_PROMISE_CANCEL, eina_promise_error_get(promise));
324
325 cancel_ran = EINA_FALSE;
326 ran = EINA_FALSE;
327
328 eina_promise_cancel(promise);
329
330 ck_assert(!cancel_ran && !ran);
331 ck_assert(eina_promise_owner_cancelled_is(owner));
332 ck_assert(!eina_promise_pending_is(promise));
333
334 // Finally free the owner
335 eina_promise_owner_value_set(owner, NULL, NULL);
212 336
213 eina_shutdown(); 337 eina_shutdown();
214} 338}
@@ -420,6 +544,36 @@ START_TEST(eina_test_pointer_promise_normal_lifetime_all)
420} 544}
421END_TEST 545END_TEST
422 546
547START_TEST(eina_test_pointer_promise_error_set)
548{
549 Eina_Promise_Owner* promise_owner;
550 Eina_Promise* promise;
551 int ran = 0;
552 int error = 0xdeadbeef;
553
554 eina_init();
555
556 promise_owner = eina_promise_add();
557
558 promise = eina_promise_owner_promise_get(promise_owner);
559
560 eina_promise_ref(promise);
561
562 eina_promise_then(promise, NULL, &_eina_test_error_cb, &ran);
563
564 eina_promise_owner_error_set(promise_owner, error);
565
566 ck_assert(ran == error);
567 ck_assert_int_eq(error, eina_promise_error_get(promise));
568 ck_assert(!eina_promise_pending_is(promise));
569 ck_assert(!eina_promise_owner_cancelled_is(promise_owner));
570
571 eina_promise_unref(promise);
572
573 eina_shutdown();
574}
575END_TEST
576
423START_TEST(eina_test_pointer_promise_immediate_set_lifetime) 577START_TEST(eina_test_pointer_promise_immediate_set_lifetime)
424{ 578{
425 Eina_Promise_Owner* owner; 579 Eina_Promise_Owner* owner;
@@ -526,6 +680,84 @@ START_TEST(eina_test_pointer_promise_cancel_promise)
526} 680}
527END_TEST 681END_TEST
528 682
683START_TEST(eina_test_pointer_promise_cancel_finished_promise)
684{
685 Eina_Bool cancel_ran = EINA_FALSE;
686 int ran = 0;
687 Eina_Promise_Owner* owner;
688 Eina_Promise* promise;
689
690 eina_init();
691
692 owner = eina_promise_add();
693 eina_promise_owner_default_cancel_cb_add(owner, &cancel_callback, &cancel_ran, NULL);
694
695 promise = eina_promise_owner_promise_get(owner);
696
697 eina_promise_then(promise, &_cancel_then_callback, &_cancel_error_callback, &ran);
698
699 eina_promise_ref(promise);
700 eina_promise_owner_value_set(owner, NULL, NULL);
701
702 ck_assert(!cancel_ran);
703 ck_assert_int_eq(1, ran);
704 ck_assert(!eina_promise_owner_cancelled_is(owner));
705 ck_assert(!eina_promise_pending_is(promise));
706 ck_assert_int_eq(0, eina_promise_error_get(promise));
707
708 eina_promise_cancel(promise);
709
710 // The conditions should not have been changed.
711 ck_assert(!cancel_ran);
712 ck_assert_int_eq(1, ran);
713 ck_assert(!eina_promise_owner_cancelled_is(owner));
714 ck_assert(!eina_promise_pending_is(promise));
715 ck_assert_int_eq(0, eina_promise_error_get(promise));
716
717 eina_promise_unref(promise);
718
719 eina_shutdown();
720}
721END_TEST
722
723START_TEST(eina_test_pointer_promise_double_cancel_promise)
724{
725 Eina_Bool ran = EINA_FALSE, cancel_ran = EINA_FALSE;
726 Eina_Promise_Owner* owner;
727 Eina_Promise* promise;
728
729 eina_init();
730
731 owner = eina_promise_add();
732 eina_promise_owner_default_cancel_cb_add(owner, &cancel_callback, &cancel_ran, NULL);
733
734 promise = eina_promise_owner_promise_get(owner);
735
736 eina_promise_then(promise, NULL, &_cancel_promise_callback, &ran);
737
738 eina_promise_cancel(promise);
739
740 ck_assert(cancel_ran && ran);
741 ck_assert(eina_promise_owner_cancelled_is(owner));
742 ck_assert(!eina_promise_pending_is(promise));
743 ck_assert_int_eq(EINA_ERROR_PROMISE_CANCEL, eina_promise_error_get(promise));
744
745 cancel_ran = EINA_FALSE;
746 ran = EINA_FALSE;
747
748 eina_promise_cancel(promise);
749
750 ck_assert(!cancel_ran && !ran);
751 ck_assert(eina_promise_owner_cancelled_is(owner));
752 ck_assert(!eina_promise_pending_is(promise));
753
754 // Finally free the owner
755 eina_promise_owner_value_set(owner, NULL, NULL);
756
757 eina_shutdown();
758}
759END_TEST
760
529START_TEST(eina_test_pointer_promise_progress) 761START_TEST(eina_test_pointer_promise_progress)
530{ 762{
531 Eina_Bool progress_ran = EINA_FALSE; 763 Eina_Bool progress_ran = EINA_FALSE;
@@ -668,10 +900,14 @@ eina_test_promise(TCase *tc)
668{ 900{
669 tcase_add_test(tc, eina_test_promise_normal_lifetime); 901 tcase_add_test(tc, eina_test_promise_normal_lifetime);
670 tcase_add_test(tc, eina_test_promise_normal_lifetime_all); 902 tcase_add_test(tc, eina_test_promise_normal_lifetime_all);
903 tcase_add_test(tc, eina_test_promise_error_set);
904 /* tcase_add_test(tc, eina_test_promise_error_set_all); */
671 tcase_add_test(tc, eina_test_promise_immediate_set_lifetime); 905 tcase_add_test(tc, eina_test_promise_immediate_set_lifetime);
672 tcase_add_test(tc, eina_test_promise_immediate_set_lifetime_all); 906 tcase_add_test(tc, eina_test_promise_immediate_set_lifetime_all);
673 tcase_add_test(tc, eina_test_promise_values_all); 907 tcase_add_test(tc, eina_test_promise_values_all);
674 tcase_add_test(tc, eina_test_promise_cancel_promise); 908 tcase_add_test(tc, eina_test_promise_cancel_promise);
909 tcase_add_test(tc, eina_test_promise_cancel_finished_promise);
910 tcase_add_test(tc, eina_test_promise_double_cancel_promise);
675 tcase_add_test(tc, eina_test_promise_progress); 911 tcase_add_test(tc, eina_test_promise_progress);
676 tcase_add_test(tc, eina_test_promise_progress_notify1); 912 tcase_add_test(tc, eina_test_promise_progress_notify1);
677 tcase_add_test(tc, eina_test_promise_progress_notify2); 913 tcase_add_test(tc, eina_test_promise_progress_notify2);
@@ -681,10 +917,13 @@ eina_test_promise(TCase *tc)
681 // pointer 917 // pointer
682 tcase_add_test(tc, eina_test_pointer_promise_normal_lifetime); 918 tcase_add_test(tc, eina_test_pointer_promise_normal_lifetime);
683 tcase_add_test(tc, eina_test_pointer_promise_normal_lifetime_all); 919 tcase_add_test(tc, eina_test_pointer_promise_normal_lifetime_all);
920 tcase_add_test(tc, eina_test_pointer_promise_error_set);
684 tcase_add_test(tc, eina_test_pointer_promise_immediate_set_lifetime); 921 tcase_add_test(tc, eina_test_pointer_promise_immediate_set_lifetime);
685 tcase_add_test(tc, eina_test_pointer_promise_immediate_set_lifetime_all); 922 tcase_add_test(tc, eina_test_pointer_promise_immediate_set_lifetime_all);
686 tcase_add_test(tc, eina_test_pointer_promise_values_all); 923 tcase_add_test(tc, eina_test_pointer_promise_values_all);
687 tcase_add_test(tc, eina_test_pointer_promise_cancel_promise); 924 tcase_add_test(tc, eina_test_pointer_promise_cancel_promise);
925 tcase_add_test(tc, eina_test_pointer_promise_cancel_finished_promise);
926 tcase_add_test(tc, eina_test_pointer_promise_double_cancel_promise);
688 tcase_add_test(tc, eina_test_pointer_promise_progress); 927 tcase_add_test(tc, eina_test_pointer_promise_progress);
689 tcase_add_test(tc, eina_test_pointer_promise_progress_notify1); 928 tcase_add_test(tc, eina_test_pointer_promise_progress_notify1);
690 tcase_add_test(tc, eina_test_pointer_promise_progress_notify2); 929 tcase_add_test(tc, eina_test_pointer_promise_progress_notify2);