test-timer-list.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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 1000
  44. static int timer_list_fn1_called = 0;
  45. /*
  46. * Reimplementation of timer_list_entry_time_to_expire
  47. */
  48. static uint8_t
  49. time_to_expire(uint8_t expire_time, uint8_t current_time)
  50. {
  51. uint8_t diff, half_interval;
  52. diff = expire_time - current_time;
  53. half_interval = ~0;
  54. half_interval /= 2;
  55. if (diff > half_interval) {
  56. return (0);
  57. }
  58. return (diff);
  59. }
  60. static void
  61. check_time_to_expire(void)
  62. {
  63. unsigned int current_time, delta, expire_time, res;
  64. for (current_time = 0; current_time <= 255 * 2; current_time++) {
  65. for (delta = 0; delta < 255; delta++) {
  66. expire_time = current_time + delta;
  67. res = time_to_expire((uint8_t)expire_time, (uint8_t)current_time);
  68. if (delta < 128) {
  69. assert(res == delta);
  70. } else {
  71. assert(res == 0);
  72. }
  73. }
  74. }
  75. }
  76. static int
  77. timer_list_fn1(void *data1, void *data2)
  78. {
  79. timer_list_fn1_called++;
  80. assert(data1 == &timer_list_fn1_called);
  81. assert(data2 == timer_list_fn1);
  82. return (0);
  83. }
  84. static void
  85. check_timer_list_basics(void)
  86. {
  87. struct timer_list tlist;
  88. struct timer_list_entry *tlist_entry;
  89. struct timer_list_entry *tlist_speed_entry[SPEED_TEST_NO_ITEMS];
  90. int i;
  91. timer_list_init(&tlist);
  92. assert(timer_list_add(&tlist, 0, timer_list_fn1, NULL, NULL) == NULL);
  93. assert(timer_list_add(&tlist, TIMER_LIST_MAX_INTERVAL + 1, timer_list_fn1, NULL, NULL) == NULL);
  94. assert(timer_list_add(&tlist, 1, NULL, NULL, NULL) == NULL);
  95. /*
  96. * callback is called
  97. */
  98. timer_list_fn1_called = 0;
  99. tlist_entry = timer_list_add(&tlist, SHORT_TIMEOUT / 2, timer_list_fn1, &timer_list_fn1_called, timer_list_fn1);
  100. assert(tlist_entry != NULL);
  101. poll(NULL, 0, SHORT_TIMEOUT);
  102. assert(timer_list_time_to_expire(&tlist) == 0);
  103. assert(timer_list_time_to_expire_ms(&tlist) == 0);
  104. timer_list_expire(&tlist);
  105. assert(timer_list_fn1_called == 1);
  106. assert(timer_list_time_to_expire(&tlist) == PR_INTERVAL_NO_TIMEOUT);
  107. assert(timer_list_time_to_expire_ms(&tlist) == ~((uint32_t)0));
  108. timer_list_expire(&tlist);
  109. assert(timer_list_fn1_called == 1);
  110. /*
  111. * Callback is not called
  112. */
  113. timer_list_fn1_called = 0;
  114. tlist_entry = timer_list_add(&tlist, LONG_TIMEOUT, timer_list_fn1, &timer_list_fn1_called, timer_list_fn1);
  115. assert(tlist_entry != NULL);
  116. poll(NULL, 0, SHORT_TIMEOUT);
  117. assert(timer_list_time_to_expire(&tlist) > 0);
  118. assert(timer_list_time_to_expire_ms(&tlist) > 0);
  119. timer_list_expire(&tlist);
  120. assert(timer_list_fn1_called == 0);
  121. /*
  122. * Delete entry
  123. */
  124. timer_list_delete(&tlist, tlist_entry);
  125. assert(timer_list_time_to_expire(&tlist) == PR_INTERVAL_NO_TIMEOUT);
  126. assert(timer_list_time_to_expire_ms(&tlist) == ~((uint32_t)0));
  127. /*
  128. * Test speed and more entries
  129. */
  130. timer_list_fn1_called = 0;
  131. for (i = 0; i < SPEED_TEST_NO_ITEMS; i++) {
  132. tlist_speed_entry[i] = timer_list_add(&tlist, SHORT_TIMEOUT / 2,
  133. timer_list_fn1, &timer_list_fn1_called, timer_list_fn1);
  134. assert(tlist_speed_entry[i] != NULL);
  135. }
  136. for (i = 0; i < SPEED_TEST_NO_ITEMS; i++) {
  137. timer_list_reschedule(&tlist, tlist_speed_entry[i]);
  138. }
  139. poll(NULL, 0, SHORT_TIMEOUT);
  140. timer_list_expire(&tlist);
  141. assert(timer_list_fn1_called == SPEED_TEST_NO_ITEMS);
  142. timer_list_free(&tlist);
  143. }
  144. int
  145. main(void)
  146. {
  147. PR_Init(PR_USER_THREAD, PR_PRIORITY_NORMAL, 0);
  148. check_time_to_expire();
  149. check_timer_list_basics();
  150. assert(PR_Cleanup() == PR_SUCCESS);
  151. return (0);
  152. }