test_bulletin.c 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. /*
  2. * Copyright (c) 2018 Richard Braun.
  3. *
  4. * This program is free software: you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation, either version 3 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. *
  17. *
  18. * This test makes a bulletin subscriber subscribe to a bulletin, and uses
  19. * a timer to periodically publish, unsubscribe, and resubscribe to the
  20. * bulletin. A counter is used to test passing values to the notification
  21. * callback function.
  22. */
  23. #include <stdbool.h>
  24. #include <stddef.h>
  25. #include <stdio.h>
  26. #include <kern/bulletin.h>
  27. #include <kern/clock.h>
  28. #include <kern/error.h>
  29. #include <kern/log.h>
  30. #include <kern/timer.h>
  31. #include <test/test.h>
  32. #define TEST_INTERVAL 1
  33. #define TEST_NR_LOOPS 4
  34. static struct bulletin test_bulletin;
  35. static struct bulletin_sub test_bulletin_sub;
  36. static struct timer test_timer;
  37. static uint32_t test_counter;
  38. static void
  39. test_notify (uintptr_t value, void *arg)
  40. {
  41. log_info ("test: notify: value:%lu arg:%p", value, arg);
  42. }
  43. static void
  44. test_tick (struct timer *timer)
  45. {
  46. bulletin_publish (&test_bulletin, ++test_counter);
  47. bulletin_unsubscribe (&test_bulletin, &test_bulletin_sub);
  48. if (test_counter == TEST_NR_LOOPS)
  49. {
  50. log_info ("test (bulletin): OK");
  51. return;
  52. }
  53. bulletin_subscribe (&test_bulletin, &test_bulletin_sub,
  54. test_notify, (void *) 0x123);
  55. timer_schedule (&test_timer, timer_get_time (timer) +
  56. clock_ticks_from_ms (TEST_INTERVAL * 1000));
  57. }
  58. TEST_INLINE (bulletin)
  59. {
  60. bulletin_init (&test_bulletin);
  61. bulletin_subscribe (&test_bulletin, &test_bulletin_sub,
  62. test_notify, (void *) 0x123);
  63. timer_init (&test_timer, test_tick, TIMER_DETACHED);
  64. timer_schedule (&test_timer, clock_get_time () +
  65. clock_ticks_from_ms (TEST_INTERVAL * 1000));
  66. return (TEST_OK);
  67. }