test_scheduler.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. /* Copyright (c) 2014, The Tor Project, Inc. */
  2. /* See LICENSE for licensing information */
  3. #include <math.h>
  4. #include "orconfig.h"
  5. /* Libevent stuff */
  6. #ifdef HAVE_EVENT2_EVENT_H
  7. #include <event2/event.h>
  8. #else
  9. #include <event.h>
  10. #endif
  11. #define TOR_CHANNEL_INTERNAL_
  12. #define CHANNEL_PRIVATE_
  13. #include "or.h"
  14. #include "compat_libevent.h"
  15. #include "channel.h"
  16. #define SCHEDULER_PRIVATE_
  17. #include "scheduler.h"
  18. /* Test suite stuff */
  19. #include "test.h"
  20. #include "fakechans.h"
  21. /* Statics in scheduler.c exposed to the test suite */
  22. extern smartlist_t *channels_pending;
  23. extern struct event *run_sched_ev;
  24. extern uint64_t queue_heuristic;
  25. extern time_t queue_heuristic_timestamp;
  26. /* Event base for scheduelr tests */
  27. static struct event_base *mock_event_base = NULL;
  28. /* Statics controlling mocks */
  29. static circuitmux_t *mock_ccm_tgt_1 = NULL;
  30. static circuitmux_t *mock_ccm_tgt_2 = NULL;
  31. static circuitmux_t *mock_cgp_tgt_1 = NULL;
  32. static const circuitmux_policy_t *mock_cgp_val_1 = NULL;
  33. static circuitmux_t *mock_cgp_tgt_2 = NULL;
  34. static const circuitmux_policy_t *mock_cgp_val_2 = NULL;
  35. /* Setup for mock event stuff */
  36. static void mock_event_free_all(void);
  37. static void mock_event_init(void);
  38. /* Mocks used by scheduler tests */
  39. static int circuitmux_compare_muxes_mock(circuitmux_t *cmux_1,
  40. circuitmux_t *cmux_2);
  41. static const circuitmux_policy_t * circuitmux_get_policy_mock(
  42. circuitmux_t *cmux);
  43. static struct event_base * tor_libevent_get_base_mock(void);
  44. /* Scheduler test cases */
  45. static void test_scheduler_compare_channels(void *arg);
  46. static void test_scheduler_initfree(void *arg);
  47. static void test_scheduler_queue_heuristic(void *arg);
  48. /* Mock event init/free */
  49. /* Shamelessly stolen from compat_libevent.c */
  50. #define V(major, minor, patch) \
  51. (((major) << 24) | ((minor) << 16) | ((patch) << 8))
  52. static void
  53. mock_event_free_all(void)
  54. {
  55. test_assert(mock_event_base != NULL);
  56. if (mock_event_base) {
  57. event_base_free(mock_event_base);
  58. mock_event_base = NULL;
  59. }
  60. test_eq(mock_event_base, NULL);
  61. done:
  62. return;
  63. }
  64. static void
  65. mock_event_init(void)
  66. {
  67. #ifdef HAVE_EVENT2_EVENT_H
  68. struct event_config *cfg = NULL;
  69. #endif
  70. test_eq(mock_event_base, NULL);
  71. /*
  72. * Really cut down from tor_libevent_initialize of
  73. * src/common/compat_libevent.c to kill config dependencies
  74. */
  75. if (!mock_event_base) {
  76. #ifdef HAVE_EVENT2_EVENT_H
  77. cfg = event_config_new();
  78. #if LIBEVENT_VERSION_NUMBER >= V(2,0,9)
  79. /* We can enable changelist support with epoll, since we don't give
  80. * Libevent any dup'd fds. This lets us avoid some syscalls. */
  81. event_config_set_flag(cfg, EVENT_BASE_FLAG_EPOLL_USE_CHANGELIST);
  82. #endif
  83. mock_event_base = event_base_new_with_config(cfg);
  84. event_config_free(cfg);
  85. #else
  86. mock_event_base = event_init();
  87. #endif
  88. }
  89. test_assert(mock_event_base != NULL);
  90. done:
  91. return;
  92. }
  93. /* Mocks */
  94. static int
  95. circuitmux_compare_muxes_mock(circuitmux_t *cmux_1,
  96. circuitmux_t *cmux_2)
  97. {
  98. int result = 0;
  99. test_assert(cmux_1 != NULL);
  100. test_assert(cmux_2 != NULL);
  101. if (cmux_1 != cmux_2) {
  102. if (cmux_1 == mock_ccm_tgt_1 && cmux_2 == mock_ccm_tgt_2) result = -1;
  103. else if (cmux_1 == mock_ccm_tgt_2 && cmux_2 == mock_ccm_tgt_1) {
  104. result = 1;
  105. }
  106. else {
  107. if (cmux_1 == mock_ccm_tgt_1 || cmux_1 == mock_ccm_tgt_1) result = -1;
  108. else if (cmux_2 == mock_ccm_tgt_1 || cmux_2 == mock_ccm_tgt_2) {
  109. result = 1;
  110. } else {
  111. result = circuitmux_compare_muxes__real(cmux_1, cmux_2);
  112. }
  113. }
  114. }
  115. /* else result = 0 always */
  116. done:
  117. return result;
  118. }
  119. static const circuitmux_policy_t *
  120. circuitmux_get_policy_mock(circuitmux_t *cmux)
  121. {
  122. const circuitmux_policy_t *result = NULL;
  123. test_assert(cmux != NULL);
  124. if (cmux) {
  125. if (cmux == mock_cgp_tgt_1) result = mock_cgp_val_1;
  126. else if (cmux == mock_cgp_tgt_2) result = mock_cgp_val_2;
  127. else result = circuitmux_get_policy__real(cmux);
  128. }
  129. done:
  130. return result;
  131. }
  132. static struct event_base *
  133. tor_libevent_get_base_mock(void)
  134. {
  135. return mock_event_base;
  136. }
  137. /* Test cases */
  138. static void
  139. test_scheduler_compare_channels(void *arg)
  140. {
  141. /* We don't actually need whole fake channels... */
  142. channel_t c1, c2;
  143. /* ...and some dummy circuitmuxes too */
  144. circuitmux_t *cm1 = NULL, *cm2 = NULL;
  145. int result;
  146. (void)arg;
  147. /* We can't actually see sizeof(circuitmux_t) from here */
  148. cm1 = tor_malloc_zero(sizeof(void *));
  149. cm2 = tor_malloc_zero(sizeof(void *));
  150. c1.cmux = cm1;
  151. c2.cmux = cm2;
  152. /* Configure circuitmux_get_policy() mock */
  153. mock_cgp_tgt_1 = cm1;
  154. /*
  155. * This is to test the different-policies case, which uses the policy
  156. * cast to an intptr_t as an arbitrary but definite thing to compare.
  157. */
  158. mock_cgp_val_1 = (const circuitmux_policy_t *)(1);
  159. mock_cgp_tgt_2 = cm2;
  160. mock_cgp_val_2 = (const circuitmux_policy_t *)(2);
  161. MOCK(circuitmux_get_policy, circuitmux_get_policy_mock);
  162. /* Now set up circuitmux_compare_muxes() mock using cm1/cm2 */
  163. mock_ccm_tgt_1 = cm1;
  164. mock_ccm_tgt_2 = cm2;
  165. MOCK(circuitmux_compare_muxes, circuitmux_compare_muxes_mock);
  166. /* Equal-channel case */
  167. result = scheduler_compare_channels(&c1, &c1);
  168. test_eq(result, 0);
  169. /* Distinct channels, distinct policies */
  170. result = scheduler_compare_channels(&c1, &c2);
  171. test_eq(result, -1);
  172. result = scheduler_compare_channels(&c2, &c1);
  173. test_eq(result, 1);
  174. /* Distinct channels, same policy */
  175. mock_cgp_val_2 = mock_cgp_val_1;
  176. result = scheduler_compare_channels(&c1, &c2);
  177. test_eq(result, -1);
  178. result = scheduler_compare_channels(&c2, &c1);
  179. test_eq(result, 1);
  180. done:
  181. UNMOCK(circuitmux_compare_muxes);
  182. mock_ccm_tgt_1 = NULL;
  183. mock_ccm_tgt_2 = NULL;
  184. UNMOCK(circuitmux_get_policy);
  185. mock_cgp_tgt_1 = NULL;
  186. mock_cgp_val_1 = NULL;
  187. mock_cgp_tgt_2 = NULL;
  188. mock_cgp_val_2 = NULL;
  189. tor_free(cm1);
  190. tor_free(cm2);
  191. return;
  192. }
  193. static void
  194. test_scheduler_initfree(void *arg)
  195. {
  196. (void)arg;
  197. test_eq(channels_pending, NULL);
  198. test_eq(run_sched_ev, NULL);
  199. mock_event_init();
  200. MOCK(tor_libevent_get_base, tor_libevent_get_base_mock);
  201. scheduler_init();
  202. test_assert(channels_pending != NULL);
  203. test_assert(run_sched_ev != NULL);
  204. scheduler_free_all();
  205. UNMOCK(tor_libevent_get_base);
  206. mock_event_free_all();
  207. test_eq(channels_pending, NULL);
  208. test_eq(run_sched_ev, NULL);
  209. done:
  210. return;
  211. }
  212. static void
  213. test_scheduler_queue_heuristic(void *arg)
  214. {
  215. time_t now = approx_time();
  216. uint64_t qh;
  217. (void)arg;
  218. queue_heuristic = 0;
  219. queue_heuristic_timestamp = 0;
  220. /* Not yet inited case */
  221. scheduler_update_queue_heuristic(now - 180);
  222. test_eq(queue_heuristic, 0);
  223. test_eq(queue_heuristic_timestamp, now - 180);
  224. queue_heuristic = 1000000000L;
  225. queue_heuristic_timestamp = now - 120;
  226. scheduler_update_queue_heuristic(now - 119);
  227. test_eq(queue_heuristic, 500000000L);
  228. test_eq(queue_heuristic_timestamp, now - 119);
  229. scheduler_update_queue_heuristic(now - 116);
  230. test_eq(queue_heuristic, 62500000L);
  231. test_eq(queue_heuristic_timestamp, now - 116);
  232. qh = scheduler_get_queue_heuristic();
  233. test_eq(qh, 0);
  234. done:
  235. return;
  236. }
  237. struct testcase_t scheduler_tests[] = {
  238. { "compare_channels", test_scheduler_compare_channels,
  239. TT_FORK, NULL, NULL },
  240. { "initfree", test_scheduler_initfree, TT_FORK, NULL, NULL },
  241. { "queue_heuristic", test_scheduler_queue_heuristic,
  242. TT_FORK, NULL, NULL },
  243. END_OF_TESTCASES
  244. };