summaryrefslogtreecommitdiff
path: root/test/test_bulletin.c
blob: 4a2fdb53c9abbda513571a12e65af165652cf529 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
/*
 * Copyright (c) 2018 Richard Braun.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 *
 * This test makes a bulletin subscriber subscribe to a bulletin, and uses
 * a timer to periodically publish, unsubscribe, and resubscribe to the
 * bulletin. A counter is used to test passing values to the notification
 * callback function.
 */

#include <assert.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdio.h>

#include <kern/bulletin.h>
#include <kern/clock.h>
#include <kern/error.h>
#include <kern/log.h>
#include <kern/timer.h>
#include <test/test.h>

#define TEST_INTERVAL   1
#define TEST_NR_LOOPS   4

static struct bulletin test_bulletin;
static struct bulletin_sub test_bulletin_sub;
static struct timer test_timer;
static unsigned int test_counter;

static void
test_notify(uintptr_t value, void *arg)
{
    log_info("test: notify: value:%lu arg:%p", value, arg);
}

static void
test_tick(struct timer *timer)
{
    uint64_t ticks;

    test_counter++;
    bulletin_publish(&test_bulletin, test_counter);
    bulletin_unsubscribe(&test_bulletin, &test_bulletin_sub);

    if (test_counter == TEST_NR_LOOPS) {
        log_info("test: done");
        return;
    }

    bulletin_subscribe(&test_bulletin, &test_bulletin_sub,
                       test_notify, (void *)0x123);

    ticks = timer_get_time(timer) + clock_ticks_from_ms(TEST_INTERVAL * 1000);
    timer_schedule(&test_timer, ticks);
}

void __init
test_setup(void)
{
    uint64_t ticks;

    bulletin_init(&test_bulletin);
    bulletin_subscribe(&test_bulletin, &test_bulletin_sub,
                       test_notify, (void *)0x123);

    timer_init(&test_timer, test_tick, TIMER_DETACHED);
    ticks = clock_get_time() + clock_ticks_from_ms(TEST_INTERVAL * 1000);
    timer_schedule(&test_timer, ticks);
}