test_bulletin.c 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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/log.h>
  29. #include <kern/timer.h>
  30. #include <test/test.h>
  31. #define TEST_INTERVAL 1
  32. #define TEST_NR_LOOPS 4
  33. static struct bulletin test_bulletin;
  34. static struct bulletin_sub test_bulletin_sub;
  35. static struct timer test_timer;
  36. static uint32_t test_counter;
  37. static void
  38. test_notify (uintptr_t value, void *arg)
  39. {
  40. log_info ("test: notify: value:%lu arg:%p", value, arg);
  41. }
  42. static void
  43. test_tick (struct timer *timer)
  44. {
  45. bulletin_publish (&test_bulletin, ++test_counter);
  46. bulletin_unsubscribe (&test_bulletin, &test_bulletin_sub);
  47. if (test_counter == TEST_NR_LOOPS)
  48. {
  49. log_info ("test (bulletin): OK");
  50. return;
  51. }
  52. bulletin_subscribe (&test_bulletin, &test_bulletin_sub,
  53. test_notify, (void *) 0x123);
  54. timer_schedule (&test_timer, timer_get_time (timer) +
  55. clock_ticks_from_ms (TEST_INTERVAL * 1000));
  56. }
  57. TEST_INLINE (bulletin)
  58. {
  59. bulletin_init (&test_bulletin);
  60. bulletin_subscribe (&test_bulletin, &test_bulletin_sub,
  61. test_notify, (void *) 0x123);
  62. timer_init (&test_timer, test_tick, TIMER_DETACHED);
  63. timer_schedule (&test_timer, clock_get_time () +
  64. clock_ticks_from_ms (TEST_INTERVAL * 1000));
  65. return (TEST_OK);
  66. }