test_exponential_backoff.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443
  1. /*
  2. This file is part of GNUnet.
  3. (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_client_lib.h"
  24. #include "gnunet_configuration_lib.h"
  25. #include "gnunet_program_lib.h"
  26. #include "gnunet_protocols.h"
  27. #define VERBOSE GNUNET_NO
  28. #define START_ARM GNUNET_YES
  29. #define LOG_BACKOFF GNUNET_NO
  30. #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 10)
  31. #define SERVICE_TEST_TIMEOUT GNUNET_TIME_UNIT_FOREVER_REL
  32. #define FIVE_MILLISECONDS GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, 5)
  33. static const struct GNUNET_CONFIGURATION_Handle *cfg;
  34. static struct GNUNET_ARM_Handle *arm;
  35. static int ok = 1;
  36. static int trialCount;
  37. static struct GNUNET_TIME_Absolute startedWaitingAt;
  38. struct GNUNET_TIME_Relative waitedFor;
  39. #if LOG_BACKOFF
  40. static FILE *killLogFilePtr;
  41. static char *killLogFileName;
  42. #endif
  43. /**
  44. * Context for handling the shutdown of a service.
  45. */
  46. struct ShutdownContext
  47. {
  48. /**
  49. * Connection to the service that is being shutdown.
  50. */
  51. struct GNUNET_CLIENT_Connection *sock;
  52. /**
  53. * Time allowed for shutdown to happen.
  54. */
  55. struct GNUNET_TIME_Absolute timeout;
  56. /**
  57. * Task set up to cancel the shutdown request on timeout.
  58. */
  59. GNUNET_SCHEDULER_TaskIdentifier cancel_task;
  60. /**
  61. * Task to call once shutdown complete
  62. */
  63. GNUNET_CLIENT_ShutdownTask cont;
  64. /**
  65. * Closure for shutdown continuation
  66. */
  67. void *cont_cls;
  68. /**
  69. * We received a confirmation that the service will shut down.
  70. */
  71. int confirmed;
  72. };
  73. /**
  74. * Handler receiving response to service shutdown requests.
  75. * First call with NULL: service misbehaving, or something.
  76. * First call with GNUNET_MESSAGE_TYPE_ARM_SHUTDOWN_ACK:
  77. * - service will shutdown
  78. * Second call with NULL:
  79. * - service has now really shut down.
  80. *
  81. * @param cls closure
  82. * @param msg NULL, indicating socket closure.
  83. */
  84. static void
  85. service_shutdown_handler (void *cls, const struct GNUNET_MessageHeader *msg)
  86. {
  87. struct ShutdownContext *shutdown_ctx = cls;
  88. if ((msg == NULL) && (shutdown_ctx->confirmed != GNUNET_YES))
  89. {
  90. /* Means the other side closed the connection and never confirmed a shutdown */
  91. GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
  92. "Service handle shutdown before ACK!\n");
  93. if (shutdown_ctx->cont != NULL)
  94. shutdown_ctx->cont (shutdown_ctx->cont_cls, GNUNET_SYSERR);
  95. GNUNET_SCHEDULER_cancel (shutdown_ctx->cancel_task);
  96. GNUNET_CLIENT_disconnect (shutdown_ctx->sock, GNUNET_NO);
  97. GNUNET_free (shutdown_ctx);
  98. }
  99. else if ((msg == NULL) && (shutdown_ctx->confirmed == GNUNET_YES))
  100. {
  101. #if VERBOSE
  102. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Service shutdown complete.\n");
  103. #endif
  104. if (shutdown_ctx->cont != NULL)
  105. shutdown_ctx->cont (shutdown_ctx->cont_cls, GNUNET_NO);
  106. GNUNET_SCHEDULER_cancel (shutdown_ctx->cancel_task);
  107. GNUNET_CLIENT_disconnect (shutdown_ctx->sock, GNUNET_NO);
  108. GNUNET_free (shutdown_ctx);
  109. }
  110. else
  111. {
  112. GNUNET_assert (ntohs (msg->size) == sizeof (struct GNUNET_MessageHeader));
  113. switch (ntohs (msg->type))
  114. {
  115. case GNUNET_MESSAGE_TYPE_ARM_SHUTDOWN_ACK:
  116. #if VERBOSE
  117. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  118. "Received confirmation for service shutdown.\n");
  119. #endif
  120. shutdown_ctx->confirmed = GNUNET_YES;
  121. GNUNET_CLIENT_receive (shutdown_ctx->sock, &service_shutdown_handler,
  122. shutdown_ctx, GNUNET_TIME_UNIT_FOREVER_REL);
  123. break;
  124. default: /* Fall through */
  125. GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Service shutdown refused!\n");
  126. if (shutdown_ctx->cont != NULL)
  127. shutdown_ctx->cont (shutdown_ctx->cont_cls, GNUNET_YES);
  128. GNUNET_SCHEDULER_cancel (shutdown_ctx->cancel_task);
  129. GNUNET_CLIENT_disconnect (shutdown_ctx->sock, GNUNET_NO);
  130. GNUNET_free (shutdown_ctx);
  131. break;
  132. }
  133. }
  134. }
  135. /**
  136. * Shutting down took too long, cancel receive and return error.
  137. *
  138. * @param cls closure
  139. * @param tc context information (why was this task triggered now)
  140. */
  141. void
  142. service_shutdown_cancel (void *cls,
  143. const struct GNUNET_SCHEDULER_TaskContext *tc)
  144. {
  145. struct ShutdownContext *shutdown_ctx = cls;
  146. GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "service_shutdown_cancel called!\n");
  147. shutdown_ctx->cont (shutdown_ctx->cont_cls, GNUNET_SYSERR);
  148. GNUNET_CLIENT_disconnect (shutdown_ctx->sock, GNUNET_NO);
  149. GNUNET_free (shutdown_ctx);
  150. }
  151. /**
  152. * If possible, write a shutdown message to the target
  153. * buffer and destroy the client connection.
  154. *
  155. * @param cls the "struct GNUNET_CLIENT_Connection" to destroy
  156. * @param size number of bytes available in buf
  157. * @param buf NULL on error, otherwise target buffer
  158. * @return number of bytes written to buf
  159. */
  160. static size_t
  161. write_shutdown (void *cls, size_t size, void *buf)
  162. {
  163. struct GNUNET_MessageHeader *msg;
  164. struct ShutdownContext *shutdown_ctx = cls;
  165. if (size < sizeof (struct GNUNET_MessageHeader))
  166. {
  167. GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
  168. _("Failed to transmit shutdown request to client.\n"));
  169. shutdown_ctx->cont (shutdown_ctx->cont_cls, GNUNET_SYSERR);
  170. GNUNET_CLIENT_disconnect (shutdown_ctx->sock, GNUNET_NO);
  171. GNUNET_free (shutdown_ctx);
  172. return 0; /* client disconnected */
  173. }
  174. GNUNET_CLIENT_receive (shutdown_ctx->sock, &service_shutdown_handler,
  175. shutdown_ctx, GNUNET_TIME_UNIT_FOREVER_REL);
  176. shutdown_ctx->cancel_task =
  177. GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_absolute_get_remaining
  178. (shutdown_ctx->timeout),
  179. &service_shutdown_cancel, shutdown_ctx);
  180. msg = (struct GNUNET_MessageHeader *) buf;
  181. msg->type = htons (GNUNET_MESSAGE_TYPE_ARM_SHUTDOWN);
  182. msg->size = htons (sizeof (struct GNUNET_MessageHeader));
  183. return sizeof (struct GNUNET_MessageHeader);
  184. }
  185. /**
  186. * Request that the service should shutdown.
  187. * Afterwards, the connection will automatically be
  188. * disconnected. Hence the "sock" should not
  189. * be used by the caller after this call
  190. * (calling this function frees "sock" after a while).
  191. *
  192. * @param sock the socket connected to the service
  193. * @param timeout how long to wait before giving up on transmission
  194. * @param cont continuation to call once the service is really down
  195. * @param cont_cls closure for continuation
  196. *
  197. */
  198. static void
  199. arm_service_shutdown (struct GNUNET_CLIENT_Connection *sock,
  200. struct GNUNET_TIME_Relative timeout,
  201. GNUNET_CLIENT_ShutdownTask cont, void *cont_cls)
  202. {
  203. struct ShutdownContext *shutdown_ctx;
  204. shutdown_ctx = GNUNET_malloc (sizeof (struct ShutdownContext));
  205. shutdown_ctx->cont = cont;
  206. shutdown_ctx->cont_cls = cont_cls;
  207. shutdown_ctx->sock = sock;
  208. shutdown_ctx->timeout = GNUNET_TIME_relative_to_absolute (timeout);
  209. GNUNET_CLIENT_notify_transmit_ready (sock,
  210. sizeof (struct GNUNET_MessageHeader),
  211. timeout, GNUNET_NO, &write_shutdown,
  212. shutdown_ctx);
  213. }
  214. static void
  215. arm_notify_stop (void *cls, int success)
  216. {
  217. GNUNET_assert (success == GNUNET_NO);
  218. #if START_ARM
  219. GNUNET_ARM_stop_service (arm, "arm", TIMEOUT, NULL, NULL);
  220. #endif
  221. }
  222. static void
  223. kill_task (void *cbData, const struct GNUNET_SCHEDULER_TaskContext *tc);
  224. static void
  225. do_nothing_notify (void *cls, int success)
  226. {
  227. GNUNET_assert (success == GNUNET_YES);
  228. ok = 1;
  229. GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS, &kill_task, NULL);
  230. }
  231. static void
  232. arm_notify (void *cls, int success)
  233. {
  234. GNUNET_assert (success == GNUNET_YES);
  235. GNUNET_ARM_start_service (arm, "do-nothing", TIMEOUT, &do_nothing_notify,
  236. NULL);
  237. }
  238. static void
  239. kill_task (void *cbData, const struct GNUNET_SCHEDULER_TaskContext *tc);
  240. static void
  241. do_nothing_restarted_notify_task (void *cls,
  242. const struct GNUNET_SCHEDULER_TaskContext *tc)
  243. {
  244. static char a;
  245. trialCount++;
  246. #if LOG_BACKOFF
  247. if ((tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
  248. {
  249. fprintf (killLogFilePtr, "%d.Reason is shutdown!\n", trialCount);
  250. }
  251. else if ((tc->reason & GNUNET_SCHEDULER_REASON_TIMEOUT) != 0)
  252. {
  253. fprintf (killLogFilePtr, "%d.Reason is timeout!\n", trialCount);
  254. }
  255. else if ((tc->reason & GNUNET_SCHEDULER_REASON_PREREQ_DONE) != 0)
  256. {
  257. fprintf (killLogFilePtr, "%d.Service is running!\n", trialCount);
  258. }
  259. #endif
  260. GNUNET_SCHEDULER_add_now (&kill_task, &a);
  261. }
  262. static void
  263. do_test (void *cbData, const struct GNUNET_SCHEDULER_TaskContext *tc)
  264. {
  265. GNUNET_CLIENT_service_test ("do-nothing", cfg, TIMEOUT,
  266. &do_nothing_restarted_notify_task, NULL);
  267. }
  268. static void
  269. shutdown_cont (void *cls, int reason)
  270. {
  271. trialCount++;
  272. startedWaitingAt = GNUNET_TIME_absolute_get ();
  273. GNUNET_SCHEDULER_add_delayed (waitedFor, &do_test, NULL);
  274. }
  275. static void
  276. kill_task (void *cbData, const struct GNUNET_SCHEDULER_TaskContext *tc)
  277. {
  278. static struct GNUNET_CLIENT_Connection *doNothingConnection = NULL;
  279. if (NULL != cbData)
  280. {
  281. waitedFor = GNUNET_TIME_absolute_get_duration (startedWaitingAt);
  282. #if LOG_BACKOFF
  283. fprintf (killLogFilePtr, "Waited for: %llu ms\n",
  284. (unsigned long long) waitedFor.rel_value);
  285. #endif
  286. }
  287. else
  288. {
  289. waitedFor.rel_value = 0;
  290. }
  291. /* Connect to the doNothing task */
  292. doNothingConnection = GNUNET_CLIENT_connect ("do-nothing", cfg);
  293. GNUNET_assert (doNothingConnection != NULL);
  294. if (trialCount == 12)
  295. {
  296. GNUNET_CLIENT_disconnect (doNothingConnection, GNUNET_NO);
  297. GNUNET_ARM_stop_service (arm, "do-nothing", TIMEOUT, &arm_notify_stop,
  298. NULL);
  299. ok = 0;
  300. return;
  301. }
  302. /* Use the created connection to kill the doNothingTask */
  303. arm_service_shutdown (doNothingConnection, TIMEOUT, &shutdown_cont, NULL);
  304. }
  305. static void
  306. task (void *cls, char *const *args, const char *cfgfile,
  307. const struct GNUNET_CONFIGURATION_Handle *c)
  308. {
  309. cfg = c;
  310. arm = GNUNET_ARM_connect (cfg, NULL);
  311. #if START_ARM
  312. GNUNET_ARM_start_service (arm, "arm", GNUNET_TIME_UNIT_ZERO, &arm_notify,
  313. NULL);
  314. #else
  315. arm_do_nothing (NULL, GNUNET_YES);
  316. #endif
  317. }
  318. static int
  319. check ()
  320. {
  321. char *const argv[] = {
  322. "test-arm-api",
  323. "-c", "test_arm_api_data.conf",
  324. #if VERBOSE
  325. "-L", "DEBUG",
  326. #endif
  327. NULL
  328. };
  329. struct GNUNET_GETOPT_CommandLineOption options[] = {
  330. GNUNET_GETOPT_OPTION_END
  331. };
  332. /* Running ARM and running the do_nothing task */
  333. GNUNET_assert (GNUNET_OK ==
  334. GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1,
  335. argv, "test-exponential-backoff", "nohelp",
  336. options, &task, NULL));
  337. return ok;
  338. }
  339. static int
  340. init ()
  341. {
  342. #if LOG_BACKOFF
  343. killLogFileName = GNUNET_DISK_mktemp ("exponential-backoff-waiting.log");
  344. if (NULL == (killLogFilePtr = FOPEN (killLogFileName, "w")))
  345. {
  346. GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "fopen",
  347. killLogFileName);
  348. GNUNET_free (killLogFileName);
  349. return GNUNET_SYSERR;
  350. }
  351. #endif
  352. return GNUNET_OK;
  353. }
  354. static void
  355. houseKeep ()
  356. {
  357. #if LOG_BACKOFF
  358. GNUNET_assert (0 == fclose (killLogFilePtr));
  359. GNUNET_free (killLogFileName);
  360. #endif
  361. }
  362. int
  363. main (int argc, char *argv[])
  364. {
  365. int ret;
  366. GNUNET_log_setup ("test-exponential-backoff",
  367. #if VERBOSE
  368. "DEBUG",
  369. #else
  370. "WARNING",
  371. #endif
  372. NULL);
  373. init ();
  374. ret = check ();
  375. houseKeep ();
  376. return ret;
  377. }
  378. /* end of test_exponential_backoff.c */