test_testbed_api_operations.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541
  1. /*
  2. This file is part of GNUnet
  3. Copyright (C) 2008--2013 GNUnet e.V.
  4. GNUnet is free software: you can redistribute it and/or modify it
  5. under the terms of the GNU Affero General Public License as published
  6. by the Free Software Foundation, either version 3 of the License,
  7. or (at your option) any later version.
  8. GNUnet is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Affero General Public License for more details.
  12. You should have received a copy of the GNU Affero General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>.
  14. SPDX-License-Identifier: AGPL3.0-or-later
  15. */
  16. /**
  17. * @file testbed/test_testbed_api_operations.c
  18. * @brief tests cases for testbed_api_operations.c
  19. * @author Sree Harsha Totakura
  20. */
  21. #include "platform.h"
  22. #include "gnunet_util_lib.h"
  23. #include "testbed_api_operations.h"
  24. /**
  25. * Generic logging shortcut
  26. */
  27. #define LOG(kind,...) \
  28. GNUNET_log (kind, __VA_ARGS__)
  29. /**
  30. * Delay to start step task
  31. */
  32. #define STEP_DELAY \
  33. GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, 500)
  34. /**
  35. * Queue A. Initially the max active is set to 2 and then reduced to 0 - this
  36. * should block op2 even after op1 has finished. Later the max active is set to
  37. * 2 and this should start op2
  38. */
  39. struct OperationQueue *q1;
  40. /**
  41. * Queue B. Max active set to 2 is not changed throughout the test
  42. */
  43. struct OperationQueue *q2;
  44. /**
  45. * This operation should go into both queues and block op2 until it is done
  46. */
  47. struct GNUNET_TESTBED_Operation *op1;
  48. /**
  49. * This operation should go into q1 and q2
  50. */
  51. struct GNUNET_TESTBED_Operation *op2;
  52. /**
  53. * This operation should go into both queues and should consume 2 units of
  54. * resources on both queues. Since op2 needs a resource from both queues and is
  55. * queues before this operation, it will be blocked until op2 is released even
  56. * though q1 has enough free resources
  57. */
  58. struct GNUNET_TESTBED_Operation *op3;
  59. /**
  60. * Just like op3, this operation also consumes 2 units of resources on both
  61. * queues. Since this is queued after op3 and both queues are at max active
  62. * 2. This will be blocked until op3 is done.
  63. */
  64. struct GNUNET_TESTBED_Operation *op4;
  65. /**
  66. * This operation is started after op4 is released and should consume only 1
  67. * resource on queue q1. It should be started along with op6 and op7
  68. */
  69. struct GNUNET_TESTBED_Operation *op5;
  70. /**
  71. * This operation is started after op4 is released and should consume only 1
  72. * resource on q2. It should be started along with op5 and op7
  73. */
  74. struct GNUNET_TESTBED_Operation *op6;
  75. /**
  76. * This operation is started after op4 is released and should consume 1 resource
  77. * on both queues q1 and q1. It should be started along with op5 and op6. It is
  78. * then inactivated when op6 is released. op8's start should release this
  79. * operation implicitly.
  80. */
  81. struct GNUNET_TESTBED_Operation *op7;
  82. /**
  83. * This operation is started after op6 is finished in step task. It consumes 2
  84. * resources on both queues q1 and q2. This operation should evict op7. After
  85. * starting, it should be made inactive, active and inactive again in the step task.
  86. */
  87. struct GNUNET_TESTBED_Operation *op8;
  88. /**
  89. * This opration is started after activating op8. It should consume a resource
  90. * on queues q1 and q2. It should not be started until op8 is again made
  91. * inactive at which point it should be released. It can be released as soon as
  92. * it begins.
  93. */
  94. struct GNUNET_TESTBED_Operation *op9;
  95. /**
  96. * The delay task identifier
  97. */
  98. struct GNUNET_SCHEDULER_Task * step_task;
  99. /**
  100. * Enumeration of test stages
  101. */
  102. enum Test
  103. {
  104. /**
  105. * Initial stage
  106. */
  107. TEST_INIT,
  108. /**
  109. * op1 has been started
  110. */
  111. TEST_OP1_STARTED,
  112. /**
  113. * op1 has been released
  114. */
  115. TEST_OP1_RELEASED,
  116. /**
  117. * Temporary pause where no operations should start as we set max active in q1
  118. * to 0 in stage TEST_OP1_STARTED
  119. */
  120. TEST_PAUSE,
  121. /**
  122. * op2 has started
  123. */
  124. TEST_OP2_STARTED,
  125. /**
  126. * op2 released
  127. */
  128. TEST_OP2_RELEASED,
  129. /**
  130. * op3 has started
  131. */
  132. TEST_OP3_STARTED,
  133. /**
  134. * op3 has finished
  135. */
  136. TEST_OP3_RELEASED,
  137. /**
  138. * op4 has started
  139. */
  140. TEST_OP4_STARTED,
  141. /**
  142. * op4 has released
  143. */
  144. TEST_OP4_RELEASED,
  145. /**
  146. * op5, op6, op7 started
  147. */
  148. TEST_OP5_6_7_STARTED,
  149. /**
  150. * op5 has released
  151. */
  152. TEST_OP5_RELEASED,
  153. /**
  154. * op6 has released
  155. */
  156. TEST_OP6_RELEASED,
  157. /**
  158. * op8 has began waiting
  159. */
  160. TEST_OP8_WAITING,
  161. /**
  162. * op7 has released
  163. */
  164. TEST_OP7_RELEASED,
  165. /**
  166. * op8 has started
  167. */
  168. TEST_OP8_STARTED,
  169. /**
  170. * op8 is inactive
  171. */
  172. TEST_OP8_INACTIVE_1,
  173. /**
  174. * op8 is active
  175. */
  176. TEST_OP8_ACTIVE,
  177. /**
  178. * op8 has been released
  179. */
  180. TEST_OP8_RELEASED,
  181. /**
  182. * op9 has started
  183. */
  184. TEST_OP9_STARTED,
  185. /**
  186. * op9 has been released
  187. */
  188. TEST_OP9_RELEASED
  189. };
  190. /**
  191. * The test result
  192. */
  193. enum Test result;
  194. /**
  195. * Function to call to start an operation once all
  196. * queues the operation is part of declare that the
  197. * operation can be activated.
  198. */
  199. static void
  200. start_cb (void *cls);
  201. /**
  202. * Function to cancel an operation (release all associated resources). This can
  203. * be because of a call to "GNUNET_TESTBED_operation_cancel" (before the
  204. * operation generated an event) or AFTER the operation generated an event due
  205. * to a call to "GNUNET_TESTBED_operation_done". Thus it is not guaranteed that
  206. * a callback to the 'OperationStart' preceeds the call to 'OperationRelease'.
  207. * Implementations of this function are expected to clean up whatever state is
  208. * in 'cls' and release all resources associated with the operation.
  209. */
  210. static void
  211. release_cb (void *cls);
  212. /**
  213. * Task to simulate artificial delay and change the test stage
  214. *
  215. * @param cls NULL
  216. */
  217. static void
  218. step (void *cls)
  219. {
  220. GNUNET_assert (NULL != step_task);
  221. step_task = NULL;
  222. switch (result)
  223. {
  224. case TEST_OP1_STARTED:
  225. GNUNET_TESTBED_operation_release_ (op1);
  226. GNUNET_TESTBED_operation_queue_reset_max_active_ (q1, 0);
  227. op3 = GNUNET_TESTBED_operation_create_ (&op3, &start_cb, &release_cb);
  228. GNUNET_TESTBED_operation_queue_insert2_ (q1, op3, 2);
  229. GNUNET_TESTBED_operation_queue_insert2_ (q2, op3, 2);
  230. GNUNET_TESTBED_operation_begin_wait_ (op3);
  231. op4 = GNUNET_TESTBED_operation_create_ (&op4, &start_cb, &release_cb);
  232. GNUNET_TESTBED_operation_queue_insert2_ (q1, op4, 2);
  233. GNUNET_TESTBED_operation_queue_insert2_ (q2, op4, 2);
  234. GNUNET_TESTBED_operation_begin_wait_ (op4);
  235. break;
  236. case TEST_OP1_RELEASED:
  237. result = TEST_PAUSE;
  238. GNUNET_TESTBED_operation_queue_reset_max_active_ (q1, 2);
  239. break;
  240. case TEST_OP2_STARTED:
  241. GNUNET_TESTBED_operation_release_ (op2);
  242. break;
  243. case TEST_OP3_STARTED:
  244. GNUNET_TESTBED_operation_release_ (op3);
  245. break;
  246. case TEST_OP4_STARTED:
  247. GNUNET_TESTBED_operation_release_ (op4);
  248. break;
  249. case TEST_OP6_RELEASED:
  250. op8 = GNUNET_TESTBED_operation_create_ (&op8, &start_cb, &release_cb);
  251. GNUNET_TESTBED_operation_queue_insert2_ (q1, op8, 2);
  252. GNUNET_TESTBED_operation_queue_insert2_ (q2, op8, 2);
  253. result = TEST_OP8_WAITING;
  254. GNUNET_TESTBED_operation_begin_wait_ (op8);
  255. break;
  256. case TEST_OP8_STARTED:
  257. GNUNET_TESTBED_operation_inactivate_ (op8);
  258. result = TEST_OP8_INACTIVE_1;
  259. step_task = GNUNET_SCHEDULER_add_delayed (STEP_DELAY, &step, NULL);
  260. break;
  261. case TEST_OP8_INACTIVE_1:
  262. GNUNET_TESTBED_operation_activate_ (op8);
  263. result = TEST_OP8_ACTIVE;
  264. op9 = GNUNET_TESTBED_operation_create_ (&op9, &start_cb, &release_cb);
  265. GNUNET_TESTBED_operation_queue_insert2_ (q1, op9, 1);
  266. GNUNET_TESTBED_operation_queue_insert2_ (q2, op9, 1);
  267. GNUNET_TESTBED_operation_begin_wait_ (op9);
  268. step_task = GNUNET_SCHEDULER_add_delayed (STEP_DELAY, &step, NULL);
  269. break;
  270. case TEST_OP8_ACTIVE:
  271. GNUNET_TESTBED_operation_inactivate_ (op8);
  272. /* op8 should be released by now due to above call */
  273. GNUNET_assert (TEST_OP8_RELEASED == result);
  274. break;
  275. case TEST_OP9_STARTED:
  276. GNUNET_TESTBED_operation_release_ (op9);
  277. break;
  278. default:
  279. GNUNET_assert (0);
  280. }
  281. }
  282. /**
  283. * Function to call to start an operation once all
  284. * queues the operation is part of declare that the
  285. * operation can be activated.
  286. */
  287. static void
  288. start_cb (void *cls)
  289. {
  290. switch (result)
  291. {
  292. case TEST_INIT:
  293. GNUNET_assert (&op1 == cls);
  294. result = TEST_OP1_STARTED;
  295. GNUNET_assert (NULL == step_task);
  296. step_task =
  297. GNUNET_SCHEDULER_add_delayed (STEP_DELAY, &step, NULL);
  298. break;
  299. case TEST_PAUSE:
  300. GNUNET_assert (&op2 == cls);
  301. result = TEST_OP2_STARTED;
  302. GNUNET_assert (NULL == step_task);
  303. step_task =
  304. GNUNET_SCHEDULER_add_delayed (STEP_DELAY, &step, NULL);
  305. break;
  306. case TEST_OP2_RELEASED:
  307. GNUNET_assert (&op3 == cls);
  308. result = TEST_OP3_STARTED;
  309. GNUNET_assert (NULL == step_task);
  310. step_task =
  311. GNUNET_SCHEDULER_add_delayed (STEP_DELAY, &step, NULL);
  312. break;
  313. case TEST_OP3_RELEASED:
  314. GNUNET_assert (&op4 == cls);
  315. result = TEST_OP4_STARTED;
  316. GNUNET_assert (NULL == step_task);
  317. step_task =
  318. GNUNET_SCHEDULER_add_delayed (STEP_DELAY, &step, NULL);
  319. break;
  320. case TEST_OP4_RELEASED:
  321. {
  322. static int nops;
  323. nops++;
  324. if (nops == 3)
  325. {
  326. result = TEST_OP5_6_7_STARTED;
  327. GNUNET_TESTBED_operation_release_ (op5);
  328. op5 = NULL;
  329. }
  330. }
  331. break;
  332. case TEST_OP7_RELEASED:
  333. GNUNET_assert (&op8 == cls);
  334. result = TEST_OP8_STARTED;
  335. step_task = GNUNET_SCHEDULER_add_delayed (STEP_DELAY, &step, NULL);
  336. break;
  337. case TEST_OP8_RELEASED:
  338. GNUNET_assert (&op9 == cls);
  339. result = TEST_OP9_STARTED;
  340. step_task = GNUNET_SCHEDULER_add_delayed (STEP_DELAY, &step, NULL);
  341. break;
  342. default:
  343. GNUNET_assert (0);
  344. }
  345. }
  346. /**
  347. * Function to cancel an operation (release all associated resources). This can
  348. * be because of a call to "GNUNET_TESTBED_operation_cancel" (before the
  349. * operation generated an event) or AFTER the operation generated an event due
  350. * to a call to "GNUNET_TESTBED_operation_done". Thus it is not guaranteed that
  351. * a callback to the 'OperationStart' preceeds the call to 'OperationRelease'.
  352. * Implementations of this function are expected to clean up whatever state is
  353. * in 'cls' and release all resources associated with the operation.
  354. */
  355. static void
  356. release_cb (void *cls)
  357. {
  358. switch (result)
  359. {
  360. case TEST_OP1_STARTED:
  361. GNUNET_assert (&op1 == cls);
  362. result = TEST_OP1_RELEASED;
  363. op1 = NULL;
  364. step_task =
  365. GNUNET_SCHEDULER_add_delayed (STEP_DELAY, &step, NULL);
  366. break;
  367. case TEST_OP2_STARTED:
  368. GNUNET_assert (&op2 == cls);
  369. result = TEST_OP2_RELEASED;
  370. GNUNET_assert (NULL == step_task);
  371. break;
  372. case TEST_OP3_STARTED:
  373. GNUNET_assert (&op3 == cls);
  374. result = TEST_OP3_RELEASED;
  375. GNUNET_assert (NULL == step_task);
  376. break;
  377. case TEST_OP4_STARTED:
  378. GNUNET_assert (&op4 == cls);
  379. result = TEST_OP4_RELEASED;
  380. GNUNET_assert (NULL == step_task);
  381. op5 = GNUNET_TESTBED_operation_create_ (&op5, &start_cb, &release_cb);
  382. GNUNET_TESTBED_operation_queue_insert2_ (q1, op5, 1);
  383. GNUNET_TESTBED_operation_begin_wait_ (op5);
  384. op6 = GNUNET_TESTBED_operation_create_ (&op6, &start_cb, &release_cb);
  385. GNUNET_TESTBED_operation_queue_insert2_ (q2, op6, 1);
  386. GNUNET_TESTBED_operation_begin_wait_ (op6);
  387. op7 = GNUNET_TESTBED_operation_create_ (&op7, &start_cb, &release_cb);
  388. GNUNET_TESTBED_operation_queue_insert2_ (q1, op7, 1);
  389. GNUNET_TESTBED_operation_queue_insert2_ (q2, op7, 1);
  390. GNUNET_TESTBED_operation_begin_wait_ (op7);
  391. break;
  392. case TEST_OP5_6_7_STARTED:
  393. result = TEST_OP5_RELEASED;
  394. op5 = NULL;
  395. GNUNET_TESTBED_operation_release_ (op6);
  396. break;
  397. case TEST_OP5_RELEASED:
  398. op6 = NULL;
  399. result = TEST_OP6_RELEASED;
  400. GNUNET_TESTBED_operation_inactivate_ (op7);
  401. step_task = GNUNET_SCHEDULER_add_now (&step, NULL);
  402. break;
  403. case TEST_OP8_WAITING:
  404. GNUNET_assert (&op7 == cls);
  405. op7 = NULL;
  406. result = TEST_OP7_RELEASED;
  407. break;
  408. case TEST_OP8_ACTIVE:
  409. result = TEST_OP8_RELEASED;
  410. op8 = NULL;
  411. break;
  412. case TEST_OP9_STARTED:
  413. GNUNET_assert (&op9 == cls);
  414. result = TEST_OP9_RELEASED;
  415. GNUNET_TESTBED_operation_queue_destroy_ (q1);
  416. GNUNET_TESTBED_operation_queue_destroy_ (q2);
  417. q1 = NULL;
  418. q2 = NULL;
  419. break;
  420. default:
  421. GNUNET_assert (0);
  422. }
  423. }
  424. /**
  425. * Main run function.
  426. *
  427. * @param cls NULL
  428. * @param args arguments passed to GNUNET_PROGRAM_run
  429. * @param cfgfile the path to configuration file
  430. * @param cfg the configuration file handle
  431. */
  432. static void
  433. run (void *cls, char *const *args, const char *cfgfile,
  434. const struct GNUNET_CONFIGURATION_Handle *config)
  435. {
  436. q1 = GNUNET_TESTBED_operation_queue_create_ (OPERATION_QUEUE_TYPE_FIXED, 1);
  437. GNUNET_assert (NULL != q1);
  438. q2 = GNUNET_TESTBED_operation_queue_create_ (OPERATION_QUEUE_TYPE_FIXED, 2);
  439. GNUNET_assert (NULL != q2);
  440. op1 = GNUNET_TESTBED_operation_create_ (&op1, start_cb, release_cb);
  441. GNUNET_assert (NULL != op1);
  442. op2 = GNUNET_TESTBED_operation_create_ (&op2, start_cb, release_cb);
  443. GNUNET_TESTBED_operation_queue_insert_ (q1, op1);
  444. GNUNET_TESTBED_operation_queue_insert_ (q2, op1);
  445. GNUNET_TESTBED_operation_begin_wait_ (op1);
  446. GNUNET_TESTBED_operation_queue_insert_ (q1, op2);
  447. GNUNET_TESTBED_operation_queue_insert_ (q2, op2);
  448. GNUNET_TESTBED_operation_begin_wait_ (op2);
  449. result = TEST_INIT;
  450. }
  451. /**
  452. * Main function
  453. */
  454. int
  455. main (int argc, char **argv)
  456. {
  457. int ret;
  458. char *const argv2[] =
  459. { "test_testbed_api_operations", "-c", "test_testbed_api.conf", NULL };
  460. struct GNUNET_GETOPT_CommandLineOption options[] =
  461. { GNUNET_GETOPT_OPTION_END };
  462. ret =
  463. GNUNET_PROGRAM_run ((sizeof (argv2) / sizeof (char *)) - 1, argv2,
  464. "test_testbed_api_operations", "nohelp", options,
  465. &run, NULL);
  466. if ((GNUNET_OK != ret) || (TEST_OP9_RELEASED != result))
  467. return 1;
  468. op1 = NULL;
  469. op2 = NULL;
  470. op3 = NULL;
  471. op4 = NULL;
  472. op5 = NULL;
  473. op6 = NULL;
  474. op7 = NULL;
  475. op8 = NULL;
  476. op9 = NULL;
  477. q1 = NULL;
  478. q2 = NULL;
  479. return 0;
  480. }
  481. /* end of test_testbed_api_operations.c */