test_exponential_backoff.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483
  1. /*
  2. This file is part of GNUnet.
  3. Copyright (C) 2009 Christian Grothoff (and other contributing authors)
  4. GNUnet is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published
  6. by the Free Software Foundation; either version 3, or (at your
  7. 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. General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with GNUnet; see the file COPYING. If not, write to the
  14. Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  15. Boston, MA 02111-1307, USA.
  16. */
  17. /**
  18. * @file arm/test_exponential_backoff.c
  19. * @brief testcase for gnunet-service-arm.c
  20. */
  21. #include "platform.h"
  22. #include "gnunet_arm_service.h"
  23. #include "gnunet_util_lib.h"
  24. #include "gnunet_protocols.h"
  25. #define LOG(...) GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, __VA_ARGS__)
  26. #define START_ARM GNUNET_YES
  27. #define LOG_BACKOFF GNUNET_NO
  28. #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 10)
  29. #define SERVICE_TEST_TIMEOUT GNUNET_TIME_UNIT_FOREVER_REL
  30. #define FIVE_MILLISECONDS GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, 5)
  31. #define SERVICE "do-nothing"
  32. #define BINARY "mockup-service"
  33. #define CFGFILENAME "test_arm_api_data2.conf"
  34. static const struct GNUNET_CONFIGURATION_Handle *cfg;
  35. static struct GNUNET_ARM_Handle *arm;
  36. static struct GNUNET_ARM_MonitorHandle *mon;
  37. static int ok = 1;
  38. static int phase = 0;
  39. static int trialCount;
  40. static struct GNUNET_TIME_Absolute startedWaitingAt;
  41. struct GNUNET_TIME_Relative waitedFor;
  42. struct GNUNET_TIME_Relative waitedFor_prev;
  43. #if LOG_BACKOFF
  44. static FILE *killLogFilePtr;
  45. static char *killLogFileName;
  46. #endif
  47. typedef void (*GNUNET_CLIENT_ShutdownTask) (void *cls, int reason);
  48. /**
  49. * Context for handling the shutdown of a service.
  50. */
  51. struct ShutdownContext
  52. {
  53. /**
  54. * Connection to the service that is being shutdown.
  55. */
  56. struct GNUNET_CLIENT_Connection *sock;
  57. /**
  58. * Time allowed for shutdown to happen.
  59. */
  60. struct GNUNET_TIME_Absolute timeout;
  61. /**
  62. * Task set up to cancel the shutdown request on timeout.
  63. */
  64. struct GNUNET_SCHEDULER_Task * cancel_task;
  65. /**
  66. * Task to call once shutdown complete
  67. */
  68. GNUNET_CLIENT_ShutdownTask cont;
  69. /**
  70. * Closure for shutdown continuation
  71. */
  72. void *cont_cls;
  73. /**
  74. * We received a confirmation that the service will shut down.
  75. */
  76. int confirmed;
  77. };
  78. /**
  79. * Handler receiving response to service shutdown requests.
  80. * We expect it to be called with NULL, since the service that
  81. * we are shutting down will just die without replying.
  82. *
  83. * @param cls closure
  84. * @param msg NULL, indicating socket closure.
  85. */
  86. static void
  87. service_shutdown_handler (void *cls, const struct GNUNET_MessageHeader *msg)
  88. {
  89. struct ShutdownContext *shutdown_ctx = cls;
  90. if (NULL == msg)
  91. {
  92. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Service shutdown complete.\n");
  93. if (shutdown_ctx->cont != NULL)
  94. shutdown_ctx->cont (shutdown_ctx->cont_cls, GNUNET_NO);
  95. GNUNET_SCHEDULER_cancel (shutdown_ctx->cancel_task);
  96. GNUNET_CLIENT_disconnect (shutdown_ctx->sock);
  97. GNUNET_free (shutdown_ctx);
  98. return;
  99. }
  100. GNUNET_assert (0);
  101. }
  102. /**
  103. * Shutting down took too long, cancel receive and return error.
  104. *
  105. * @param cls closure
  106. * @param tc context information (why was this task triggered now)
  107. */
  108. void
  109. service_shutdown_cancel (void *cls,
  110. const struct GNUNET_SCHEDULER_TaskContext *tc)
  111. {
  112. struct ShutdownContext *shutdown_ctx = cls;
  113. GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "service_shutdown_cancel called!\n");
  114. shutdown_ctx->cont (shutdown_ctx->cont_cls, GNUNET_SYSERR);
  115. GNUNET_CLIENT_disconnect (shutdown_ctx->sock);
  116. GNUNET_free (shutdown_ctx);
  117. }
  118. /**
  119. * If possible, write a shutdown message to the target
  120. * buffer and destroy the client connection.
  121. *
  122. * @param cls the "struct GNUNET_CLIENT_Connection" to destroy
  123. * @param size number of bytes available in buf
  124. * @param buf NULL on error, otherwise target buffer
  125. * @return number of bytes written to buf
  126. */
  127. static size_t
  128. write_shutdown (void *cls, size_t size, void *buf)
  129. {
  130. struct GNUNET_MessageHeader *msg;
  131. struct ShutdownContext *shutdown_ctx = cls;
  132. if (size < sizeof (struct GNUNET_MessageHeader))
  133. {
  134. LOG ("Failed to send a shutdown request\n");
  135. shutdown_ctx->cont (shutdown_ctx->cont_cls, GNUNET_SYSERR);
  136. GNUNET_CLIENT_disconnect (shutdown_ctx->sock);
  137. GNUNET_free (shutdown_ctx);
  138. return 0; /* client disconnected */
  139. }
  140. GNUNET_CLIENT_receive (shutdown_ctx->sock, &service_shutdown_handler,
  141. shutdown_ctx, GNUNET_TIME_UNIT_FOREVER_REL);
  142. shutdown_ctx->cancel_task = GNUNET_SCHEDULER_add_delayed (
  143. GNUNET_TIME_absolute_get_remaining (shutdown_ctx->timeout),
  144. &service_shutdown_cancel, shutdown_ctx);
  145. msg = (struct GNUNET_MessageHeader *) buf;
  146. msg->type = htons (GNUNET_MESSAGE_TYPE_ARM_STOP);
  147. msg->size = htons (sizeof (struct GNUNET_MessageHeader));
  148. strcpy ((char *) &msg[1], SERVICE);
  149. LOG ("Sent a shutdown request\n");
  150. return sizeof (struct GNUNET_MessageHeader) + strlen (SERVICE) + 1;
  151. }
  152. /**
  153. * Request that the service should shutdown.
  154. * Afterwards, the connection will automatically be
  155. * disconnected. Hence the "sock" should not
  156. * be used by the caller after this call
  157. * (calling this function frees "sock" after a while).
  158. *
  159. * @param sock the socket connected to the service
  160. * @param timeout how long to wait before giving up on transmission
  161. * @param cont continuation to call once the service is really down
  162. * @param cont_cls closure for continuation
  163. *
  164. */
  165. static void
  166. do_nothing_service_shutdown (struct GNUNET_CLIENT_Connection *sock,
  167. struct GNUNET_TIME_Relative timeout,
  168. GNUNET_CLIENT_ShutdownTask cont, void *cont_cls)
  169. {
  170. struct ShutdownContext *shutdown_ctx;
  171. shutdown_ctx = GNUNET_new (struct ShutdownContext);
  172. shutdown_ctx->cont = cont;
  173. shutdown_ctx->cont_cls = cont_cls;
  174. shutdown_ctx->sock = sock;
  175. shutdown_ctx->timeout = GNUNET_TIME_relative_to_absolute (timeout);
  176. GNUNET_CLIENT_notify_transmit_ready (sock,
  177. sizeof (struct GNUNET_MessageHeader) + strlen (SERVICE) + 1,
  178. timeout, GNUNET_NO, &write_shutdown,
  179. shutdown_ctx);
  180. }
  181. static void
  182. kill_task (void *cbData, const struct GNUNET_SCHEDULER_TaskContext *tc);
  183. static void
  184. shutdown_cont (void *cls, int reason)
  185. {
  186. if (GNUNET_NO != reason)
  187. {
  188. /* Re-try shutdown */
  189. LOG ("do-nothing didn't die, trying again\n");
  190. GNUNET_SCHEDULER_add_now (kill_task, NULL);
  191. return;
  192. }
  193. startedWaitingAt = GNUNET_TIME_absolute_get ();
  194. LOG ("do-nothing is dead, starting the countdown\n");
  195. }
  196. static void
  197. kill_task (void *cbData, const struct GNUNET_SCHEDULER_TaskContext *tc)
  198. {
  199. static struct GNUNET_CLIENT_Connection *doNothingConnection = NULL;
  200. if (NULL != cbData)
  201. {
  202. waitedFor = GNUNET_TIME_absolute_get_duration (startedWaitingAt);
  203. LOG ("Waited for: %s\n",
  204. GNUNET_STRINGS_relative_time_to_string (waitedFor, GNUNET_YES));
  205. }
  206. else
  207. {
  208. waitedFor.rel_value_us = 0;
  209. }
  210. /* Connect to the doNothing task */
  211. doNothingConnection = GNUNET_CLIENT_connect (SERVICE, cfg);
  212. GNUNET_assert (doNothingConnection != NULL);
  213. if (trialCount == 12)
  214. waitedFor_prev = waitedFor;
  215. else if (trialCount == 13)
  216. {
  217. GNUNET_CLIENT_disconnect (doNothingConnection);
  218. GNUNET_ARM_request_service_stop (arm, SERVICE, TIMEOUT, NULL, NULL);
  219. if (waitedFor_prev.rel_value_us >= waitedFor.rel_value_us)
  220. ok = 9;
  221. else
  222. ok = 0;
  223. trialCount += 1;
  224. return;
  225. }
  226. trialCount += 1;
  227. /* Use the created connection to kill the doNothingTask */
  228. do_nothing_service_shutdown (doNothingConnection,
  229. TIMEOUT, &shutdown_cont, NULL);
  230. }
  231. static void
  232. trigger_disconnect (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
  233. {
  234. GNUNET_ARM_disconnect_and_free (arm);
  235. GNUNET_ARM_monitor_disconnect_and_free (mon);
  236. }
  237. static void
  238. arm_stop_cb (void *cls, enum GNUNET_ARM_RequestStatus status, const char *servicename, enum GNUNET_ARM_Result result)
  239. {
  240. GNUNET_break (status == GNUNET_ARM_REQUEST_SENT_OK);
  241. GNUNET_break (result == GNUNET_ARM_RESULT_STOPPED);
  242. LOG ("ARM service stopped\n");
  243. GNUNET_SCHEDULER_add_now (trigger_disconnect, NULL);
  244. }
  245. static void
  246. srv_status (void *cls, const char *service, enum GNUNET_ARM_ServiceStatus status)
  247. {
  248. LOG ("Service %s is %u, phase %u\n", service, status, phase);
  249. if (status == GNUNET_ARM_SERVICE_MONITORING_STARTED)
  250. {
  251. phase++;
  252. GNUNET_ARM_request_service_start (arm, SERVICE,
  253. GNUNET_OS_INHERIT_STD_OUT_AND_ERR, TIMEOUT, NULL, NULL);
  254. return;
  255. }
  256. if (phase == 1)
  257. {
  258. GNUNET_break (status == GNUNET_ARM_SERVICE_STARTING);
  259. GNUNET_break (0 == strcasecmp (service, SERVICE));
  260. GNUNET_break (phase == 1);
  261. LOG ("do-nothing is starting\n");
  262. phase++;
  263. ok = 1;
  264. GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS, &kill_task, NULL);
  265. }
  266. else if ((phase == 2) && (strcasecmp (SERVICE, service) == 0))
  267. {
  268. /* We passively monitor ARM for status updates. ARM should tell us
  269. * when do-nothing dies (no need to run a service upness test ourselves).
  270. */
  271. if (status == GNUNET_ARM_SERVICE_STARTING)
  272. {
  273. LOG ("do-nothing is starting\n");
  274. GNUNET_SCHEDULER_add_now (kill_task, &ok);
  275. }
  276. else if ((status == GNUNET_ARM_SERVICE_STOPPED) && (trialCount == 14))
  277. {
  278. phase++;
  279. GNUNET_ARM_request_service_stop (arm, "arm", TIMEOUT, arm_stop_cb, NULL);
  280. }
  281. }
  282. }
  283. static void
  284. arm_start_cb (void *cls, enum GNUNET_ARM_RequestStatus status, const char *servicename, enum GNUNET_ARM_Result result)
  285. {
  286. GNUNET_break (status == GNUNET_ARM_REQUEST_SENT_OK);
  287. GNUNET_break (result == GNUNET_ARM_RESULT_STARTING);
  288. GNUNET_break (phase == 0);
  289. LOG ("Sent 'START' request for arm to ARM %s\n",
  290. (status == GNUNET_ARM_REQUEST_SENT_OK) ? "successfully" : "unsuccessfully");
  291. }
  292. static void
  293. task (void *cls, char *const *args, const char *cfgfile,
  294. const struct GNUNET_CONFIGURATION_Handle *c)
  295. {
  296. char *armconfig;
  297. cfg = c;
  298. if (NULL != cfgfile)
  299. {
  300. if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_filename (cfg, "arm",
  301. "CONFIG", &armconfig))
  302. {
  303. GNUNET_CONFIGURATION_set_value_string ((struct GNUNET_CONFIGURATION_Handle
  304. *) cfg, "arm", "CONFIG",
  305. cfgfile);
  306. }
  307. else
  308. GNUNET_free (armconfig);
  309. }
  310. arm = GNUNET_ARM_connect (cfg, NULL, NULL);
  311. if (NULL != arm)
  312. {
  313. mon = GNUNET_ARM_monitor (cfg, &srv_status, NULL);
  314. if (NULL != mon)
  315. {
  316. #if START_ARM
  317. GNUNET_ARM_request_service_start (arm, "arm",
  318. GNUNET_OS_INHERIT_STD_OUT_AND_ERR, GNUNET_TIME_UNIT_ZERO, arm_start_cb, NULL);
  319. #else
  320. arm_start_cb (NULL, arm, GNUNET_ARM_REQUEST_SENT_OK, "arm", GNUNET_ARM_SERVICE_STARTING);
  321. #endif
  322. }
  323. else
  324. {
  325. GNUNET_ARM_disconnect_and_free (arm);
  326. arm = NULL;
  327. }
  328. }
  329. }
  330. static int
  331. check ()
  332. {
  333. char *const argv[] = {
  334. "test-exponential-backoff",
  335. "-c", CFGFILENAME,
  336. NULL
  337. };
  338. struct GNUNET_GETOPT_CommandLineOption options[] = {
  339. GNUNET_GETOPT_OPTION_END
  340. };
  341. /* Running ARM and running the do_nothing task */
  342. GNUNET_assert (GNUNET_OK ==
  343. GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1,
  344. argv, "test-exponential-backoff",
  345. "nohelp", options, &task, NULL));
  346. return ok;
  347. }
  348. static int
  349. init ()
  350. {
  351. struct GNUNET_CONFIGURATION_Handle *cfg;
  352. char pwd[PATH_MAX];
  353. char *binary;
  354. cfg = GNUNET_CONFIGURATION_create ();
  355. if (GNUNET_OK != GNUNET_CONFIGURATION_parse (cfg,
  356. "test_arm_api_data.conf"))
  357. return GNUNET_SYSERR;
  358. if (NULL == getcwd (pwd, PATH_MAX))
  359. return GNUNET_SYSERR;
  360. GNUNET_assert (0 < GNUNET_asprintf (&binary, "%s/%s", pwd, BINARY));
  361. GNUNET_CONFIGURATION_set_value_string (cfg, SERVICE, "BINARY", binary);
  362. GNUNET_free (binary);
  363. if (GNUNET_OK != GNUNET_CONFIGURATION_write (cfg, CFGFILENAME))
  364. {
  365. GNUNET_CONFIGURATION_destroy (cfg);
  366. return GNUNET_SYSERR;
  367. }
  368. GNUNET_CONFIGURATION_destroy (cfg);
  369. #if LOG_BACKOFF
  370. killLogFileName = GNUNET_DISK_mktemp ("exponential-backoff-waiting.log");
  371. if (NULL == (killLogFilePtr = FOPEN (killLogFileName, "w")))
  372. {
  373. GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "fopen",
  374. killLogFileName);
  375. GNUNET_free (killLogFileName);
  376. return GNUNET_SYSERR;
  377. }
  378. #endif
  379. return GNUNET_OK;
  380. }
  381. static void
  382. houseKeep ()
  383. {
  384. #if LOG_BACKOFF
  385. GNUNET_assert (0 == fclose (killLogFilePtr));
  386. GNUNET_free (killLogFileName);
  387. #endif
  388. (void) unlink (CFGFILENAME);
  389. }
  390. int
  391. main (int argc, char *argv[])
  392. {
  393. int ret;
  394. GNUNET_log_setup ("test-exponential-backoff",
  395. "WARNING",
  396. NULL);
  397. if (GNUNET_OK != init ())
  398. return 1;
  399. ret = check ();
  400. houseKeep ();
  401. return ret;
  402. }
  403. /* end of test_exponential_backoff.c */