test-timer-list.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  1. /*
  2. * Copyright (c) 2020 Red Hat, Inc.
  3. *
  4. * All rights reserved.
  5. *
  6. * Author: Jan Friesse (jfriesse@redhat.com)
  7. *
  8. * This software licensed under BSD license, the text of which follows:
  9. *
  10. * Redistribution and use in source and binary forms, with or without
  11. * modification, are permitted provided that the following conditions are met:
  12. *
  13. * - Redistributions of source code must retain the above copyright notice,
  14. * this list of conditions and the following disclaimer.
  15. * - Redistributions in binary form must reproduce the above copyright notice,
  16. * this list of conditions and the following disclaimer in the documentation
  17. * and/or other materials provided with the distribution.
  18. * - Neither the name of the Red Hat, Inc. nor the names of its
  19. * contributors may be used to endorse or promote products derived from this
  20. * software without specific prior written permission.
  21. *
  22. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  23. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  24. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  25. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  26. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  27. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  28. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  29. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  30. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  31. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
  32. * THE POSSIBILITY OF SUCH DAMAGE.
  33. */
  34. #include <stdio.h>
  35. #include <assert.h>
  36. #include <string.h>
  37. #include <errno.h>
  38. #include <stdint.h>
  39. #include <poll.h>
  40. #include "timer-list.h"
  41. #define SHORT_TIMEOUT 100
  42. #define LONG_TIMEOUT (60 * 1000)
  43. #define SPEED_TEST_NO_ITEMS 10000
  44. #define HEAP_TEST_NO_ITEMS 20
  45. /*
  46. * Valid heap checking is slow
  47. */
  48. #define HEAP_SPEED_TEST_NO_ITEMS 1000
  49. static int timer_list_fn1_called = 0;
  50. /*
  51. * Reimplementation of timer_list_entry_time_to_expire
  52. */
  53. static uint8_t
  54. time_to_expire(uint8_t expire_time, uint8_t current_time)
  55. {
  56. uint8_t diff, half_interval;
  57. diff = expire_time - current_time;
  58. half_interval = ~0;
  59. half_interval /= 2;
  60. if (diff > half_interval) {
  61. return (0);
  62. }
  63. return (diff);
  64. }
  65. static void
  66. check_time_to_expire(void)
  67. {
  68. unsigned int current_time, delta, expire_time, res;
  69. for (current_time = 0; current_time <= 255 * 2; current_time++) {
  70. for (delta = 0; delta < 255; delta++) {
  71. expire_time = current_time + delta;
  72. res = time_to_expire((uint8_t)expire_time, (uint8_t)current_time);
  73. if (delta < 128) {
  74. assert(res == delta);
  75. } else {
  76. assert(res == 0);
  77. }
  78. }
  79. }
  80. }
  81. static int
  82. timer_list_fn1(void *data1, void *data2)
  83. {
  84. timer_list_fn1_called++;
  85. assert(data1 == &timer_list_fn1_called);
  86. assert(data2 == timer_list_fn1);
  87. return (0);
  88. }
  89. static void
  90. check_timer_list_basics(void)
  91. {
  92. struct timer_list tlist;
  93. struct timer_list_entry *tlist_entry;
  94. struct timer_list_entry *tlist_speed_entry[SPEED_TEST_NO_ITEMS];
  95. int i;
  96. timer_list_init(&tlist);
  97. assert(timer_list_add(&tlist, 0, timer_list_fn1, NULL, NULL) == NULL);
  98. assert(timer_list_add(&tlist, TIMER_LIST_MAX_INTERVAL + 1, timer_list_fn1, NULL, NULL) == NULL);
  99. assert(timer_list_add(&tlist, 1, NULL, NULL, NULL) == NULL);
  100. /*
  101. * callback is called
  102. */
  103. timer_list_fn1_called = 0;
  104. tlist_entry = timer_list_add(&tlist, SHORT_TIMEOUT / 2, timer_list_fn1, &timer_list_fn1_called, timer_list_fn1);
  105. assert(tlist_entry != NULL);
  106. (void)poll(NULL, 0, SHORT_TIMEOUT);
  107. assert(timer_list_time_to_expire(&tlist) == 0);
  108. assert(timer_list_time_to_expire_ms(&tlist) == 0);
  109. timer_list_expire(&tlist);
  110. assert(timer_list_fn1_called == 1);
  111. assert(timer_list_time_to_expire(&tlist) == PR_INTERVAL_NO_TIMEOUT);
  112. assert(timer_list_time_to_expire_ms(&tlist) == ~((uint32_t)0));
  113. timer_list_expire(&tlist);
  114. assert(timer_list_fn1_called == 1);
  115. /*
  116. * Callback is not called
  117. */
  118. timer_list_fn1_called = 0;
  119. tlist_entry = timer_list_add(&tlist, LONG_TIMEOUT, timer_list_fn1, &timer_list_fn1_called, timer_list_fn1);
  120. assert(tlist_entry != NULL);
  121. (void)poll(NULL, 0, SHORT_TIMEOUT);
  122. assert(timer_list_time_to_expire(&tlist) > 0);
  123. assert(timer_list_time_to_expire_ms(&tlist) > 0);
  124. timer_list_expire(&tlist);
  125. assert(timer_list_fn1_called == 0);
  126. /*
  127. * Delete entry
  128. */
  129. timer_list_entry_delete(&tlist, tlist_entry);
  130. assert(timer_list_time_to_expire(&tlist) == PR_INTERVAL_NO_TIMEOUT);
  131. assert(timer_list_time_to_expire_ms(&tlist) == ~((uint32_t)0));
  132. /*
  133. * Check changing of interval
  134. */
  135. timer_list_fn1_called = 0;
  136. tlist_entry = timer_list_add(&tlist, LONG_TIMEOUT, timer_list_fn1, &timer_list_fn1_called, timer_list_fn1);
  137. assert(tlist_entry != NULL);
  138. assert(timer_list_entry_set_interval(&tlist, tlist_entry, SHORT_TIMEOUT) == 0);
  139. (void)poll(NULL, 0, SHORT_TIMEOUT);
  140. assert(timer_list_time_to_expire(&tlist) == 0);
  141. assert(timer_list_time_to_expire_ms(&tlist) == 0);
  142. timer_list_expire(&tlist);
  143. assert(timer_list_fn1_called == 1);
  144. /*
  145. * Test speed and more entries
  146. */
  147. timer_list_fn1_called = 0;
  148. for (i = 0; i < SPEED_TEST_NO_ITEMS; i++) {
  149. tlist_speed_entry[i] = timer_list_add(&tlist, SHORT_TIMEOUT / 2,
  150. timer_list_fn1, &timer_list_fn1_called, timer_list_fn1);
  151. assert(tlist_speed_entry[i] != NULL);
  152. }
  153. for (i = 0; i < SPEED_TEST_NO_ITEMS; i++) {
  154. timer_list_entry_reschedule(&tlist, tlist_speed_entry[i]);
  155. }
  156. (void)poll(NULL, 0, SHORT_TIMEOUT);
  157. timer_list_expire(&tlist);
  158. assert(timer_list_fn1_called == SPEED_TEST_NO_ITEMS);
  159. timer_list_free(&tlist);
  160. }
  161. static void
  162. check_timer_heap(void)
  163. {
  164. struct timer_list tlist;
  165. uint32_t u32;
  166. int i;
  167. int j;
  168. struct timer_list_entry *tlist_entry[HEAP_TEST_NO_ITEMS];
  169. struct timer_list_entry *tlist_entry_small;
  170. struct timer_list_entry *tlist_speed_entry[HEAP_SPEED_TEST_NO_ITEMS];
  171. timer_list_init(&tlist);
  172. /*
  173. * Empty tlist
  174. */
  175. assert(tlist.allocated == 0);
  176. assert(tlist.size == 0);
  177. u32 = ~((uint32_t)0);
  178. assert(timer_list_time_to_expire_ms(&tlist) == u32);
  179. assert(timer_list_time_to_expire(&tlist) == PR_INTERVAL_NO_TIMEOUT);
  180. /*
  181. * Adding increasing numbers keeps heap property so there should be no reshufling
  182. */
  183. for (i = 0; i < HEAP_TEST_NO_ITEMS; i++) {
  184. tlist_entry[i] = timer_list_add(&tlist, LONG_TIMEOUT * (i + 1),
  185. timer_list_fn1, NULL, NULL);
  186. assert(tlist_entry[i] != NULL);
  187. assert(tlist.size == i + 1);
  188. assert(timer_list_debug_is_valid_heap(&tlist));
  189. for (j = 0; j < i + 1; j++) {
  190. assert(tlist.entries[j] == tlist_entry[j]);
  191. }
  192. }
  193. /*
  194. * Add small item which should become first item in tlist entries to keep heap
  195. * property
  196. */
  197. tlist_entry_small = timer_list_add(&tlist, SHORT_TIMEOUT, timer_list_fn1, NULL, NULL);
  198. assert(timer_list_debug_is_valid_heap(&tlist));
  199. assert(tlist.size == i + 1);
  200. assert(tlist.entries[0] == tlist_entry_small);
  201. assert(timer_list_entry_get_interval(tlist_entry_small) == SHORT_TIMEOUT);
  202. /*
  203. * Remove all items
  204. */
  205. for (i = 0; i < HEAP_TEST_NO_ITEMS; i++) {
  206. timer_list_entry_delete(&tlist, tlist_entry[i]);
  207. assert(timer_list_debug_is_valid_heap(&tlist));
  208. assert(tlist.entries[0] == tlist_entry_small);
  209. assert(timer_list_entry_get_interval(tlist_entry[i]) == LONG_TIMEOUT * (i + 1));
  210. }
  211. /*
  212. * Remove small item
  213. */
  214. timer_list_entry_delete(&tlist, tlist_entry_small);
  215. assert(timer_list_debug_is_valid_heap(&tlist));
  216. /*
  217. * Add items in reverse order
  218. */
  219. for (i = 0; i < HEAP_TEST_NO_ITEMS; i++) {
  220. tlist_entry[i] = timer_list_add(&tlist, LONG_TIMEOUT * ((HEAP_TEST_NO_ITEMS - i) + 1),
  221. timer_list_fn1, NULL, NULL);
  222. assert(tlist_entry[i] != NULL);
  223. assert(tlist.size == i + 1);
  224. assert(timer_list_debug_is_valid_heap(&tlist));
  225. }
  226. /*
  227. * Remove all items
  228. */
  229. for (i = 0; i < HEAP_TEST_NO_ITEMS; i++) {
  230. timer_list_entry_delete(&tlist, tlist_entry[i]);
  231. assert(timer_list_debug_is_valid_heap(&tlist));
  232. }
  233. /*
  234. * Add items in standard and reverse order
  235. */
  236. for (i = 0; i < HEAP_TEST_NO_ITEMS / 2; i++) {
  237. tlist_entry[i * 2] = timer_list_add(&tlist, LONG_TIMEOUT * ((HEAP_TEST_NO_ITEMS - i) + 1),
  238. timer_list_fn1, NULL, NULL);
  239. assert(tlist_entry[i] != NULL);
  240. assert(tlist.size == (i * 2) + 1);
  241. tlist_entry[i * 2 + 1] = timer_list_add(&tlist, LONG_TIMEOUT * (i + 1),
  242. timer_list_fn1, NULL, NULL);
  243. assert(tlist_entry[i] != NULL);
  244. assert(tlist.size == (i * 2) + 2);
  245. assert(timer_list_debug_is_valid_heap(&tlist));
  246. }
  247. /*
  248. * Remove items
  249. */
  250. for (i = 0; i < HEAP_TEST_NO_ITEMS; i++) {
  251. timer_list_entry_delete(&tlist, tlist_entry[i]);
  252. assert(timer_list_debug_is_valid_heap(&tlist));
  253. }
  254. assert(tlist.size == 0);
  255. /*
  256. * Add items again
  257. */
  258. for (i = 0; i < HEAP_TEST_NO_ITEMS / 2; i++) {
  259. tlist_entry[i * 2] = timer_list_add(&tlist, LONG_TIMEOUT * ((HEAP_TEST_NO_ITEMS - i) + 1),
  260. timer_list_fn1, NULL, NULL);
  261. assert(tlist_entry[i] != NULL);
  262. assert(tlist.size == (i * 2) + 1);
  263. tlist_entry[i * 2 + 1] = timer_list_add(&tlist, LONG_TIMEOUT * (i + 1),
  264. timer_list_fn1, NULL, NULL);
  265. assert(tlist_entry[i] != NULL);
  266. assert(tlist.size == (i * 2) + 2);
  267. assert(timer_list_debug_is_valid_heap(&tlist));
  268. }
  269. tlist_entry_small = timer_list_add(&tlist, SHORT_TIMEOUT, timer_list_fn1, NULL, NULL);
  270. assert(timer_list_debug_is_valid_heap(&tlist));
  271. assert(tlist.entries[0] == tlist_entry_small);
  272. /*
  273. * And try reschedule
  274. */
  275. for (i = 0; i < HEAP_TEST_NO_ITEMS; i++) {
  276. timer_list_entry_reschedule(&tlist, tlist_entry[i]);
  277. assert(timer_list_debug_is_valid_heap(&tlist));
  278. assert(tlist.entries[0] == tlist_entry_small);
  279. }
  280. /*
  281. * Try delete
  282. */
  283. timer_list_entry_delete(&tlist, tlist_entry_small);
  284. assert(timer_list_debug_is_valid_heap(&tlist));
  285. for (i = 0; i < HEAP_TEST_NO_ITEMS; i++) {
  286. timer_list_entry_delete(&tlist, tlist_entry[i]);
  287. assert(timer_list_debug_is_valid_heap(&tlist));
  288. }
  289. assert(tlist.size == 0);
  290. /*
  291. * Speed test
  292. */
  293. for (i = 0; i < HEAP_SPEED_TEST_NO_ITEMS; i++) {
  294. tlist_speed_entry[i] = timer_list_add(&tlist, SHORT_TIMEOUT / 2,
  295. timer_list_fn1, &timer_list_fn1_called, timer_list_fn1);
  296. assert(tlist_speed_entry[i] != NULL);
  297. assert(timer_list_debug_is_valid_heap(&tlist));
  298. }
  299. for (i = 0; i < HEAP_SPEED_TEST_NO_ITEMS; i++) {
  300. timer_list_entry_reschedule(&tlist, tlist_speed_entry[i]);
  301. assert(timer_list_debug_is_valid_heap(&tlist));
  302. }
  303. /*
  304. * Free list
  305. */
  306. timer_list_free(&tlist);
  307. }
  308. int
  309. main(void)
  310. {
  311. PR_Init(PR_USER_THREAD, PR_PRIORITY_NORMAL, 0);
  312. check_time_to_expire();
  313. check_timer_heap();
  314. check_timer_list_basics();
  315. assert(PR_Cleanup() == PR_SUCCESS);
  316. return (0);
  317. }