test_scheduler.c 21 KB

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