summaryrefslogtreecommitdiff
path: root/src/tests/ecore/ecore_test_promise2.c
blob: d91efdde681449776ebe774374304a6cae04c1e3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif

#include <Ecore.h>
#include <Eina.h>
#include <stdlib.h>
#include <errno.h>
#include <time.h>
#include <Eo.h>
#include "ecore_suite.h"

#define CHAIN_SIZE (3)
#define DEFAULT_ERROR (EFBIG)
#define DEFAULT_MSG ("Future resolve is working!")
#define DEFAULT_INT_VALUE (5466)
#define DEFAULT_INT_VALUE_AS_STRING ("5466")
#define DEFAULT_TIMEOUT (0.1)

#define VALUE_TYPE_CHECK(_v, _type)                     \
  do {                                                  \
     ck_assert_ptr_eq(_v.type, _type);                  \
} while(0)

#define ERROR_CHECK(_v, _errno)                                 \
  do {                                                          \
     Eina_Error _err;                                           \
     VALUE_TYPE_CHECK(_v, EINA_VALUE_TYPE_ERROR);               \
     fail_if(!eina_value_get(&_v, &_err));                      \
     ck_assert_int_eq(_err, _errno);                            \
  } while (0)

typedef struct _PromiseCtx {
   Eina_Promise *p;
   Ecore_Timer *t;
   Eina_Bool fail;
   Eina_Value *value;
} PromiseCtx;

typedef struct _Easy_Ctx {
   Eina_Bool success_called;
   Eina_Bool error_called;
   Eina_Bool free_called;
   Eina_Bool stop_loop;
} Easy_Ctx;

typedef struct _Race_Ctx {
   Eina_Value value;
   unsigned int success_idx;
   unsigned int failed;
   unsigned int success;
} Race_Ctx;

typedef struct _Race_Future_Ctx {
   Race_Ctx *race_ctx;
   unsigned int idx;
   int value;
} Race_Future_Ctx;

static void
_cancel(void *data, const Eina_Promise *dead_ptr EINA_UNUSED)
{
   PromiseCtx *ctx = data;
   if (ctx->t) ecore_timer_del(ctx->t);
   ctx->t = NULL;
   eina_value_free(ctx->value);
   free(ctx);
}

static void
_promise_cancel_test(void *data, const Eina_Promise *dead_ptr EINA_UNUSED)
{
   Eina_Bool *cancel_called = data;
   *cancel_called = EINA_TRUE;
}

static Eina_Bool
_simple_timeout(void *data)
{
   PromiseCtx *ctx = data;

   if (ctx->fail) eina_promise_reject(ctx->p, DEFAULT_ERROR);
   else
     {
        Eina_Value v;

        fail_if(!eina_value_copy(ctx->value, &v));
        eina_promise_resolve(ctx->p, v);
        eina_value_free(ctx->value);
     }
   free(ctx);
   return EINA_FALSE;
}

static Eina_Future_Scheduler *
_future_scheduler_get(void)
{
   return efl_loop_future_scheduler_get(ecore_main_loop_get());
}

static PromiseCtx *
_promise_ctx_new(void)
{
   PromiseCtx *ctx;
   ctx = calloc(1, sizeof(PromiseCtx));
   fail_if(!ctx);
   ctx->p = eina_promise_new(_future_scheduler_get(), _cancel, ctx);
   fail_if(!ctx->p);
   return ctx;
}

static Eina_Future *
_future_get(PromiseCtx *ctx, double timeout)
{
   Eina_Future *f;

   f = eina_future_new(ctx->p);
   fail_if(!f);
   ctx->t = ecore_timer_add(timeout, _simple_timeout, ctx);
   fail_if(!ctx->t);
   return f;
}

static Eina_Future *
_fail_future_get(void)
{
   PromiseCtx *ctx = _promise_ctx_new();
   ctx->fail = EINA_TRUE;
   return _future_get(ctx, DEFAULT_TIMEOUT);
}

static Eina_Future *
_int_future_with_value_and_timeout(int value, double timeout)
{
   PromiseCtx *ctx = _promise_ctx_new();
   ctx->value = eina_value_util_int_new(value);
   fail_if(!ctx->value);
   return _future_get(ctx, timeout);
}

static Eina_Future *
_int_future_get(void)
{
   return _int_future_with_value_and_timeout(DEFAULT_INT_VALUE, DEFAULT_TIMEOUT);
}

static Eina_Future *
_str_future_get(void)
{
   PromiseCtx *ctx = _promise_ctx_new();
   ctx->value = eina_value_util_string_new(DEFAULT_MSG);
   fail_if(!ctx->value);
   return _future_get(ctx, DEFAULT_TIMEOUT);
}

static Eina_Value
_simple_err(void *data EINA_UNUSED, const Eina_Value v, const Eina_Future *dead_future EINA_UNUSED)
{
   ERROR_CHECK(v, DEFAULT_ERROR);
   ecore_main_loop_quit();
   return v;
}

static Eina_Value
_simple_ok(void *data EINA_UNUSED, const Eina_Value v, const Eina_Future *dead_future EINA_UNUSED)
{
   const char *msg;

   VALUE_TYPE_CHECK(v, EINA_VALUE_TYPE_STRING);
   fail_if(!eina_value_get(&v, &msg));
   ck_assert_str_eq(DEFAULT_MSG, msg);
   ecore_main_loop_quit();
   return v;
}

static Eina_Value
_chain_stop(void *data, const Eina_Value v, const Eina_Future *dead_future EINA_UNUSED)
{
   int *i = data;
   fail_if(*i != CHAIN_SIZE);
   ecore_main_loop_quit();
   return v;
}

static Eina_Value
_chain_no_error(void *data, const Eina_Value v, const Eina_Future *dead_future EINA_UNUSED)
{
   Eina_Value new_v;
   static int count = DEFAULT_INT_VALUE;
   int current_i;
   int *i = data;

   VALUE_TYPE_CHECK(v, EINA_VALUE_TYPE_INT);
   fail_if(!eina_value_get(&v, &current_i));
   fail_if(current_i != count++);
   fail_if(!eina_value_setup(&new_v, EINA_VALUE_TYPE_INT));
   fail_if(!eina_value_set(&new_v, count));
   (*i)++;
   return new_v;
}

static Eina_Value
_chain_error(void *data, const Eina_Value v, const Eina_Future *dead_future EINA_UNUSED)
{
   int *i = data;

   ERROR_CHECK(v, DEFAULT_ERROR);
   (*i)++;
   return v;
}

static Eina_Value
_cancel_cb(void *data, const Eina_Value v, const Eina_Future *dead_future EINA_UNUSED)
{
   Eina_Value new_v;
   int *cancel_count = data;

   fail_if(!eina_value_setup(&new_v, EINA_VALUE_TYPE_INT));
   ERROR_CHECK(v, ECANCELED);
   (*cancel_count)++;
   /* Although this function returns an INT Eina_Value, the next
      _cancel_cb must receive a EINA_VALYE_TYPE_ERROR as ECANCELED */
   return new_v;
}

static Eina_Value
_inner_resolve(void *data, const Eina_Value v, const Eina_Future *dead_future EINA_UNUSED)
{
   Eina_Value new_v;
   fail_if(!eina_value_setup(&new_v, EINA_VALUE_TYPE_STRING));
   fail_if(!eina_value_set(&new_v, DEFAULT_MSG));
   eina_promise_resolve(data, new_v);
   return v;
}

static Eina_Value
_inner_fail(void *data, const Eina_Value v, const Eina_Future *dead_future EINA_UNUSED)
{
   eina_promise_reject(data, DEFAULT_ERROR);
   return v;
}

static void
_inner_promise_cancel(void *data EINA_UNUSED, const Eina_Promise *dead_ptr EINA_UNUSED)
{
   //This must never happen...
   fail_if(EINA_FALSE);
}

static Eina_Value
_future_promise_create(void *data, const Eina_Value v EINA_UNUSED, const Eina_Future *dead_future EINA_UNUSED)
{
   Eina_Promise *p;

   p = eina_promise_new(_future_scheduler_get(), _inner_promise_cancel, NULL);
   fail_if(!p);
   eina_future_then(_str_future_get(),
                    data ? _inner_fail : _inner_resolve,
                    p);
   return eina_promise_as_value(p);
}

static Eina_Value
_inner_future_last(void *data, const Eina_Value v, const Eina_Future *dead_future EINA_UNUSED)
{
   if (data)
     ERROR_CHECK(v, DEFAULT_ERROR);
   else
     {
        const char *msg;
        VALUE_TYPE_CHECK(v, EINA_VALUE_TYPE_STRING);
        fail_if(!eina_value_get(&v, &msg));
        ck_assert_str_eq(DEFAULT_MSG, msg);
     }
   ecore_main_loop_quit();
   return v;
}

static Eina_Value
_convert_check(void *data EINA_UNUSED, const Eina_Value v, const Eina_Future *dead_future EINA_UNUSED)
{
   const char *number;
   VALUE_TYPE_CHECK(v, EINA_VALUE_TYPE_STRING);
   fail_if(!eina_value_get(&v, &number));
   ck_assert_str_eq(DEFAULT_INT_VALUE_AS_STRING, number);
   ecore_main_loop_quit();
   return v;
}

static Eina_Value
_easy_success(void *data, const Eina_Value v)
{
   Easy_Ctx *ctx = data;
   const char *msg;

   ctx->success_called = EINA_TRUE;
   VALUE_TYPE_CHECK(v, EINA_VALUE_TYPE_STRING);
   fail_if(!eina_value_get(&v, &msg));
   ck_assert_str_eq(DEFAULT_MSG, msg);
   return v;
}

static Eina_Value
_easy_error(void *data, const Eina_Error err)
{
   Eina_Value v;
   Easy_Ctx *ctx = data;
   fail_if(err != EINVAL);
   fail_if(!eina_value_setup(&v, EINA_VALUE_TYPE_ERROR));
   fail_if(!eina_value_set(&v, err));
   ctx->error_called = EINA_TRUE;
   return v;
}

static void
_easy_free(void *data, const Eina_Future *dead_future EINA_UNUSED)
{
   Easy_Ctx *ctx = data;
   ctx->free_called = EINA_TRUE;
   if (ctx->stop_loop) ecore_main_loop_quit();
}

static Eina_Value
_all_cb(void *data, const Eina_Value array, const Eina_Future *dead EINA_UNUSED)
{
   unsigned int len, i, *expected_len = data;

   VALUE_TYPE_CHECK(array, EINA_VALUE_TYPE_ARRAY);
   len = eina_value_array_count(&array);
   fail_if(len != *expected_len);

   for (i = 0; i < len; i++)
     {
        Eina_Value v;

        fail_if(!eina_value_array_get(&array, i, &v));
        if (i % 2 == 0)
          {
             const char *msg;
             VALUE_TYPE_CHECK(v, EINA_VALUE_TYPE_STRING);
             fail_if(!eina_value_get(&v, &msg));
             ck_assert_str_eq(DEFAULT_MSG, msg);
          }
        else
          {
             int ivalue = 0;
             VALUE_TYPE_CHECK(v, EINA_VALUE_TYPE_INT);
             fail_if(!eina_value_get(&v, &ivalue));
             fail_if(ivalue != DEFAULT_INT_VALUE);
          }
        eina_value_flush(&v);
     }
   ecore_main_loop_quit();
   return array;
}

static Eina_Value
_future_all_count(void *data, const Eina_Value v, const Eina_Future *dead EINA_UNUSED)
{
   unsigned int *futures_called = data;
   (*futures_called)++;
   return v;
}

static Eina_Value
_race_cb(void *data, const Eina_Value v, const Eina_Future *dead EINA_UNUSED)
{
   Race_Future_Ctx *future_ctx = data;
   Race_Ctx *ctx = future_ctx->race_ctx;

   if (v.type == EINA_VALUE_TYPE_ERROR)
     {
        Eina_Error err;
        eina_value_get(&v, &err);
        fail_if(err != ECANCELED);
        ctx->failed++;
     }
   else if (v.type == EINA_VALUE_TYPE_INT)
     {
        int i;
        fail_if(!eina_value_get(&v, &i));
        fail_if(future_ctx->value != i);
        ctx->success++;
        ctx->success_idx = future_ctx->idx;
        fail_if(!eina_value_copy(&v, &ctx->value));
     }
   else fail_if(EINA_TRUE); //This is not supposed to happen!
   free(future_ctx);
   return v;
}

static Eina_Value
_race_end_cb(void *data, const Eina_Value v, const Eina_Future *dead EINA_UNUSED)
{
   Race_Ctx *ctx = data;
   unsigned int idx;
   Eina_Value_Struct *st;
   Eina_Value r;

   VALUE_TYPE_CHECK(v, EINA_VALUE_TYPE_STRUCT);

   st = eina_value_memory_get(&v);
   fail_if(!st);
   fail_if(st->desc != EINA_PROMISE_RACE_STRUCT_DESC);
   fail_if(!eina_value_struct_get(&v, "index", &idx));
   fail_if(idx != ctx->success_idx);
   fail_if(!eina_value_struct_get(&v, "value", &r));
   fail_if(eina_value_compare(&r, &ctx->value));
   eina_value_flush(&r);
   ecore_main_loop_quit();
   return v;
}

static Eina_Value
_promise_empty_done(void *data, const Eina_Value value, const Eina_Future *dead_future EINA_UNUSED)
{
   Eina_Bool *pdone = data;

   if (!value.type) *pdone = EINA_TRUE;

   ecore_main_loop_quit();

   return value;
}

START_TEST(efl_test_timeout)
{
   Eina_Future *f;
   Eina_Bool done = EINA_FALSE;

   fail_if(!ecore_init());
   f = eina_future_then(efl_loop_Eina_FutureXXX_timeout(ecore_main_loop_get(), 0.0001),
                        _promise_empty_done, &done);
   fail_if(!f);
   ecore_main_loop_begin();
   ecore_shutdown();

   fail_unless(done);
}
END_TEST

START_TEST(efl_test_job)
{
   Eina_Future *f;
   Eina_Bool done = EINA_FALSE;

   fail_if(!ecore_init());
   f = eina_future_then(efl_loop_Eina_FutureXXX_job(ecore_main_loop_get()),
                        _promise_empty_done, &done);
   fail_if(!f);
   ecore_main_loop_begin();
   ecore_shutdown();

   fail_unless(done);
}
END_TEST

START_TEST(efl_test_idle)
{
   Eina_Future *f;
   Eina_Bool done = EINA_FALSE;

   fail_if(!ecore_init());
   f = eina_future_then(efl_loop_Eina_FutureXXX_idle(ecore_main_loop_get()),
                        _promise_empty_done, &done);
   fail_if(!f);
   ecore_main_loop_begin();
   ecore_shutdown();

   fail_unless(done);
}
END_TEST


START_TEST(efl_test_promise_future_success)
{
   Eina_Future *f;
   fail_if(!ecore_init());
   f = eina_future_then(_str_future_get(),
                        _simple_ok, NULL);
   fail_if(!f);
   ecore_main_loop_begin();
   ecore_shutdown();
}
END_TEST

START_TEST(efl_test_promise_future_failure)
{
   Eina_Future *f;
   fail_if(!ecore_init());
   f = eina_future_then(_fail_future_get(),
                        _simple_err, NULL);
   fail_if(!f);
   ecore_main_loop_begin();
   ecore_shutdown();
}
END_TEST

START_TEST(efl_test_promise_future_chain_no_error)
{
   Eina_Future *f;
   static int i = 0;

   fail_if(!ecore_init());
   f = eina_future_chain(_int_future_get(),
                         {.cb = _chain_no_error, .data = &i},
                         {.cb = _chain_no_error, .data = &i},
                         {.cb = _chain_no_error, .data = &i},
                         {.cb = _chain_stop, .data = &i});
   fail_if(!f);
   ecore_main_loop_begin();
   ecore_shutdown();
}
END_TEST

START_TEST(efl_test_promise_future_chain_error)
{
   Eina_Future *f;
   static int i = 0;

   fail_if(!ecore_init());
   f = eina_future_chain(_fail_future_get(),
                         {.cb = _chain_error, .data = &i},
                         {.cb = _chain_error, .data = &i},
                         {.cb = _chain_error, .data = &i},
                         {.cb = _chain_stop, .data = &i});
   fail_if(!f);
   ecore_main_loop_begin();
   ecore_shutdown();
}
END_TEST

START_TEST(efl_test_promise_future_cancel)
{
   fail_if(!ecore_init());
   int i;

   for (i = 0; i < 3; i++)
     {
        Eina_Promise *p;
        Eina_Future *first, *last, *middle;
        int cancel_count = 0;
        Eina_Bool cancel_called = EINA_FALSE;

        p = eina_promise_new(_future_scheduler_get(), _promise_cancel_test, &cancel_called);
        fail_if(!p);
        first = eina_future_new(p);
        fail_if(!first);
        if (i == 2)
          {
             Eina_Future *f;
             last = NULL;
             f = eina_future_then(first, _cancel_cb, &cancel_count);
             fail_if(!f);
             middle = eina_future_then(f, _cancel_cb, &cancel_count);
             fail_if(!middle);
             f = eina_future_then(middle, _cancel_cb, &cancel_count);
             fail_if(!f);
          }
        else
          {
             middle = NULL;
             last = eina_future_chain(first,
                                      {.cb = _cancel_cb, .data = &cancel_count},
                                      {.cb = _cancel_cb, .data = &cancel_count},
                                      {.cb = _cancel_cb, .data = &cancel_count});
             fail_if(!last);
          }
        if (i == 0)
          eina_future_cancel(last);
        else if (i == 1)
          eina_future_cancel(first);
        else
          eina_future_cancel(middle);
        fail_if(cancel_count != CHAIN_SIZE);
        fail_if(!cancel_called);
     }
   ecore_shutdown();
}
END_TEST

START_TEST(efl_test_promise_future_inner_promise)
{
   Eina_Future *f;

   fail_if(!ecore_init());
   f = eina_future_chain(_str_future_get(),
                         {.cb = _future_promise_create, .data = NULL},
                         {.cb = _inner_future_last, .data = NULL});
   fail_if(!f);
   ecore_main_loop_begin();
   ecore_shutdown();
}
END_TEST

START_TEST(efl_test_promise_future_inner_promise_fail)
{
   Eina_Future *f;
   void *data =(void *) 0x01;

   fail_if(!ecore_init());
   f = eina_future_chain(_str_future_get(),
                         {.cb = _future_promise_create, .data = data},
                         {.cb = _inner_future_last, .data = data});
   fail_if(!f);
   ecore_main_loop_begin();
   ecore_shutdown();
}
END_TEST

START_TEST(efl_test_promise_future_implicit_cancel)
{
   Eina_Promise *p;
   Eina_Future *f;
   int cancel_count = 0;
   Eina_Bool cancel_called = EINA_FALSE;
   Eina_Value v = EINA_VALUE_EMPTY;

   fail_if(!ecore_init());

   p = eina_promise_new(_future_scheduler_get(), _promise_cancel_test, &cancel_called);
   fail_if(!p);
   f = eina_future_new(p);
   fail_if(!f);
   f = eina_future_chain(f,
                         {.cb = _cancel_cb, .data = &cancel_count},
                         {.cb = _cancel_cb, .data = &cancel_count},
                         {.cb = _cancel_cb, .data = &cancel_count});
   fail_if(!f);
   eina_promise_resolve(p, v);
   /*
     The promise was resolved, but the mainloop is not running.
     Since ecore_shutdown() will be called all the futures must be cancelled
   */
   ecore_shutdown();
   //All the futures were cancelled at this point
   fail_if(cancel_count != CHAIN_SIZE);
   //Cancel should not be called, since we called eina_promise_resolve()
   fail_if(cancel_called);
}
END_TEST

START_TEST(efl_test_promise_future_convert)
{
   Eina_Future *f;

   fail_if(!ecore_init());
   f = eina_future_chain(_int_future_get(),
                         eina_future_cb_convert_to(EINA_VALUE_TYPE_STRING),
                         { .cb = _convert_check, .data = NULL });
   fail_if(!f);
   ecore_main_loop_begin();
   ecore_shutdown();

}
END_TEST

START_TEST(efl_test_promise_future_easy)
{
   Eina_Future *f;
   Easy_Ctx easy1 = { 0 };
   Easy_Ctx easy2 = { 0 };
   Easy_Ctx easy3 = { 0 };

   easy3.stop_loop = EINA_TRUE;
   fail_if(!ecore_init());
   f = eina_future_then_from_desc(_str_future_get(),
                                  eina_future_cb_easy(_easy_success,
                                                      _easy_error,
                                                      _easy_free,
                                                      EINA_VALUE_TYPE_STRING,
                                                      &easy1));
   fail_if(!f);
   f = eina_future_then_easy(f, _easy_success, _easy_error,
                             _easy_free, NULL, &easy2);
   fail_if(!f);
   f = eina_future_chain_easy(f, {_easy_success, _easy_error,
          _easy_free, EINA_VALUE_TYPE_INT, &easy3});
   fail_if(!f);
   ecore_main_loop_begin();
   ecore_shutdown();
   fail_if(!(easy1.success_called && !easy1.error_called && easy1.free_called));
   fail_if(!(easy2.success_called && !easy2.error_called && easy2.free_called));
   fail_if(!(!easy3.success_called && easy3.error_called && easy3.free_called));
}
END_TEST

START_TEST(efl_test_promise_future_all)
{
   Eina_Future *futures[11];
   unsigned int i, futures_called = 0, len = EINA_C_ARRAY_LENGTH(futures);

   fail_if(!ecore_init());
   for (i = 0; i < len - 1; i++)
     {
        Eina_Future *f;
        if (i % 2 == 0)
          f = _str_future_get();
        else
          f = _int_future_get();
        fail_if(!f);
        futures[i] = eina_future_then(f, _future_all_count, &futures_called);
        fail_if(!futures[i]);
     }

   futures[--len] = NULL;
   fail_if(!eina_future_then(eina_future_all_array(futures), _all_cb, &len));
   ecore_main_loop_begin();
   ecore_shutdown();
   fail_if(futures_called != len);
}
END_TEST

START_TEST(efl_test_promise_future_race)
{
   Race_Ctx ctx = { 0 };
   Eina_Future *futures[11];
   unsigned int i, len = EINA_C_ARRAY_LENGTH(futures);
   double timeouts[10] = {
     2.0, 1.0, 0.5, 0.1, 4.5, 2.3, 5.6, 1.0, 0.5, 0.3
   };

   srand(time(NULL));
   fail_if(!ecore_init());
   for (i = 0; i < len - 1; i++)
     {
        Race_Future_Ctx *future_ctx = calloc(1, sizeof(Race_Future_Ctx));
        fail_if(!future_ctx);
        future_ctx->race_ctx = &ctx;
        future_ctx->idx = i;
        future_ctx->value = rand() % RAND_MAX;
        futures[i] = eina_future_then(_int_future_with_value_and_timeout(future_ctx->value, timeouts[i]),
                                      _race_cb, future_ctx);
        fail_if(!futures[i]);
     }

   futures[--len] = NULL;
   fail_if(!eina_future_then(eina_future_race_array(futures),
                             _race_end_cb, &ctx));
   ecore_main_loop_begin();
   eina_value_flush(&ctx.value);
   ecore_shutdown();
   fail_if(ctx.success != 1);
   fail_if(ctx.failed != (len - 1));
}
END_TEST

static Eina_Value
_eo_future1_ok(Eo *eo EINA_UNUSED, const Eina_Value v)
{
   const char *number;

   VALUE_TYPE_CHECK(v, EINA_VALUE_TYPE_STRING);
   fail_if(!eina_value_get(&v, &number));
   ck_assert_str_eq(DEFAULT_INT_VALUE_AS_STRING, number);
   return v;
}

static Eina_Value
_eo_future1_err(Eo *eo EINA_UNUSED, Eina_Error err EINA_UNUSED)
{
   //Should not happen
   fail_if(EINA_TRUE);
}

static Eina_Value
_eo_future2_ok(Eo *eo EINA_UNUSED, const Eina_Value v)
{
   //Should not happen
   fail_if(EINA_TRUE);
   return v;
}

static Eina_Value
_eo_future2_err(Eo *eo EINA_UNUSED, Eina_Error err)
{
   Eina_Value v;

   fail_if(err != EINVAL);
   fail_if(!eina_value_setup(&v, EINA_VALUE_TYPE_INT));
   fail_if(!eina_value_set(&v, DEFAULT_INT_VALUE));
   return v;
}

static void
_eo_future_free(Eo *eo, const Eina_Future *dead EINA_UNUSED)
{
   int *free_called = efl_key_data_get(eo, "free_called");
   (*free_called)++;
}

static Eina_Value
_eo_chain_stop(void *data EINA_UNUSED, const Eina_Value v, const Eina_Future *dead_future EINA_UNUSED)
{
   int ivalue = 0;
   VALUE_TYPE_CHECK(v, EINA_VALUE_TYPE_INT);
   fail_if(!eina_value_get(&v, &ivalue));
   ck_assert_int_eq(ivalue, DEFAULT_INT_VALUE);
   ecore_main_loop_quit();
   return v;
}

START_TEST(efl_test_promise_eo)
{
   Eina_Future *f;
   Eo *obj;
   int free_called = 0;

   fail_if(!efl_object_init());
   fail_if(!ecore_init());

   //Use a random object..
   obj = efl_add(EFL_IO_BUFFER_CLASS, NULL);
   fail_if(!obj);
   efl_key_data_set(obj, "free_called", &free_called);
   f = eina_future_chain(_int_future_get(),
                         eina_future_cb_convert_to(EINA_VALUE_TYPE_STRING),
                         efl_future_cb(obj, _eo_future1_ok, _eo_future1_err, _eo_future_free, EINA_VALUE_TYPE_STRING),
                         efl_future_cb(obj, _eo_future2_ok, _eo_future2_err, _eo_future_free, EINA_VALUE_TYPE_INT),
                         { .cb = _eo_chain_stop });
   fail_if(!f);
   ecore_main_loop_begin();
   efl_unref(obj);
   ecore_shutdown();
   efl_object_shutdown();
   ck_assert_int_eq(free_called, 2);
}
END_TEST

static Eina_Value
_eo_future_link_success(Eo *eo EINA_UNUSED, const Eina_Value v)
{
   //This should never happen
   fail_if(EINA_TRUE);
   return v;
}

static Eina_Value
_eo_future_link_err(Eo *eo, Eina_Error err)
{
   int *err_called = efl_key_data_get(eo, "err_called");
   Eina_Value v;

   fail_if(!err_called);
   ck_assert_int_eq(err, ECANCELED);
   fail_if(!eina_value_setup(&v, EINA_VALUE_TYPE_ERROR));
   fail_if(!eina_value_set(&v, err));
   (*err_called)++;
   return v;
}

static Eina_Value
_eo_link_chain_end(void *data EINA_UNUSED, const Eina_Value v, const Eina_Future *dead EINA_UNUSED)
{
   int *err_called = data;
   VALUE_TYPE_CHECK(v, EINA_VALUE_TYPE_ERROR);
   ERROR_CHECK(v, ECANCELED);
   (*err_called)++;
   return v;
}

START_TEST(efl_test_promise_eo_link)
{
   Eina_Future *f;
   Eo *obj;
   int err_called = 0;

   fail_if(!efl_object_init());
   fail_if(!ecore_init());

   //Use a random object..
   obj = efl_add(EFL_IO_BUFFER_CLASS, NULL);
   fail_if(!obj);

   efl_key_data_set(obj, "err_called", &err_called);
   fail_if(!efl_key_data_get(obj, "err_called"));
   f = efl_future_chain(obj, _int_future_get(),
                        {.success = _eo_future_link_success, .error = _eo_future_link_err},
                        {.success = _eo_future_link_success, .error = _eo_future_link_err},
                        {.success = _eo_future_link_success, .error = _eo_future_link_err},
                        {.success = _eo_future_link_success, .error = _eo_future_link_err},
                        {.success = _eo_future_link_success, .error = _eo_future_link_err});
   fail_if(!f);
   f = eina_future_then(f, _eo_link_chain_end, &err_called);
   fail_if(!f);
   /*
     Since the mainloop is not running and the object is deleted the whole chain must be cancelled.
   */
   efl_unref(obj);
   ecore_shutdown();
   efl_object_shutdown();
   ck_assert_int_eq(err_called, 6);
}
END_TEST

#define CORO_COUNT 10
#define CORO_SLEEP 0.1

static Eina_Value
_coro(void *data, Eina_Coro *coro, Efl_Loop *loop EINA_UNUSED)
{
   int *pi = data;

   for (; *pi < CORO_COUNT; (*pi)++)
     {
        usleep(CORO_SLEEP * 1000000);
        eina_coro_yield_or_return(coro, EINA_VALUE_EMPTY);
     }

   // returned value is an EINA_VALUE_TYPE_PROMISE
   return eina_future_as_value(_str_future_get());
}

static Eina_Bool
_timer_test(void *data)
{
   int *pi = data;
   (*pi)++;

   return EINA_TRUE;
}

START_TEST(efl_test_coro)
{
   Eina_Future *f;
   int coro_count = 0;
   int timer_count = 0;

   fail_if(!ecore_init());
   f = eina_future_then(efl_loop_coro(ecore_main_loop_get(),
                                      EFL_LOOP_CORO_PRIO_IDLE,
                                      &coro_count, _coro, NULL),
                        .cb = _simple_ok);
   fail_if(!f);

   // timer is 2x faster so it will always expire
   ecore_timer_add(CORO_SLEEP / 2, _timer_test, &timer_count);

   ecore_main_loop_begin();
   ecore_shutdown();

   ck_assert_int_eq(coro_count, CORO_COUNT);
   ck_assert_int_ge(timer_count, CORO_COUNT);
}
END_TEST

static Eina_Value
_await(void *data, Eina_Coro *coro, Efl_Loop *loop)
{
   int *pi = data;

   for (; *pi < CORO_COUNT; (*pi)++)
     {
        Eina_Future *f = eina_future_chain(efl_loop_Eina_FutureXXX_timeout(loop, CORO_SLEEP),
                                           // convert to string so we don't get dummy EMPTY...
                                           // happened to me during development :-)
                                           eina_future_cb_convert_to(EINA_VALUE_TYPE_STRING));
        // await will eina_coro_yield() internally.
        Eina_Value v = eina_future_await(f, coro, NULL);
        if (v.type == EINA_VALUE_TYPE_ERROR) return v;
        ck_assert_ptr_eq(v.type, EINA_VALUE_TYPE_STRING); // job delivers EINA_VALUE_EMPTY
     }

   // returned value is an EINA_VALUE_TYPE_PROMISE
   return eina_future_as_value(_str_future_get());
}

START_TEST(efl_test_promise_future_await)
{
   Eina_Future *f;
   int coro_count = 0;
   int timer_count = 0;

   fail_if(!ecore_init());
   f = eina_future_then(efl_loop_coro(ecore_main_loop_get(),
                                      EFL_LOOP_CORO_PRIO_IDLE,
                                      &coro_count, _await, NULL),
                        .cb = _simple_ok);
   fail_if(!f);

   // timer is 2x faster so it will always expire
   ecore_timer_add(CORO_SLEEP / 2, _timer_test, &timer_count);

   ecore_main_loop_begin();
   ecore_shutdown();

   ck_assert_int_eq(coro_count, CORO_COUNT);
   ck_assert_int_ge(timer_count, CORO_COUNT);
}
END_TEST


void ecore_test_ecore_promise2(TCase *tc)
{
   tcase_add_test(tc, efl_test_timeout);
   tcase_add_test(tc, efl_test_job);
   tcase_add_test(tc, efl_test_idle);
   tcase_add_test(tc, efl_test_promise_future_success);
   tcase_add_test(tc, efl_test_promise_future_failure);
   tcase_add_test(tc, efl_test_promise_future_chain_no_error);
   tcase_add_test(tc, efl_test_promise_future_chain_error);
   tcase_add_test(tc, efl_test_promise_future_cancel);
   tcase_add_test(tc, efl_test_promise_future_implicit_cancel);
   tcase_add_test(tc, efl_test_promise_future_inner_promise);
   tcase_add_test(tc, efl_test_promise_future_inner_promise_fail);
   tcase_add_test(tc, efl_test_promise_future_convert);
   tcase_add_test(tc, efl_test_promise_future_easy);
   tcase_add_test(tc, efl_test_promise_future_all);
   tcase_add_test(tc, efl_test_promise_future_race);
   //FIXME: We should move this to EO tests, however they depend on Ecore...
   tcase_add_test(tc, efl_test_promise_eo);
   tcase_add_test(tc, efl_test_promise_eo_link);
   tcase_add_test(tc, efl_test_coro);
   tcase_add_test(tc, efl_test_promise_future_await);
}