1 /*
2 * Copyright (c) 2020-2021 Huawei Device Co., Ltd.
3 *
4 * HDF is dual licensed: you can use it either under the terms of
5 * the GPL, or the BSD license, at your option.
6 * See the LICENSE file in the root of this repository for complete details.
7 */
8
9 #include <securec.h>
10 #include "hdf_log.h"
11 #include "hdf_message_test.h"
12 #include "hdf_wlan_priority_queue.h"
13 #include "osal_thread.h"
14 #include "osal_time.h"
15
16
17 #define TEST_QUEUE_SIZE 30
18
19 #define NO_PRIORITY 1
20 #define MUTI_PRIORITY 2
21
22 #define POP_TIMEOUT 3000
MessageQueueTest001(void)23 int32_t MessageQueueTest001(void)
24 {
25 int a = 1;
26 int b = 2;
27 int c = 3;
28 int d = 4;
29 int32_t errCode;
30 void *p = NULL;
31 PriorityQueue *queue = CreatePriorityQueue(TEST_QUEUE_SIZE, NO_PRIORITY);
32 if (queue == NULL) {
33 HDF_LOGE("%s:Create queue failed!", __func__);
34 return -1;
35 }
36 do {
37 errCode = PushPriorityQueue(queue, 0, &a);
38 MSG_BREAK_IF_NOT_SUCCESS(errCode);
39
40 errCode = PushPriorityQueue(queue, 0, &b);
41 MSG_BREAK_IF_NOT_SUCCESS(errCode);
42
43 errCode = PushPriorityQueue(queue, 0, &c);
44 MSG_BREAK_IF_NOT_SUCCESS(errCode);
45
46 errCode = PushPriorityQueue(queue, 0, &d);
47 MSG_BREAK_IF_NOT_SUCCESS(errCode);
48
49 p = PopPriorityQueue(queue, 0);
50 MSG_BREAK_IF(errCode, p == NULL);
51 MSG_BREAK_IF(errCode, p != &a);
52
53 p = PopPriorityQueue(queue, 0);
54 MSG_BREAK_IF(errCode, p == NULL);
55 MSG_BREAK_IF(errCode, p != &b);
56
57 p = PopPriorityQueue(queue, 0);
58 MSG_BREAK_IF(errCode, p == NULL);
59 MSG_BREAK_IF(errCode, p != &c);
60
61 p = PopPriorityQueue(queue, 0);
62 MSG_BREAK_IF(errCode, p == NULL);
63 MSG_BREAK_IF(errCode, p != &d);
64 } while (0);
65
66 DestroyPriorityQueue(queue);
67
68 return errCode;
69 }
70
71 // Proprity test
MessageQueueTest002(void)72 int32_t MessageQueueTest002(void)
73 {
74 int a = 1;
75 int b = 2;
76 int c = 3;
77 int d = 4;
78 int32_t errCode;
79 void *p = NULL;
80 PriorityQueue *queue = NULL;
81 queue = CreatePriorityQueue(TEST_QUEUE_SIZE, MUTI_PRIORITY);
82 if (queue == NULL) {
83 HDF_LOGE("%s:Create queue failed!", __func__);
84 return -1;
85 }
86 do {
87 errCode = PushPriorityQueue(queue, 0, &a);
88 MSG_BREAK_IF_NOT_SUCCESS(errCode);
89
90 errCode = PushPriorityQueue(queue, 1, &b);
91 MSG_BREAK_IF_NOT_SUCCESS(errCode);
92
93 errCode = PushPriorityQueue(queue, 1, &c);
94 MSG_BREAK_IF_NOT_SUCCESS(errCode);
95
96 errCode = PushPriorityQueue(queue, 0, &d);
97 MSG_BREAK_IF_NOT_SUCCESS(errCode);
98
99 p = PopPriorityQueue(queue, 0);
100 MSG_BREAK_IF(errCode, p == NULL);
101 MSG_BREAK_IF(errCode, p != &a);
102
103 p = PopPriorityQueue(queue, 0);
104 MSG_BREAK_IF(errCode, p == NULL);
105 MSG_BREAK_IF(errCode, p != &d);
106
107 p = PopPriorityQueue(queue, 0);
108 MSG_BREAK_IF(errCode, p == NULL);
109 MSG_BREAK_IF(errCode, p != &b);
110
111 p = PopPriorityQueue(queue, 0);
112 MSG_BREAK_IF(errCode, p == NULL);
113 MSG_BREAK_IF(errCode, p != &c);
114 } while (0);
115
116 DestroyPriorityQueue(queue);
117 return errCode;
118 }
119
120 int g_testValue = 0;
121 #define PUSH_DELAY 2000
RunPushQueue(void * para)122 static int RunPushQueue(void *para)
123 {
124 if (para == NULL) {
125 return -1;
126 }
127 OsalMSleep(PUSH_DELAY);
128 PushPriorityQueue((PriorityQueue *)para, 0, &g_testValue);
129 return 0;
130 }
131
132 // wait and awake test
MessageQueueTest003(void)133 int32_t MessageQueueTest003(void)
134 {
135 int32_t errCode = HDF_SUCCESS;
136 void *p = NULL;
137 PriorityQueue *queue = NULL;
138 int32_t status;
139 struct OsalThreadParam config;
140 OSAL_DECLARE_THREAD(pushThread);
141 queue = CreatePriorityQueue(TEST_QUEUE_SIZE, NO_PRIORITY);
142 if (queue == NULL) {
143 HDF_LOGE("%s:Create queue failed!", __func__);
144 return -1;
145 }
146
147 do {
148 config.name = "PushQueueWithDelay";
149 config.priority = OSAL_THREAD_PRI_DEFAULT;
150 config.stackSize = 0x1000;
151 status = OsalThreadCreate(&pushThread, RunPushQueue, queue);
152 if (status != HDF_SUCCESS) {
153 HDF_LOGE("%s:OsalThreadCreate failed!status=%d", __func__, status);
154 errCode = HDF_FAILURE;
155 break;
156 }
157 status = OsalThreadStart(&pushThread, &config);
158 if (status != HDF_SUCCESS) {
159 HDF_LOGE("%s:OsalThreadStart failed!status=%d", __func__, status);
160 OsalThreadDestroy(&pushThread);
161 errCode = HDF_FAILURE;
162 break;
163 }
164
165 p = PopPriorityQueue(queue, POP_TIMEOUT);
166 MSG_BREAK_IF(errCode, p == NULL);
167 MSG_BREAK_IF(errCode, p != &g_testValue);
168 } while (false);
169
170 OsalThreadDestroy(&pushThread);
171 DestroyPriorityQueue(queue);
172 return errCode;
173 }
174