test_statistics_api_watch.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. /*
  2. This file is part of GNUnet.
  3. (C) 2009, 2011, 2012 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 statistics/test_statistics_api_watch.c
  19. * @brief testcase for statistics_api.c watch functions
  20. * @author Christian Grothoff
  21. */
  22. #include "platform.h"
  23. #include "gnunet_util_lib.h"
  24. #include "gnunet_statistics_service.h"
  25. static int ok;
  26. static struct GNUNET_STATISTICS_Handle *h;
  27. static struct GNUNET_STATISTICS_Handle *h2;
  28. static GNUNET_SCHEDULER_TaskIdentifier shutdown_task;
  29. static void
  30. force_shutdown (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
  31. {
  32. fprintf (stderr, "Timeout, failed to receive notifications: %d\n", ok);
  33. GNUNET_STATISTICS_destroy (h, GNUNET_NO);
  34. GNUNET_STATISTICS_destroy (h2, GNUNET_NO);
  35. ok = 7;
  36. }
  37. static void
  38. normal_shutdown (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
  39. {
  40. GNUNET_STATISTICS_destroy (h, GNUNET_NO);
  41. GNUNET_STATISTICS_destroy (h2, GNUNET_NO);
  42. }
  43. static int
  44. watch_1 (void *cls, const char *subsystem, const char *name, uint64_t value,
  45. int is_persistent)
  46. {
  47. GNUNET_assert (value == 42);
  48. GNUNET_assert (0 == strcmp (name, "test-1"));
  49. ok &= ~1;
  50. if (0 == ok)
  51. {
  52. GNUNET_SCHEDULER_cancel (shutdown_task);
  53. GNUNET_SCHEDULER_add_now (&normal_shutdown, NULL);
  54. }
  55. return GNUNET_OK;
  56. }
  57. static int
  58. watch_2 (void *cls, const char *subsystem, const char *name, uint64_t value,
  59. int is_persistent)
  60. {
  61. GNUNET_assert (value == 43);
  62. GNUNET_assert (0 == strcmp (name, "test-2"));
  63. ok &= ~2;
  64. if (0 == ok)
  65. {
  66. GNUNET_SCHEDULER_cancel (shutdown_task);
  67. GNUNET_SCHEDULER_add_now (&normal_shutdown, NULL);
  68. }
  69. return GNUNET_OK;
  70. }
  71. static void
  72. run (void *cls, char *const *args, const char *cfgfile,
  73. const struct GNUNET_CONFIGURATION_Handle *cfg)
  74. {
  75. h = GNUNET_STATISTICS_create ("dummy", cfg);
  76. GNUNET_assert (GNUNET_OK ==
  77. GNUNET_STATISTICS_watch (h, "test-statistics-api-watch",
  78. "test-1", &watch_1, NULL));
  79. GNUNET_assert (GNUNET_OK ==
  80. GNUNET_STATISTICS_watch (h, "test-statistics-api-watch",
  81. "test-2", &watch_2, NULL));
  82. h2 = GNUNET_STATISTICS_create ("test-statistics-api-watch", cfg);
  83. GNUNET_STATISTICS_set (h2, "test-1", 42, GNUNET_NO);
  84. GNUNET_STATISTICS_set (h2, "test-2", 43, GNUNET_NO);
  85. shutdown_task =
  86. GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_MINUTES, &force_shutdown,
  87. NULL);
  88. }
  89. int
  90. main (int argc, char *argv_ign[])
  91. {
  92. char *const argv[] = { "test-statistics-api",
  93. "-c",
  94. "test_statistics_api_data.conf",
  95. NULL
  96. };
  97. struct GNUNET_GETOPT_CommandLineOption options[] = {
  98. GNUNET_GETOPT_OPTION_END
  99. };
  100. struct GNUNET_OS_Process *proc;
  101. char *binary;
  102. binary = GNUNET_OS_get_libexec_binary_path ("gnunet-service-statistics");
  103. proc =
  104. GNUNET_OS_start_process (GNUNET_YES, GNUNET_OS_INHERIT_STD_OUT_AND_ERR,
  105. NULL, NULL, NULL,
  106. binary,
  107. "gnunet-service-statistics",
  108. "-c", "test_statistics_api_data.conf", NULL);
  109. GNUNET_assert (NULL != proc);
  110. ok = 3;
  111. GNUNET_PROGRAM_run (3, argv, "test-statistics-api", "nohelp", options, &run,
  112. NULL);
  113. if (0 != GNUNET_OS_process_kill (proc, GNUNET_TERM_SIG))
  114. {
  115. GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
  116. ok = 1;
  117. }
  118. GNUNET_OS_process_wait (proc);
  119. GNUNET_OS_process_destroy (proc);
  120. proc = NULL;
  121. GNUNET_free (binary);
  122. return ok;
  123. }
  124. /* end of test_statistics_api_watch.c */