10 #include <user_request.h>
14 static TReturn always_send_ok(TcoreHal *hal, unsigned int data_len, void *data)
16 return TCORE_RETURN_SUCCESS;
19 static struct tcore_hal_operations hops = {
20 .send = always_send_ok,
23 static void test_queue_hal(void)
26 TcorePending *pending1;
27 TcorePending *pending2;
28 TcorePending *pending3;
29 TcorePending *pending = NULL;
32 h = tcore_hal_new(NULL, "test_hal", &hops, TCORE_HAL_MODE_CUSTOM);
35 ret = tcore_hal_set_power_state(h, TRUE);
36 g_assert(ret == TCORE_RETURN_SUCCESS);
39 pending1 = tcore_pending_new(NULL, 1);
41 tcore_pending_set_priority(pending1, TCORE_PENDING_PRIORITY_IMMEDIATELY);
44 pending2 = tcore_pending_new(NULL, 1);
46 tcore_pending_set_auto_free_status_after_sent(pending2, TRUE);
49 pending3 = tcore_pending_new(NULL, 1);
51 tcore_pending_set_priority(pending3, TCORE_PENDING_PRIORITY_IMMEDIATELY);
54 ret = tcore_hal_send_request(h, pending1);
55 g_assert(ret == TCORE_RETURN_SUCCESS);
57 /* send pending2 -> queue */
58 ret = tcore_hal_send_request(h, pending2);
59 g_assert(ret == TCORE_RETURN_SUCCESS);
62 ret = tcore_hal_dispatch_response_data(h, 1, 0, NULL);
63 g_assert(ret == TCORE_RETURN_SUCCESS);
65 /* check next pending */
66 pending = tcore_queue_ref_pending_by_id(tcore_hal_ref_queue(h), 1);
67 g_assert(pending == pending2);
69 /* force send (because g_main_loop not work in unit test) */
70 tcore_hal_send_force(h);
72 /* check next pending (pending2 is auto free) */
73 pending = tcore_queue_ref_pending_by_id(tcore_hal_ref_queue(h), 1);
74 g_assert(pending == NULL);
77 ret = tcore_hal_send_request(h, pending3);
78 g_assert(ret == TCORE_RETURN_SUCCESS);
80 pending = tcore_queue_ref_pending_by_id(tcore_hal_ref_queue(h), 1);
81 g_assert(pending == pending3);
84 ret = tcore_hal_dispatch_response_data(h, 1, 0, NULL);
85 g_assert(ret == TCORE_RETURN_SUCCESS);
87 /* check next pending (no more pending) */
88 pending = tcore_queue_ref_pending_by_id(tcore_hal_ref_queue(h), 1);
89 g_assert(pending == NULL);
94 static void test_queue_push_priority(void)
97 TcorePending *pending1;
98 TcorePending *pending2;
99 TcorePending *pending3;
100 TcorePending *pending = NULL;
103 q = tcore_queue_new(NULL);
107 pending1 = tcore_pending_new(NULL, 1);
109 tcore_pending_set_priority(pending1, TCORE_PENDING_PRIORITY_IMMEDIATELY);
111 ret = tcore_queue_push(q, pending1);
112 g_assert(ret == TCORE_RETURN_SUCCESS);
115 pending2 = tcore_pending_new(NULL, 1);
118 ret = tcore_queue_push(q, pending2);
119 g_assert(ret == TCORE_RETURN_SUCCESS);
121 /* third item, but push to second item position */
122 pending3 = tcore_pending_new(NULL, 1);
124 tcore_pending_set_priority(pending3, TCORE_PENDING_PRIORITY_IMMEDIATELY);
126 ret = tcore_queue_push(q, pending3);
127 g_assert(ret == TCORE_RETURN_SUCCESS);
129 /* pop test (order by priority) */
130 pending = tcore_queue_pop_by_id(q, 1);
131 g_assert(pending == pending1);
133 pending = tcore_queue_pop_by_id(q, 1);
134 g_assert(pending == pending3);
136 pending = tcore_queue_pop_by_id(q, 1);
137 g_assert(pending == pending2);
142 static void test_queue_push(void)
145 TcorePending *pending1;
146 TcorePending *pending2;
147 TcorePending *pending3;
148 TcorePending *pending = NULL;
151 q = tcore_queue_new(NULL);
155 pending1 = tcore_pending_new(NULL, 1);
158 ret = tcore_queue_push(q, pending1);
159 g_assert(ret == TCORE_RETURN_SUCCESS);
162 pending2 = tcore_pending_new(NULL, 1);
165 ret = tcore_queue_push(q, pending2);
166 g_assert(ret == TCORE_RETURN_SUCCESS);
169 pending3 = tcore_pending_new(NULL, 1);
172 ret = tcore_queue_push(q, pending3);
173 g_assert(ret == TCORE_RETURN_SUCCESS);
175 /* pop test (order by push sequence (same priority)) */
176 pending = tcore_queue_pop_by_id(q, 1);
177 g_assert(pending == pending1);
179 pending = tcore_queue_pop_by_id(q, 1);
180 g_assert(pending == pending2);
182 pending = tcore_queue_pop_by_id(q, 1);
183 g_assert(pending == pending3);
188 static void on_resp(TcorePending *pending, int data_len, const void *data, void *user_data)
190 printf("on_resp !!\n");
194 static void test_queue_push_cancel(void)
197 TcorePending *pending1;
198 TcorePending *pending2;
199 TcorePending *pending3;
205 h = tcore_hal_new(NULL, "test_hal", &hops, TCORE_HAL_MODE_CUSTOM);
208 ret = tcore_hal_set_power_state(h, TRUE);
209 g_assert(ret == TCORE_RETURN_SUCCESS);
212 ur1 = tcore_user_request_new(NULL, NULL);
215 tcore_user_request_set_command(ur1, TREQ_NETWORK_SEARCH);
217 pending1 = tcore_pending_new(NULL, 1);
220 tcore_pending_set_response_callback(pending1, on_resp, NULL);
221 tcore_pending_link_user_request(pending1, ur1);
223 ret = tcore_hal_send_request(h, pending1);
224 g_assert(ret == TCORE_RETURN_SUCCESS);
226 /* force update send state */
227 tcore_pending_emit_send_callback(pending1, TRUE);
230 ur2 = tcore_user_request_new(NULL, NULL);
233 tcore_user_request_set_command(ur2, TREQ_NETWORK_SEARCH);
235 pending2 = tcore_pending_new(NULL, 1);
238 tcore_pending_set_response_callback(pending2, on_resp, NULL);
239 tcore_pending_link_user_request(pending2, ur2);
241 ret = tcore_hal_send_request(h, pending2);
242 g_assert(ret == TCORE_RETURN_SUCCESS);
245 pending3 = tcore_pending_new(NULL, 1);
248 ret = tcore_hal_send_request(h, pending3);
249 g_assert(ret == TCORE_RETURN_SUCCESS);
253 p = tcore_queue_search_by_command(tcore_hal_ref_queue(h), TREQ_NETWORK_SEARCH, TRUE);
254 g_assert(p == pending1);
256 p = tcore_queue_search_by_command(tcore_hal_ref_queue(h), TREQ_NETWORK_SEARCH, FALSE);
257 g_assert(p == pending2);
261 ret = tcore_queue_cancel_pending_by_command(tcore_hal_ref_queue(h), TREQ_NETWORK_SEARCH);
262 g_assert(ret == TCORE_RETURN_SUCCESS);
264 g_assert(g_flag == 2);
268 static void test_queue_new(void)
272 q = tcore_queue_new(NULL);
278 int main(int argc, char **argv)
280 g_test_init(&argc, &argv, NULL);
282 g_test_add_func("/queue/new", test_queue_new);
283 g_test_add_func("/queue/push", test_queue_push);
284 g_test_add_func("/queue/push_priority", test_queue_push_priority);
285 g_test_add_func("/queue/push_cancel", test_queue_push_cancel);
286 g_test_add_func("/queue/hal", test_queue_hal);