test_scheduler.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768
  1. /* Copyright (c) 2014-2016, 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. /* Event base for scheduelr tests */
  22. static struct event_base *mock_event_base = NULL;
  23. /* Statics controlling mocks */
  24. static circuitmux_t *mock_ccm_tgt_1 = NULL;
  25. static circuitmux_t *mock_ccm_tgt_2 = NULL;
  26. static circuitmux_t *mock_cgp_tgt_1 = NULL;
  27. static circuitmux_policy_t *mock_cgp_val_1 = NULL;
  28. static circuitmux_t *mock_cgp_tgt_2 = NULL;
  29. static circuitmux_policy_t *mock_cgp_val_2 = NULL;
  30. static int scheduler_compare_channels_mock_ctr = 0;
  31. static int scheduler_run_mock_ctr = 0;
  32. static void channel_flush_some_cells_mock_free_all(void);
  33. static void channel_flush_some_cells_mock_set(channel_t *chan,
  34. ssize_t num_cells);
  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 ssize_t channel_flush_some_cells_mock(channel_t *chan,
  40. ssize_t num_cells);
  41. static int circuitmux_compare_muxes_mock(circuitmux_t *cmux_1,
  42. circuitmux_t *cmux_2);
  43. static const circuitmux_policy_t * circuitmux_get_policy_mock(
  44. circuitmux_t *cmux);
  45. static int scheduler_compare_channels_mock(const void *c1_v,
  46. const void *c2_v);
  47. static void scheduler_run_noop_mock(void);
  48. static struct event_base * tor_libevent_get_base_mock(void);
  49. /* Scheduler test cases */
  50. static void test_scheduler_channel_states(void *arg);
  51. static void test_scheduler_compare_channels(void *arg);
  52. static void test_scheduler_initfree(void *arg);
  53. static void test_scheduler_loop(void *arg);
  54. static void test_scheduler_queue_heuristic(void *arg);
  55. /* Mock event init/free */
  56. /* Shamelessly stolen from compat_libevent.c */
  57. #define V(major, minor, patch) \
  58. (((major) << 24) | ((minor) << 16) | ((patch) << 8))
  59. static void
  60. mock_event_free_all(void)
  61. {
  62. tt_assert(mock_event_base != NULL);
  63. if (mock_event_base) {
  64. event_base_free(mock_event_base);
  65. mock_event_base = NULL;
  66. }
  67. tt_ptr_op(mock_event_base, ==, NULL);
  68. done:
  69. return;
  70. }
  71. static void
  72. mock_event_init(void)
  73. {
  74. #ifdef HAVE_EVENT2_EVENT_H
  75. struct event_config *cfg = NULL;
  76. #endif
  77. tt_ptr_op(mock_event_base, ==, NULL);
  78. /*
  79. * Really cut down from tor_libevent_initialize of
  80. * src/common/compat_libevent.c to kill config dependencies
  81. */
  82. if (!mock_event_base) {
  83. #ifdef HAVE_EVENT2_EVENT_H
  84. cfg = event_config_new();
  85. #if LIBEVENT_VERSION_NUMBER >= V(2,0,9)
  86. /* We can enable changelist support with epoll, since we don't give
  87. * Libevent any dup'd fds. This lets us avoid some syscalls. */
  88. event_config_set_flag(cfg, EVENT_BASE_FLAG_EPOLL_USE_CHANGELIST);
  89. #endif
  90. mock_event_base = event_base_new_with_config(cfg);
  91. event_config_free(cfg);
  92. #else
  93. mock_event_base = event_init();
  94. #endif
  95. }
  96. tt_assert(mock_event_base != NULL);
  97. done:
  98. return;
  99. }
  100. /* Mocks */
  101. typedef struct {
  102. const channel_t *chan;
  103. ssize_t cells;
  104. } flush_mock_channel_t;
  105. static smartlist_t *chans_for_flush_mock = NULL;
  106. static void
  107. channel_flush_some_cells_mock_free_all(void)
  108. {
  109. if (chans_for_flush_mock) {
  110. SMARTLIST_FOREACH_BEGIN(chans_for_flush_mock,
  111. flush_mock_channel_t *,
  112. flush_mock_ch) {
  113. SMARTLIST_DEL_CURRENT(chans_for_flush_mock, flush_mock_ch);
  114. tor_free(flush_mock_ch);
  115. } SMARTLIST_FOREACH_END(flush_mock_ch);
  116. smartlist_free(chans_for_flush_mock);
  117. chans_for_flush_mock = NULL;
  118. }
  119. }
  120. static void
  121. channel_flush_some_cells_mock_set(channel_t *chan, ssize_t num_cells)
  122. {
  123. flush_mock_channel_t *flush_mock_ch = NULL;
  124. if (!chan) return;
  125. if (num_cells <= 0) return;
  126. if (!chans_for_flush_mock) {
  127. chans_for_flush_mock = smartlist_new();
  128. }
  129. SMARTLIST_FOREACH_BEGIN(chans_for_flush_mock,
  130. flush_mock_channel_t *,
  131. flush_mock_ch) {
  132. if (flush_mock_ch != NULL && flush_mock_ch->chan != NULL) {
  133. if (flush_mock_ch->chan == chan) {
  134. /* Found it */
  135. flush_mock_ch->cells = num_cells;
  136. break;
  137. }
  138. } else {
  139. /* That shouldn't be there... */
  140. SMARTLIST_DEL_CURRENT(chans_for_flush_mock, flush_mock_ch);
  141. tor_free(flush_mock_ch);
  142. }
  143. } SMARTLIST_FOREACH_END(flush_mock_ch);
  144. if (!flush_mock_ch) {
  145. /* The loop didn't find it */
  146. flush_mock_ch = tor_malloc_zero(sizeof(*flush_mock_ch));
  147. flush_mock_ch->chan = chan;
  148. flush_mock_ch->cells = num_cells;
  149. smartlist_add(chans_for_flush_mock, flush_mock_ch);
  150. }
  151. }
  152. static ssize_t
  153. channel_flush_some_cells_mock(channel_t *chan, ssize_t num_cells)
  154. {
  155. ssize_t flushed = 0, max;
  156. char unlimited = 0;
  157. flush_mock_channel_t *found = NULL;
  158. tt_assert(chan != NULL);
  159. if (chan) {
  160. if (num_cells < 0) {
  161. num_cells = 0;
  162. unlimited = 1;
  163. }
  164. /* Check if we have it */
  165. if (chans_for_flush_mock != NULL) {
  166. SMARTLIST_FOREACH_BEGIN(chans_for_flush_mock,
  167. flush_mock_channel_t *,
  168. flush_mock_ch) {
  169. if (flush_mock_ch != NULL && flush_mock_ch->chan != NULL) {
  170. if (flush_mock_ch->chan == chan) {
  171. /* Found it */
  172. found = flush_mock_ch;
  173. break;
  174. }
  175. } else {
  176. /* That shouldn't be there... */
  177. SMARTLIST_DEL_CURRENT(chans_for_flush_mock, flush_mock_ch);
  178. tor_free(flush_mock_ch);
  179. }
  180. } SMARTLIST_FOREACH_END(flush_mock_ch);
  181. if (found) {
  182. /* We found one */
  183. if (found->cells < 0) found->cells = 0;
  184. if (unlimited) max = found->cells;
  185. else max = MIN(found->cells, num_cells);
  186. flushed += max;
  187. found->cells -= max;
  188. if (found->cells <= 0) {
  189. smartlist_remove(chans_for_flush_mock, found);
  190. tor_free(found);
  191. }
  192. }
  193. }
  194. }
  195. done:
  196. return flushed;
  197. }
  198. static int
  199. circuitmux_compare_muxes_mock(circuitmux_t *cmux_1,
  200. circuitmux_t *cmux_2)
  201. {
  202. int result = 0;
  203. tt_assert(cmux_1 != NULL);
  204. tt_assert(cmux_2 != NULL);
  205. if (cmux_1 != cmux_2) {
  206. if (cmux_1 == mock_ccm_tgt_1 && cmux_2 == mock_ccm_tgt_2) result = -1;
  207. else if (cmux_1 == mock_ccm_tgt_2 && cmux_2 == mock_ccm_tgt_1) {
  208. result = 1;
  209. } else {
  210. if (cmux_1 == mock_ccm_tgt_1 || cmux_1 == mock_ccm_tgt_2) result = -1;
  211. else if (cmux_2 == mock_ccm_tgt_1 || cmux_2 == mock_ccm_tgt_2) {
  212. result = 1;
  213. } else {
  214. result = circuitmux_compare_muxes__real(cmux_1, cmux_2);
  215. }
  216. }
  217. }
  218. /* else result = 0 always */
  219. done:
  220. return result;
  221. }
  222. static const circuitmux_policy_t *
  223. circuitmux_get_policy_mock(circuitmux_t *cmux)
  224. {
  225. const circuitmux_policy_t *result = NULL;
  226. tt_assert(cmux != NULL);
  227. if (cmux) {
  228. if (cmux == mock_cgp_tgt_1) result = mock_cgp_val_1;
  229. else if (cmux == mock_cgp_tgt_2) result = mock_cgp_val_2;
  230. else result = circuitmux_get_policy__real(cmux);
  231. }
  232. done:
  233. return result;
  234. }
  235. static int
  236. scheduler_compare_channels_mock(const void *c1_v,
  237. const void *c2_v)
  238. {
  239. uintptr_t p1, p2;
  240. p1 = (uintptr_t)(c1_v);
  241. p2 = (uintptr_t)(c2_v);
  242. ++scheduler_compare_channels_mock_ctr;
  243. if (p1 == p2) return 0;
  244. else if (p1 < p2) return 1;
  245. else return -1;
  246. }
  247. static void
  248. scheduler_run_noop_mock(void)
  249. {
  250. ++scheduler_run_mock_ctr;
  251. }
  252. static struct event_base *
  253. tor_libevent_get_base_mock(void)
  254. {
  255. return mock_event_base;
  256. }
  257. /* Test cases */
  258. static void
  259. test_scheduler_channel_states(void *arg)
  260. {
  261. channel_t *ch1 = NULL, *ch2 = NULL;
  262. int old_count;
  263. (void)arg;
  264. /* Set up libevent and scheduler */
  265. mock_event_init();
  266. MOCK(tor_libevent_get_base, tor_libevent_get_base_mock);
  267. scheduler_init();
  268. /*
  269. * Install the compare channels mock so we can test
  270. * scheduler_touch_channel().
  271. */
  272. MOCK(scheduler_compare_channels, scheduler_compare_channels_mock);
  273. /*
  274. * Disable scheduler_run so we can just check the state transitions
  275. * without having to make everything it might call work too.
  276. */
  277. MOCK(scheduler_run, scheduler_run_noop_mock);
  278. tt_int_op(smartlist_len(channels_pending), ==, 0);
  279. /* Set up a fake channel */
  280. ch1 = new_fake_channel();
  281. tt_assert(ch1);
  282. /* Start it off in OPENING */
  283. ch1->state = CHANNEL_STATE_OPENING;
  284. /* We'll need a cmux */
  285. ch1->cmux = circuitmux_alloc();
  286. /* Try to register it */
  287. channel_register(ch1);
  288. tt_assert(ch1->registered);
  289. /* It should start off in SCHED_CHAN_IDLE */
  290. tt_int_op(ch1->scheduler_state, ==, SCHED_CHAN_IDLE);
  291. /* Now get another one */
  292. ch2 = new_fake_channel();
  293. tt_assert(ch2);
  294. ch2->state = CHANNEL_STATE_OPENING;
  295. ch2->cmux = circuitmux_alloc();
  296. channel_register(ch2);
  297. tt_assert(ch2->registered);
  298. /* Send it to SCHED_CHAN_WAITING_TO_WRITE */
  299. scheduler_channel_has_waiting_cells(ch1);
  300. tt_int_op(ch1->scheduler_state, ==, SCHED_CHAN_WAITING_TO_WRITE);
  301. /* This should send it to SCHED_CHAN_PENDING */
  302. scheduler_channel_wants_writes(ch1);
  303. tt_int_op(ch1->scheduler_state, ==, SCHED_CHAN_PENDING);
  304. tt_int_op(smartlist_len(channels_pending), ==, 1);
  305. /* Now send ch2 to SCHED_CHAN_WAITING_FOR_CELLS */
  306. scheduler_channel_wants_writes(ch2);
  307. tt_int_op(ch2->scheduler_state, ==, SCHED_CHAN_WAITING_FOR_CELLS);
  308. /* Drop ch2 back to idle */
  309. scheduler_channel_doesnt_want_writes(ch2);
  310. tt_int_op(ch2->scheduler_state, ==, SCHED_CHAN_IDLE);
  311. /* ...and back to SCHED_CHAN_WAITING_FOR_CELLS */
  312. scheduler_channel_wants_writes(ch2);
  313. tt_int_op(ch2->scheduler_state, ==, SCHED_CHAN_WAITING_FOR_CELLS);
  314. /* ...and this should kick ch2 into SCHED_CHAN_PENDING */
  315. scheduler_channel_has_waiting_cells(ch2);
  316. tt_int_op(ch2->scheduler_state, ==, SCHED_CHAN_PENDING);
  317. tt_int_op(smartlist_len(channels_pending), ==, 2);
  318. /* This should send ch2 to SCHED_CHAN_WAITING_TO_WRITE */
  319. scheduler_channel_doesnt_want_writes(ch2);
  320. tt_int_op(ch2->scheduler_state, ==, SCHED_CHAN_WAITING_TO_WRITE);
  321. tt_int_op(smartlist_len(channels_pending), ==, 1);
  322. /* ...and back to SCHED_CHAN_PENDING */
  323. scheduler_channel_wants_writes(ch2);
  324. tt_int_op(ch2->scheduler_state, ==, SCHED_CHAN_PENDING);
  325. tt_int_op(smartlist_len(channels_pending), ==, 2);
  326. /* Now we exercise scheduler_touch_channel */
  327. old_count = scheduler_compare_channels_mock_ctr;
  328. scheduler_touch_channel(ch1);
  329. tt_assert(scheduler_compare_channels_mock_ctr > old_count);
  330. /* Close */
  331. channel_mark_for_close(ch1);
  332. tt_int_op(ch1->state, ==, CHANNEL_STATE_CLOSING);
  333. channel_mark_for_close(ch2);
  334. tt_int_op(ch2->state, ==, CHANNEL_STATE_CLOSING);
  335. channel_closed(ch1);
  336. tt_int_op(ch1->state, ==, CHANNEL_STATE_CLOSED);
  337. ch1 = NULL;
  338. channel_closed(ch2);
  339. tt_int_op(ch2->state, ==, CHANNEL_STATE_CLOSED);
  340. ch2 = NULL;
  341. /* Shut things down */
  342. channel_free_all();
  343. scheduler_free_all();
  344. mock_event_free_all();
  345. done:
  346. tor_free(ch1);
  347. tor_free(ch2);
  348. UNMOCK(scheduler_compare_channels);
  349. UNMOCK(scheduler_run);
  350. UNMOCK(tor_libevent_get_base);
  351. return;
  352. }
  353. static void
  354. test_scheduler_compare_channels(void *arg)
  355. {
  356. /* We don't actually need whole fake channels... */
  357. channel_t c1, c2;
  358. /* ...and some dummy circuitmuxes too */
  359. circuitmux_t *cm1 = NULL, *cm2 = NULL;
  360. int result;
  361. (void)arg;
  362. /* We can't actually see sizeof(circuitmux_t) from here */
  363. cm1 = tor_malloc_zero(sizeof(void *));
  364. cm2 = tor_malloc_zero(sizeof(void *));
  365. c1.cmux = cm1;
  366. c2.cmux = cm2;
  367. /* Configure circuitmux_get_policy() mock */
  368. mock_cgp_tgt_1 = cm1;
  369. mock_cgp_tgt_2 = cm2;
  370. /*
  371. * This is to test the different-policies case, which uses the policy
  372. * cast to an uintptr_t as an arbitrary but definite thing to compare.
  373. */
  374. mock_cgp_val_1 = tor_malloc_zero(16);
  375. mock_cgp_val_2 = tor_malloc_zero(16);
  376. if ( ((uintptr_t) mock_cgp_val_1) > ((uintptr_t) mock_cgp_val_2) ) {
  377. void *tmp = mock_cgp_val_1;
  378. mock_cgp_val_1 = mock_cgp_val_2;
  379. mock_cgp_val_2 = tmp;
  380. }
  381. MOCK(circuitmux_get_policy, circuitmux_get_policy_mock);
  382. /* Now set up circuitmux_compare_muxes() mock using cm1/cm2 */
  383. mock_ccm_tgt_1 = cm1;
  384. mock_ccm_tgt_2 = cm2;
  385. MOCK(circuitmux_compare_muxes, circuitmux_compare_muxes_mock);
  386. /* Equal-channel case */
  387. result = scheduler_compare_channels(&c1, &c1);
  388. tt_int_op(result, ==, 0);
  389. /* Distinct channels, distinct policies */
  390. result = scheduler_compare_channels(&c1, &c2);
  391. tt_int_op(result, ==, -1);
  392. result = scheduler_compare_channels(&c2, &c1);
  393. tt_int_op(result, ==, 1);
  394. /* Distinct channels, same policy */
  395. tor_free(mock_cgp_val_2);
  396. mock_cgp_val_2 = mock_cgp_val_1;
  397. result = scheduler_compare_channels(&c1, &c2);
  398. tt_int_op(result, ==, -1);
  399. result = scheduler_compare_channels(&c2, &c1);
  400. tt_int_op(result, ==, 1);
  401. done:
  402. UNMOCK(circuitmux_compare_muxes);
  403. mock_ccm_tgt_1 = NULL;
  404. mock_ccm_tgt_2 = NULL;
  405. UNMOCK(circuitmux_get_policy);
  406. mock_cgp_tgt_1 = NULL;
  407. mock_cgp_tgt_2 = NULL;
  408. tor_free(cm1);
  409. tor_free(cm2);
  410. if (mock_cgp_val_1 != mock_cgp_val_2)
  411. tor_free(mock_cgp_val_1);
  412. tor_free(mock_cgp_val_2);
  413. mock_cgp_val_1 = NULL;
  414. mock_cgp_val_2 = NULL;
  415. return;
  416. }
  417. static void
  418. test_scheduler_initfree(void *arg)
  419. {
  420. (void)arg;
  421. tt_ptr_op(channels_pending, ==, NULL);
  422. tt_ptr_op(run_sched_ev, ==, NULL);
  423. mock_event_init();
  424. MOCK(tor_libevent_get_base, tor_libevent_get_base_mock);
  425. scheduler_init();
  426. tt_assert(channels_pending != NULL);
  427. tt_assert(run_sched_ev != NULL);
  428. scheduler_free_all();
  429. UNMOCK(tor_libevent_get_base);
  430. mock_event_free_all();
  431. tt_ptr_op(channels_pending, ==, NULL);
  432. tt_ptr_op(run_sched_ev, ==, NULL);
  433. done:
  434. return;
  435. }
  436. static void
  437. test_scheduler_loop(void *arg)
  438. {
  439. channel_t *ch1 = NULL, *ch2 = NULL;
  440. (void)arg;
  441. /* Set up libevent and scheduler */
  442. mock_event_init();
  443. MOCK(tor_libevent_get_base, tor_libevent_get_base_mock);
  444. scheduler_init();
  445. /*
  446. * Install the compare channels mock so we can test
  447. * scheduler_touch_channel().
  448. */
  449. MOCK(scheduler_compare_channels, scheduler_compare_channels_mock);
  450. /*
  451. * Disable scheduler_run so we can just check the state transitions
  452. * without having to make everything it might call work too.
  453. */
  454. MOCK(scheduler_run, scheduler_run_noop_mock);
  455. tt_int_op(smartlist_len(channels_pending), ==, 0);
  456. /* Set up a fake channel */
  457. ch1 = new_fake_channel();
  458. tt_assert(ch1);
  459. /* Start it off in OPENING */
  460. ch1->state = CHANNEL_STATE_OPENING;
  461. /* We'll need a cmux */
  462. ch1->cmux = circuitmux_alloc();
  463. /* Try to register it */
  464. channel_register(ch1);
  465. tt_assert(ch1->registered);
  466. /* Finish opening it */
  467. channel_change_state(ch1, CHANNEL_STATE_OPEN);
  468. /* It should start off in SCHED_CHAN_IDLE */
  469. tt_int_op(ch1->scheduler_state, ==, SCHED_CHAN_IDLE);
  470. /* Now get another one */
  471. ch2 = new_fake_channel();
  472. tt_assert(ch2);
  473. ch2->state = CHANNEL_STATE_OPENING;
  474. ch2->cmux = circuitmux_alloc();
  475. channel_register(ch2);
  476. tt_assert(ch2->registered);
  477. /*
  478. * Don't open ch2; then channel_num_cells_writeable() will return
  479. * zero and we'll get coverage of that exception case in scheduler_run()
  480. */
  481. tt_int_op(ch1->state, ==, CHANNEL_STATE_OPEN);
  482. tt_int_op(ch2->state, ==, CHANNEL_STATE_OPENING);
  483. /* Send it to SCHED_CHAN_WAITING_TO_WRITE */
  484. scheduler_channel_has_waiting_cells(ch1);
  485. tt_int_op(ch1->scheduler_state, ==, SCHED_CHAN_WAITING_TO_WRITE);
  486. /* This should send it to SCHED_CHAN_PENDING */
  487. scheduler_channel_wants_writes(ch1);
  488. tt_int_op(ch1->scheduler_state, ==, SCHED_CHAN_PENDING);
  489. tt_int_op(smartlist_len(channels_pending), ==, 1);
  490. /* Now send ch2 to SCHED_CHAN_WAITING_FOR_CELLS */
  491. scheduler_channel_wants_writes(ch2);
  492. tt_int_op(ch2->scheduler_state, ==, SCHED_CHAN_WAITING_FOR_CELLS);
  493. /* Drop ch2 back to idle */
  494. scheduler_channel_doesnt_want_writes(ch2);
  495. tt_int_op(ch2->scheduler_state, ==, SCHED_CHAN_IDLE);
  496. /* ...and back to SCHED_CHAN_WAITING_FOR_CELLS */
  497. scheduler_channel_wants_writes(ch2);
  498. tt_int_op(ch2->scheduler_state, ==, SCHED_CHAN_WAITING_FOR_CELLS);
  499. /* ...and this should kick ch2 into SCHED_CHAN_PENDING */
  500. scheduler_channel_has_waiting_cells(ch2);
  501. tt_int_op(ch2->scheduler_state, ==, SCHED_CHAN_PENDING);
  502. tt_int_op(smartlist_len(channels_pending), ==, 2);
  503. /*
  504. * Now we've got two pending channels and need to fire off
  505. * scheduler_run(); first, unmock it.
  506. */
  507. UNMOCK(scheduler_run);
  508. scheduler_run();
  509. /* Now re-mock it */
  510. MOCK(scheduler_run, scheduler_run_noop_mock);
  511. /*
  512. * Assert that they're still in the states we left and aren't still
  513. * pending
  514. */
  515. tt_int_op(ch1->state, ==, CHANNEL_STATE_OPEN);
  516. tt_int_op(ch2->state, ==, CHANNEL_STATE_OPENING);
  517. tt_assert(ch1->scheduler_state != SCHED_CHAN_PENDING);
  518. tt_assert(ch2->scheduler_state != SCHED_CHAN_PENDING);
  519. tt_int_op(smartlist_len(channels_pending), ==, 0);
  520. /* Now, finish opening ch2, and get both back to pending */
  521. channel_change_state(ch2, CHANNEL_STATE_OPEN);
  522. scheduler_channel_wants_writes(ch1);
  523. scheduler_channel_wants_writes(ch2);
  524. scheduler_channel_has_waiting_cells(ch1);
  525. scheduler_channel_has_waiting_cells(ch2);
  526. tt_int_op(ch1->state, ==, CHANNEL_STATE_OPEN);
  527. tt_int_op(ch2->state, ==, CHANNEL_STATE_OPEN);
  528. tt_int_op(ch1->scheduler_state, ==, SCHED_CHAN_PENDING);
  529. tt_int_op(ch2->scheduler_state, ==, SCHED_CHAN_PENDING);
  530. tt_int_op(smartlist_len(channels_pending), ==, 2);
  531. /* Now, set up the channel_flush_some_cells() mock */
  532. MOCK(channel_flush_some_cells, channel_flush_some_cells_mock);
  533. /*
  534. * 16 cells on ch1 means it'll completely drain into the 32 cells
  535. * fakechan's num_cells_writeable() returns.
  536. */
  537. channel_flush_some_cells_mock_set(ch1, 16);
  538. /*
  539. * This one should get sent back to pending, since num_cells_writeable()
  540. * will still return non-zero.
  541. */
  542. channel_flush_some_cells_mock_set(ch2, 48);
  543. /*
  544. * And re-run the scheduler_run() loop with non-zero returns from
  545. * channel_flush_some_cells() this time.
  546. */
  547. UNMOCK(scheduler_run);
  548. scheduler_run();
  549. /* Now re-mock it */
  550. MOCK(scheduler_run, scheduler_run_noop_mock);
  551. /*
  552. * ch1 should have gone to SCHED_CHAN_WAITING_FOR_CELLS, with 16 flushed
  553. * and 32 writeable.
  554. */
  555. tt_int_op(ch1->scheduler_state, ==, SCHED_CHAN_WAITING_FOR_CELLS);
  556. /*
  557. * ...ch2 should also have gone to SCHED_CHAN_WAITING_FOR_CELLS, with
  558. * channel_more_to_flush() returning false and channel_num_cells_writeable()
  559. * > 0/
  560. */
  561. tt_int_op(ch2->scheduler_state, ==, SCHED_CHAN_WAITING_FOR_CELLS);
  562. /* Close */
  563. channel_mark_for_close(ch1);
  564. tt_int_op(ch1->state, ==, CHANNEL_STATE_CLOSING);
  565. channel_mark_for_close(ch2);
  566. tt_int_op(ch2->state, ==, CHANNEL_STATE_CLOSING);
  567. channel_closed(ch1);
  568. tt_int_op(ch1->state, ==, CHANNEL_STATE_CLOSED);
  569. ch1 = NULL;
  570. channel_closed(ch2);
  571. tt_int_op(ch2->state, ==, CHANNEL_STATE_CLOSED);
  572. ch2 = NULL;
  573. /* Shut things down */
  574. channel_flush_some_cells_mock_free_all();
  575. channel_free_all();
  576. scheduler_free_all();
  577. mock_event_free_all();
  578. done:
  579. tor_free(ch1);
  580. tor_free(ch2);
  581. UNMOCK(channel_flush_some_cells);
  582. UNMOCK(scheduler_compare_channels);
  583. UNMOCK(scheduler_run);
  584. UNMOCK(tor_libevent_get_base);
  585. }
  586. static void
  587. test_scheduler_queue_heuristic(void *arg)
  588. {
  589. time_t now = approx_time();
  590. uint64_t qh;
  591. (void)arg;
  592. queue_heuristic = 0;
  593. queue_heuristic_timestamp = 0;
  594. /* Not yet inited case */
  595. scheduler_update_queue_heuristic(now - 180);
  596. tt_u64_op(queue_heuristic, ==, 0);
  597. tt_int_op(queue_heuristic_timestamp, ==, now - 180);
  598. queue_heuristic = 1000000000L;
  599. queue_heuristic_timestamp = now - 120;
  600. scheduler_update_queue_heuristic(now - 119);
  601. tt_u64_op(queue_heuristic, ==, 500000000L);
  602. tt_int_op(queue_heuristic_timestamp, ==, now - 119);
  603. scheduler_update_queue_heuristic(now - 116);
  604. tt_u64_op(queue_heuristic, ==, 62500000L);
  605. tt_int_op(queue_heuristic_timestamp, ==, now - 116);
  606. qh = scheduler_get_queue_heuristic();
  607. tt_u64_op(qh, ==, 0);
  608. done:
  609. return;
  610. }
  611. struct testcase_t scheduler_tests[] = {
  612. { "channel_states", test_scheduler_channel_states, TT_FORK, NULL, NULL },
  613. { "compare_channels", test_scheduler_compare_channels,
  614. TT_FORK, NULL, NULL },
  615. { "initfree", test_scheduler_initfree, TT_FORK, NULL, NULL },
  616. { "loop", test_scheduler_loop, TT_FORK, NULL, NULL },
  617. { "queue_heuristic", test_scheduler_queue_heuristic,
  618. TT_FORK, NULL, NULL },
  619. END_OF_TESTCASES
  620. };