test_scheduler.c 20 KB

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