testc_sched.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. #include <sys/time.h>
  2. #include <time.h>
  3. #include <errno.h>
  4. #include <stdint.h>
  5. #include <stdio.h>
  6. #include "faux/time.h"
  7. #include "faux/sched.h"
  8. int testc_faux_sched_once(void)
  9. {
  10. faux_sched_t *sched = NULL;
  11. long long int nsec = 500000000l;
  12. struct timespec pol_s = {}; // One half of second
  13. struct timespec now = {};
  14. struct timespec t = {};
  15. int id = 78;
  16. char *str = "test";
  17. int e_id = 0;
  18. void *e_str = NULL;
  19. struct timespec twait = {};
  20. faux_ev_t *ev = NULL;
  21. faux_nsec_to_timespec(&pol_s, nsec);
  22. faux_timespec_now(&now);
  23. faux_timespec_sum(&t, &now, &pol_s);
  24. sched = faux_sched_new();
  25. if (!sched)
  26. return -1;
  27. // Schedule event
  28. faux_sched_once(sched, &t, id, str);
  29. // Don't wait so pop must return -1
  30. if (faux_sched_pop(sched))
  31. return -1;
  32. // Get next event interval. It must be greater than 0 and greater
  33. // than full interval (half of second)
  34. if (!faux_sched_next_interval(sched, &twait))
  35. return -1;
  36. if (faux_timespec_cmp(&twait, &(struct timespec){0, 0}) <= 0)
  37. return -1;
  38. if (faux_timespec_cmp(&twait, &pol_s) >= 0)
  39. return -1;
  40. // Wait and get event
  41. nanosleep(&pol_s, NULL); // wait
  42. if (!(ev = faux_sched_pop(sched)))
  43. return -1;
  44. e_id = faux_ev_id(ev);
  45. e_str = faux_ev_data(ev);
  46. if (e_id != id)
  47. return -1;
  48. if (e_str != str)
  49. return -1;
  50. // Schedule event delayed
  51. faux_sched_once(sched, &pol_s, id, str);
  52. // Wait and get event
  53. nanosleep(&pol_s, NULL); // wait
  54. e_str = NULL;
  55. if (!(ev = faux_sched_pop(sched)))
  56. return -1;
  57. e_id = faux_ev_id(ev);
  58. e_str = faux_ev_data(ev);
  59. if (e_id != id)
  60. return -1;
  61. if (e_str != str)
  62. return -1;
  63. faux_sched_free(sched);
  64. return 0;
  65. }
  66. int testc_faux_sched_periodic(void)
  67. {
  68. faux_sched_t *sched = NULL;
  69. long long int nsec = 500000000l;
  70. struct timespec pol_s = {}; // One half of second
  71. struct timespec now = {};
  72. struct timespec t = {};
  73. int id = 78;
  74. char *str = "test";
  75. int e_id = 0;
  76. void *e_str = NULL;
  77. faux_ev_t *ev = NULL;
  78. faux_nsec_to_timespec(&pol_s, nsec);
  79. faux_timespec_now(&now);
  80. faux_timespec_sum(&t, &now, &pol_s);
  81. sched = faux_sched_new();
  82. if (!sched)
  83. return -1;
  84. // Schedule event
  85. faux_sched_periodic_delayed(sched, id, str, &pol_s, 2);
  86. // Don't wait so pop must return -1
  87. if (faux_sched_pop(sched)) {
  88. printf("faux_shed_pop: Immediately event\n");
  89. return -1;
  90. }
  91. // Wait and get one event
  92. nanosleep(&pol_s, NULL); // wait
  93. if (!(ev = faux_sched_pop(sched))) {
  94. printf("faux_shed_pop: Can't get 1/2 event\n");
  95. return -1;
  96. }
  97. e_id = faux_ev_id(ev);
  98. e_str = faux_ev_data(ev);
  99. if (e_id != id)
  100. return -1;
  101. if (e_str != str)
  102. return -1;
  103. if (faux_sched_pop(sched)) { // another event?
  104. printf("faux_shed_pop: Two events at once\n");
  105. return -1;
  106. }
  107. nanosleep(&pol_s, NULL); // wait next time
  108. if (!faux_sched_pop(sched)) {
  109. printf("faux_shed_pop: Can't get 2/2 event\n");
  110. return -1;
  111. }
  112. nanosleep(&pol_s, NULL); // wait third time
  113. if (faux_sched_pop(sched)) { // no events any more
  114. printf("faux_shed_pop: The 3/2 event\n");
  115. return -1;
  116. }
  117. faux_sched_free(sched);
  118. return 0;
  119. }
  120. int testc_faux_sched_infinite(void)
  121. {
  122. faux_sched_t *sched = NULL;
  123. long long int nsec = 500000000l;
  124. struct timespec pol_s = {}; // One half of second
  125. struct timespec now = {};
  126. struct timespec t = {};
  127. int id = 78;
  128. char *str = "test";
  129. int e_id = 0;
  130. void *e_str = NULL;
  131. faux_ev_t *ev = NULL;
  132. faux_nsec_to_timespec(&pol_s, nsec);
  133. faux_timespec_now(&now);
  134. faux_timespec_sum(&t, &now, &pol_s);
  135. sched = faux_sched_new();
  136. if (!sched)
  137. return -1;
  138. // Schedule event
  139. faux_sched_periodic_delayed(sched, id, str, &pol_s,
  140. FAUX_SCHED_INFINITE);
  141. // Don't wait so pop must return -1
  142. if (faux_sched_pop(sched)) {
  143. printf("faux_shed_pop: Immediately event\n");
  144. return -1;
  145. }
  146. // Wait and get one event
  147. nanosleep(&pol_s, NULL); // wait
  148. if (!(ev = faux_sched_pop(sched))) {
  149. printf("faux_shed_pop: Can't get 1 event\n");
  150. return -1;
  151. }
  152. e_id = faux_ev_id(ev);
  153. e_str = faux_ev_data(ev);
  154. if (e_id != id)
  155. return -1;
  156. if (e_str != str)
  157. return -1;
  158. if (faux_sched_pop(sched)) { // another event?
  159. printf("faux_shed_pop: Two events at once\n");
  160. return -1;
  161. }
  162. nanosleep(&pol_s, NULL); // wait next time
  163. if (!faux_sched_pop(sched)) {
  164. printf("faux_shed_pop: Can't get 2 event\n");
  165. return -1;
  166. }
  167. faux_sched_del_all(sched); // Empty the list
  168. nanosleep(&pol_s, NULL); // wait third time
  169. if (faux_sched_pop(sched)) {
  170. printf("faux_shed_pop: Event after empty operation\n");
  171. return -1;
  172. }
  173. faux_sched_free(sched);
  174. return 0;
  175. }