test_scheduler.c 20 KB

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