blob: 9545183add84959c3435940edfa194bfb42132e7 [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
Vadim Yanitskiy09c8bfc2023-12-09 03:43:45 +070071static void tc_enqueue_dequeue(void)
72{
73 const unsigned int qlen = 12;
74 struct it_q_test1 *item;
75 struct osmo_it_q *q1;
76 int rc;
77
78 ENTER_TC;
79
80 printf("allocating q1\n");
81 q1 = osmo_it_q_alloc(OTC_GLOBAL, "q1", 12, NULL, NULL);
82 OSMO_ASSERT(q1);
83
84#if 0
85 printf("try dequeueing from an empty queue\n");
86 osmo_it_q_dequeue(q1, &item, list);
87 OSMO_ASSERT(item == NULL);
88#endif
89
90 printf("adding queue entries up to the limit\n");
91 for (unsigned int i = 0; i < qlen; i++) {
92 item = talloc_zero(OTC_GLOBAL, struct it_q_test1);
93 rc = osmo_it_q_enqueue(q1, item, list);
94 OSMO_ASSERT(rc == 0);
95 }
96
97 printf("removing queue entries up to the limit\n");
98 for (unsigned int i = 0; i < qlen; i++) {
99 osmo_it_q_dequeue(q1, &item, list);
100 OSMO_ASSERT(item != NULL);
101 talloc_free(item);
102 }
103
104#if 0
105 printf("try dequeueing from an empty queue\n");
106 osmo_it_q_dequeue(q1, &item, list);
107 OSMO_ASSERT(item == NULL);
108#endif
109
110 osmo_it_q_destroy(q1);
111}
112
Harald Weltee4cd2672019-08-06 19:56:16 +0200113static int g_read_cb_count;
114
115static void q_read_cb(struct osmo_it_q *q, struct llist_head *item)
116{
117 struct it_q_test1 *it = container_of(item, struct it_q_test1, list);
118 *it->foo += 1;
119 talloc_free(item);
120}
121
122static void tc_eventfd(void)
123{
124 struct osmo_it_q *q1;
125 unsigned int qlen = 30;
126 struct it_q_test1 *item;
127 int i, rc;
128
129 ENTER_TC;
130
131 printf("allocating q1\n");
132 q1 = osmo_it_q_alloc(OTC_GLOBAL, "q1", qlen, q_read_cb, NULL);
133 OSMO_ASSERT(q1);
134 osmo_fd_register(&q1->event_ofd);
135
136 /* ensure read-cb isn't called unless we enqueue something */
137 osmo_select_main(1);
138 OSMO_ASSERT(g_read_cb_count == 0);
139
140 /* ensure read-cb is called for each enqueued msg once */
141 printf("adding %u queue entries up to the limit\n", qlen);
142 for (i = 0; i < qlen; i++) {
143 item = talloc_zero(OTC_GLOBAL, struct it_q_test1);
144 item->foo = &g_read_cb_count;
145 rc = osmo_it_q_enqueue(q1, item, list);
146 OSMO_ASSERT(rc == 0);
147 }
148
149 osmo_select_main(1);
150 printf("%u entries were dequeued\n", qlen);
151 OSMO_ASSERT(g_read_cb_count == qlen);
152
153 osmo_it_q_destroy(q1);
154}
155
156int main(int argc, char **argv)
157{
158 tc_alloc();
159 tc_queue_length();
Vadim Yanitskiy09c8bfc2023-12-09 03:43:45 +0700160 tc_enqueue_dequeue();
Harald Weltee4cd2672019-08-06 19:56:16 +0200161 tc_eventfd();
Oliver Smith55934c42021-01-21 11:57:48 +0100162 return 0;
Harald Weltee4cd2672019-08-06 19:56:16 +0200163}