testc_sched.c 4.3 KB

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