test_exponential_backoff.c 11 KB

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