blob: 28e32d89ac7abc4a5d2dc29c52c39151a6e673f4 [file] [log] [blame]
Harald Weltee4cd2672019-08-06 19:56:16 +02001#include <stdio.h>
2#include <errno.h>
3
4#include <osmocom/core/talloc.h>
5#include <osmocom/core/utils.h>
6#include <osmocom/core/it_q.h>
7
8struct it_q_test1 {
9 struct llist_head list;
10 int *foo;
11};
12
13struct it_q_test2 {
14 int foo;
15 struct llist_head list;
16};
17
18#define ENTER_TC printf("\n== Entering test case %s\n", __func__)
19
20static void tc_alloc(void)
21{
22 struct osmo_it_q *q1, *q2;
23
24 ENTER_TC;
25
26 printf("allocating q1\n");
27 q1 = osmo_it_q_alloc(OTC_GLOBAL, "q1", 3, NULL, NULL);
28 OSMO_ASSERT(q1);
29
30 /* ensure that no duplicate allocation for the */
31 printf("attempting duplicate allocation of qa\n");
32 q2 = osmo_it_q_alloc(OTC_GLOBAL, "q1", 3, NULL, NULL);
33 OSMO_ASSERT(!q2);
34
35 /* ensure that same name can be re-created after destroying old one */
36 osmo_it_q_destroy(q1);
37 printf("re-allocating q1\n");
38 q1 = osmo_it_q_alloc(OTC_GLOBAL, "q1", 3, NULL, NULL);
39 OSMO_ASSERT(q1);
40
41 osmo_it_q_destroy(q1);
42}
43
44static void tc_queue_length(void)
45{
46 struct osmo_it_q *q1;
47 unsigned int qlen = 3;
48 struct it_q_test1 *item;
49 int i, rc;
50
51 ENTER_TC;
52
53 printf("allocating q1\n");
54 q1 = osmo_it_q_alloc(OTC_GLOBAL, "q1", qlen, NULL, NULL);
55 OSMO_ASSERT(q1);
56
57 printf("adding queue entries up to the limit\n");
58 for (i = 0; i < qlen; i++) {
59 item = talloc_zero(OTC_GLOBAL, struct it_q_test1);
60 rc = osmo_it_q_enqueue(q1, item, list);
61 OSMO_ASSERT(rc == 0);
62 }
63 printf("attempting to add more than the limit\n");
64 item = talloc_zero(OTC_GLOBAL, struct it_q_test1);
65 rc = osmo_it_q_enqueue(q1, item, list);
66 OSMO_ASSERT(rc == -ENOSPC);
67
68 osmo_it_q_destroy(q1);
69}
70
71static int g_read_cb_count;
72
73static void q_read_cb(struct osmo_it_q *q, struct llist_head *item)
74{
75 struct it_q_test1 *it = container_of(item, struct it_q_test1, list);
76 *it->foo += 1;
77 talloc_free(item);
78}
79
80static void tc_eventfd(void)
81{
82 struct osmo_it_q *q1;
83 unsigned int qlen = 30;
84 struct it_q_test1 *item;
85 int i, rc;
86
87 ENTER_TC;
88
89 printf("allocating q1\n");
90 q1 = osmo_it_q_alloc(OTC_GLOBAL, "q1", qlen, q_read_cb, NULL);
91 OSMO_ASSERT(q1);
92 osmo_fd_register(&q1->event_ofd);
93
94 /* ensure read-cb isn't called unless we enqueue something */
95 osmo_select_main(1);
96 OSMO_ASSERT(g_read_cb_count == 0);
97
98 /* ensure read-cb is called for each enqueued msg once */
99 printf("adding %u queue entries up to the limit\n", qlen);
100 for (i = 0; i < qlen; i++) {
101 item = talloc_zero(OTC_GLOBAL, struct it_q_test1);
102 item->foo = &g_read_cb_count;
103 rc = osmo_it_q_enqueue(q1, item, list);
104 OSMO_ASSERT(rc == 0);
105 }
106
107 osmo_select_main(1);
108 printf("%u entries were dequeued\n", qlen);
109 OSMO_ASSERT(g_read_cb_count == qlen);
110
111 osmo_it_q_destroy(q1);
112}
113
114int main(int argc, char **argv)
115{
116 tc_alloc();
117 tc_queue_length();
118 tc_eventfd();
Oliver Smith55934c42021-01-21 11:57:48 +0100119 return 0;
Harald Weltee4cd2672019-08-06 19:56:16 +0200120}