blob: 3823ef5bedabdfa3dac33146e5e29482103e0521 [file] [log] [blame]
Holger Hans Peter Freytherc7f52c42016-11-12 21:25:21 +01001#include <osmocom/core/logging.h>
2#include <osmocom/core/utils.h>
3#include <osmocom/core/write_queue.h>
4
5static const struct log_info_cat default_categories[] = {
6};
7
8static const struct log_info log_info = {
9 .cat = default_categories,
10 .num_cat = ARRAY_SIZE(default_categories),
11};
12
13static void test_wqueue_limit(void)
14{
15 struct msgb *msg;
16 struct osmo_wqueue wqueue;
17 int rc;
18
19 osmo_wqueue_init(&wqueue, 0);
20 OSMO_ASSERT(wqueue.max_length == 0);
21 OSMO_ASSERT(wqueue.current_length == 0);
22 OSMO_ASSERT(wqueue.read_cb == NULL);
23 OSMO_ASSERT(wqueue.write_cb == NULL);
24 OSMO_ASSERT(wqueue.except_cb == NULL);
25
26 /* try to add and fail */
27 msg = msgb_alloc(4096, "msg1");
28 rc = osmo_wqueue_enqueue(&wqueue, msg);
29 OSMO_ASSERT(rc < 0);
30
31 /* add one and fail on the second */
32 wqueue.max_length = 1;
33 rc = osmo_wqueue_enqueue(&wqueue, msg);
34 OSMO_ASSERT(rc == 0);
35 OSMO_ASSERT(wqueue.current_length == 1);
36 msg = msgb_alloc(4096, "msg2");
37 rc = osmo_wqueue_enqueue(&wqueue, msg);
38 OSMO_ASSERT(rc < 0);
39
40 /* add one more */
41 wqueue.max_length = 2;
42 rc = osmo_wqueue_enqueue(&wqueue, msg);
43 OSMO_ASSERT(rc == 0);
44 OSMO_ASSERT(wqueue.current_length == 2);
45
46 /* release everything */
47 osmo_wqueue_clear(&wqueue);
48 OSMO_ASSERT(wqueue.current_length == 0);
49 OSMO_ASSERT(wqueue.max_length == 2);
50
51 /* Add two, fail on the third, free it and the queue */
52 msg = msgb_alloc(4096, "msg3");
53 rc = osmo_wqueue_enqueue(&wqueue, msg);
54 OSMO_ASSERT(rc == 0);
55 OSMO_ASSERT(wqueue.current_length == 1);
56 msg = msgb_alloc(4096, "msg4");
57 rc = osmo_wqueue_enqueue(&wqueue, msg);
58 OSMO_ASSERT(rc == 0);
59 OSMO_ASSERT(wqueue.current_length == 2);
60 msg = msgb_alloc(4096, "msg5");
61 rc = osmo_wqueue_enqueue(&wqueue, msg);
62 OSMO_ASSERT(rc < 0);
63 OSMO_ASSERT(wqueue.current_length == 2);
64 msgb_free(msg);
65 osmo_wqueue_clear(&wqueue);
66}
67
68int main(int argc, char **argv)
69{
70 struct log_target *stderr_target;
71
72 log_init(&log_info, NULL);
73 stderr_target = log_target_create_stderr();
74 log_add_target(stderr_target);
Pau Espin Pedrol01e0d3e2021-02-18 19:25:44 +010075 log_set_print_filename2(stderr_target, LOG_FILENAME_NONE);
Pau Espin Pedrol690b6612021-02-18 19:10:28 +010076 log_set_print_category_hex(stderr_target, 0);
77 log_set_print_category(stderr_target, 0);
Holger Hans Peter Freytherc7f52c42016-11-12 21:25:21 +010078
79 test_wqueue_limit();
80
81 printf("Done\n");
82 return 0;
83}