test_exponential_backoff.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  1. /*
  2. This file is part of GNUnet.
  3. Copyright (C) 2009, 2016 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 arm/test_exponential_backoff.c
  18. * @brief testcase for gnunet-service-arm.c
  19. * @author Christian Grothoff
  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 LOG_BACKOFF GNUNET_NO
  27. #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 10)
  28. #define SERVICE_TEST_TIMEOUT GNUNET_TIME_UNIT_FOREVER_REL
  29. #define FIVE_MILLISECONDS GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, 5)
  30. #define SERVICE "do-nothing"
  31. #define BINARY "mockup-service"
  32. #define CFGFILENAME "test_arm_api_data2.conf"
  33. static const struct GNUNET_CONFIGURATION_Handle *cfg;
  34. static struct GNUNET_ARM_Handle *arm;
  35. static struct GNUNET_ARM_MonitorHandle *mon;
  36. static struct GNUNET_SCHEDULER_Task *kt;
  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. /**
  48. * Context for handling the shutdown of a service.
  49. */
  50. struct ShutdownContext
  51. {
  52. /**
  53. * Connection to the service that is being shutdown.
  54. */
  55. struct GNUNET_MQ_Handle *mq;
  56. /**
  57. * Task set up to cancel the shutdown request on timeout.
  58. */
  59. struct GNUNET_SCHEDULER_Task *cancel_task;
  60. };
  61. static void
  62. kill_task (void *cbData);
  63. /**
  64. * Shutting down took too long, cancel receive and return error.
  65. *
  66. * @param cls closure
  67. */
  68. static void
  69. service_shutdown_timeout (void *cls)
  70. {
  71. GNUNET_assert (0);
  72. }
  73. /**
  74. * Generic error handler, called with the appropriate error code and
  75. * the same closure specified at the creation of the message queue.
  76. * Not every message queue implementation supports an error handler.
  77. *
  78. * @param cls closure with the `struct ShutdownContext *`
  79. * @param error error code
  80. */
  81. static void
  82. mq_error_handler (void *cls,
  83. enum GNUNET_MQ_Error error)
  84. {
  85. struct ShutdownContext *shutdown_ctx = cls;
  86. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  87. "Service shutdown complete (MQ error).\n");
  88. GNUNET_SCHEDULER_cancel (shutdown_ctx->cancel_task);
  89. GNUNET_MQ_destroy (shutdown_ctx->mq);
  90. GNUNET_free (shutdown_ctx);
  91. }
  92. static void
  93. kill_task (void *cbData)
  94. {
  95. struct ShutdownContext *shutdown_ctx
  96. = GNUNET_new (struct ShutdownContext);
  97. struct GNUNET_MQ_Envelope *env;
  98. struct GNUNET_MessageHeader *msg;
  99. struct GNUNET_MQ_MessageHandler handlers[] = {
  100. GNUNET_MQ_handler_end ()
  101. };
  102. kt = NULL;
  103. if (trialCount == 13)
  104. {
  105. LOG ("Saw enough kills, asking ARM to stop mock service for good\n");
  106. GNUNET_ARM_request_service_stop (arm,
  107. SERVICE,
  108. NULL,
  109. NULL);
  110. ok = 0;
  111. trialCount++;
  112. GNUNET_free (shutdown_ctx);
  113. return;
  114. }
  115. shutdown_ctx->mq = GNUNET_CLIENT_connect (cfg,
  116. SERVICE,
  117. handlers,
  118. &mq_error_handler,
  119. shutdown_ctx);
  120. GNUNET_assert (NULL != shutdown_ctx->mq);
  121. trialCount++;
  122. LOG ("Sending a shutdown request to the mock service\n");
  123. env = GNUNET_MQ_msg (msg,
  124. GNUNET_MESSAGE_TYPE_ARM_STOP); /* FIXME: abuse of message type */
  125. GNUNET_MQ_send (shutdown_ctx->mq,
  126. env);
  127. shutdown_ctx->cancel_task
  128. = GNUNET_SCHEDULER_add_delayed (TIMEOUT,
  129. &service_shutdown_timeout,
  130. shutdown_ctx);
  131. }
  132. static void
  133. trigger_disconnect (void *cls)
  134. {
  135. GNUNET_ARM_disconnect (arm);
  136. GNUNET_ARM_monitor_stop (mon);
  137. if (NULL != kt)
  138. {
  139. GNUNET_SCHEDULER_cancel (kt);
  140. kt = NULL;
  141. }
  142. }
  143. static void
  144. arm_stop_cb (void *cls,
  145. enum GNUNET_ARM_RequestStatus status,
  146. enum GNUNET_ARM_Result result)
  147. {
  148. GNUNET_break (status == GNUNET_ARM_REQUEST_SENT_OK);
  149. GNUNET_break (result == GNUNET_ARM_RESULT_STOPPED);
  150. LOG ("ARM service stopped\n");
  151. GNUNET_SCHEDULER_shutdown ();
  152. }
  153. static void
  154. srv_status (void *cls,
  155. const char *service,
  156. enum GNUNET_ARM_ServiceStatus status)
  157. {
  158. if (status == GNUNET_ARM_SERVICE_MONITORING_STARTED)
  159. {
  160. LOG ("ARM monitor started, starting mock service\n");
  161. phase++;
  162. GNUNET_ARM_request_service_start (arm,
  163. SERVICE,
  164. GNUNET_OS_INHERIT_STD_OUT_AND_ERR,
  165. NULL,
  166. NULL);
  167. return;
  168. }
  169. if (0 != strcasecmp (service, SERVICE))
  170. return; /* not what we care about */
  171. if (phase == 1)
  172. {
  173. GNUNET_break (status == GNUNET_ARM_SERVICE_STARTING);
  174. GNUNET_break (phase == 1);
  175. LOG ("do-nothing is starting\n");
  176. phase++;
  177. ok = 1;
  178. GNUNET_assert (NULL == kt);
  179. startedWaitingAt = GNUNET_TIME_absolute_get ();
  180. kt = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS,
  181. &kill_task,
  182. NULL);
  183. }
  184. else if (phase == 2)
  185. {
  186. /* We passively monitor ARM for status updates. ARM should tell us
  187. * when do-nothing dies (no need to run a service upness test ourselves).
  188. */
  189. if (status == GNUNET_ARM_SERVICE_STARTING)
  190. {
  191. waitedFor = GNUNET_TIME_absolute_get_duration (startedWaitingAt);
  192. LOG ("Waited for: %s\n",
  193. GNUNET_STRINGS_relative_time_to_string (waitedFor,
  194. GNUNET_YES));
  195. LOG ("do-nothing is starting, killing it...\n");
  196. GNUNET_assert (NULL == kt);
  197. kt = GNUNET_SCHEDULER_add_now (&kill_task, &ok);
  198. }
  199. else if ((status == GNUNET_ARM_SERVICE_STOPPED) && (trialCount == 14))
  200. {
  201. phase++;
  202. LOG ("do-nothing stopped working %u times, we are done here\n",
  203. (unsigned int) trialCount);
  204. GNUNET_ARM_request_service_stop (arm,
  205. "arm",
  206. &arm_stop_cb,
  207. NULL);
  208. }
  209. }
  210. }
  211. static void
  212. arm_start_cb (void *cls,
  213. enum GNUNET_ARM_RequestStatus status,
  214. enum GNUNET_ARM_Result result)
  215. {
  216. GNUNET_break (status == GNUNET_ARM_REQUEST_SENT_OK);
  217. GNUNET_break (result == GNUNET_ARM_RESULT_STARTING);
  218. GNUNET_break (phase == 0);
  219. LOG ("Sent 'START' request for arm to ARM %s\n",
  220. (status == GNUNET_ARM_REQUEST_SENT_OK) ? "successfully" : "unsuccessfully");
  221. }
  222. static void
  223. task (void *cls,
  224. char *const *args,
  225. const char *cfgfile,
  226. const struct GNUNET_CONFIGURATION_Handle *c)
  227. {
  228. cfg = c;
  229. arm = GNUNET_ARM_connect (cfg, NULL, NULL);
  230. if (NULL == arm)
  231. {
  232. GNUNET_break (0);
  233. return;
  234. }
  235. mon = GNUNET_ARM_monitor_start (cfg,
  236. &srv_status,
  237. NULL);
  238. if (NULL == mon)
  239. {
  240. GNUNET_break (0);
  241. GNUNET_ARM_disconnect (arm);
  242. arm = NULL;
  243. return;
  244. }
  245. GNUNET_ARM_request_service_start (arm,
  246. "arm",
  247. GNUNET_OS_INHERIT_STD_OUT_AND_ERR,
  248. &arm_start_cb,
  249. NULL);
  250. GNUNET_SCHEDULER_add_shutdown (&trigger_disconnect,
  251. NULL);
  252. }
  253. static int
  254. check ()
  255. {
  256. char *const argv[] = {
  257. "test-exponential-backoff",
  258. "-c", CFGFILENAME,
  259. NULL
  260. };
  261. struct GNUNET_GETOPT_CommandLineOption options[] = {
  262. GNUNET_GETOPT_OPTION_END
  263. };
  264. /* Running ARM and running the do_nothing task */
  265. GNUNET_assert (GNUNET_OK ==
  266. GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1,
  267. argv,
  268. "test-exponential-backoff",
  269. "nohelp",
  270. options,
  271. &task,
  272. NULL));
  273. return ok;
  274. }
  275. #ifndef PATH_MAX
  276. /**
  277. * Assumed maximum path length (for the log file name).
  278. */
  279. #define PATH_MAX 4096
  280. #endif
  281. static int
  282. init ()
  283. {
  284. struct GNUNET_CONFIGURATION_Handle *cfg;
  285. char pwd[PATH_MAX];
  286. char *binary;
  287. cfg = GNUNET_CONFIGURATION_create ();
  288. if (GNUNET_OK != GNUNET_CONFIGURATION_parse (cfg,
  289. "test_arm_api_data.conf"))
  290. {
  291. GNUNET_CONFIGURATION_destroy (cfg);
  292. return GNUNET_SYSERR;
  293. }
  294. if (NULL == getcwd (pwd, PATH_MAX))
  295. return GNUNET_SYSERR;
  296. GNUNET_assert (0 < GNUNET_asprintf (&binary,
  297. "%s/%s",
  298. pwd,
  299. BINARY));
  300. GNUNET_CONFIGURATION_set_value_string (cfg,
  301. SERVICE,
  302. "BINARY",
  303. binary);
  304. GNUNET_free (binary);
  305. if (GNUNET_OK != GNUNET_CONFIGURATION_write (cfg,
  306. CFGFILENAME))
  307. {
  308. GNUNET_CONFIGURATION_destroy (cfg);
  309. return GNUNET_SYSERR;
  310. }
  311. GNUNET_CONFIGURATION_destroy (cfg);
  312. #if LOG_BACKOFF
  313. killLogFileName = GNUNET_DISK_mktemp ("exponential-backoff-waiting.log");
  314. if (NULL == (killLogFilePtr = FOPEN (killLogFileName,
  315. "w")))
  316. {
  317. GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING,
  318. "fopen",
  319. killLogFileName);
  320. GNUNET_free (killLogFileName);
  321. return GNUNET_SYSERR;
  322. }
  323. #endif
  324. return GNUNET_OK;
  325. }
  326. static void
  327. houseKeep ()
  328. {
  329. #if LOG_BACKOFF
  330. GNUNET_assert (0 == fclose (killLogFilePtr));
  331. GNUNET_free (killLogFileName);
  332. #endif
  333. (void) unlink (CFGFILENAME);
  334. }
  335. int
  336. main (int argc, char *argv[])
  337. {
  338. int ret;
  339. GNUNET_log_setup ("test-exponential-backoff",
  340. "WARNING",
  341. NULL);
  342. if (GNUNET_OK != init ())
  343. return 1;
  344. ret = check ();
  345. houseKeep ();
  346. return ret;
  347. }
  348. /* end of test_exponential_backoff.c */