test_scheduler.c 20 KB

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