1 /*
2 * Copyright (c) 2013-2019 Huawei Technologies Co., Ltd. All rights reserved.
3 * Copyright (c) 2020-2021 Huawei Device Co., Ltd. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without modification,
6 * are permitted provided that the following conditions are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright notice, this list of
9 * conditions and the following disclaimer.
10 *
11 * 2. Redistributions in binary form must reproduce the above copyright notice, this list
12 * of conditions and the following disclaimer in the documentation and/or other materials
13 * provided with the distribution.
14 *
15 * 3. Neither the name of the copyright holder nor the names of its contributors may be used
16 * to endorse or promote products derived from this software without specific prior written
17 * permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
21 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
23 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
24 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
26 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
27 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
28 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
29 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 */
31 #include "It_posix_queue.h"
32
33 static UINT32 g_index = 0;
34
PthreadF01(VOID * arg)35 static VOID *PthreadF01(VOID *arg)
36 {
37 INT32 i, ret;
38 UINT32 uret = 0;
39 INT32 count = (INT32)arg;
40 struct mq_attr attr = { 0 };
41
42 attr.mq_msgsize = MQUEUE_STANDARD_NAME_LENGTH;
43 attr.mq_maxmsg = 5; // 5, queue max message size.
44
45 uret = g_index % count;
46
47 (void)snprintf_s(g_mqueueName[uret], MQUEUE_STANDARD_NAME_LENGTH, MQUEUE_STANDARD_NAME_LENGTH - 1, \
48 "/mqueue_161_%d_%d", LosCurTaskIDGet(), (uret));
49 g_mqueueId[uret] = mq_open(g_mqueueName[uret], O_CREAT | O_RDWR, S_IRUSR | S_IWUSR, &attr);
50 ICUNIT_GOTO_NOT_EQUAL(g_mqueueId[uret], (mqd_t)-1, g_mqueueId[uret], EXIT);
51
52 return NULL;
53
54 EXIT:
55 for (i = 0; i < count; i++) {
56 mq_close(g_mqueueId[i]);
57 mq_unlink(g_mqueueName[i]);
58 }
59 return NULL;
60 }
61
PthreadF02(VOID * arg)62 static VOID *PthreadF02(VOID *arg)
63 {
64 INT32 ret;
65 INT32 queueCount = MQUEUE_MAX_NUM_TEST;
66 UINT32 i, j, count;
67 const CHAR *msgptr = MQUEUE_SEND_STRING_TEST;
68 CHAR msgrcd[MQUEUE_STANDARD_NAME_LENGTH] = {0};
69 struct timespec ts = { 0 };
70
71 ts.tv_sec = 0xffff;
72
73 for (i = 0; i < 10; i++) { // 10, The loop frequency.
74 count = g_index % queueCount;
75
76 for (j = 0; j <= count; j++) {
77 ret = mq_timedsend(g_mqueueId[j], msgptr, strlen(msgptr), 0, &ts);
78 ICUNIT_GOTO_EQUAL(ret, MQUEUE_NO_ERROR, ret, EXIT);
79
80 ret = mq_timedreceive(g_mqueueId[j], msgrcd, MQUEUE_STANDARD_NAME_LENGTH, 0, &ts);
81 ICUNIT_GOTO_EQUAL(ret, MQUEUE_SHORT_ARRAY_LENGTH, ret, EXIT);
82 ICUNIT_GOTO_STRING_EQUAL(msgrcd, MQUEUE_SEND_STRING_TEST, msgrcd, EXIT);
83 }
84 }
85 return NULL;
86 EXIT:
87 for (i = 0; i < queueCount; i++) {
88 mq_close(g_mqueueId[i]);
89 mq_unlink(g_mqueueName[i]);
90 }
91 return NULL;
92 }
93
PthreadF03(VOID * arg)94 static VOID *PthreadF03(VOID *arg)
95 {
96 INT32 ret, i;
97 INT32 count = (INT32)arg;
98
99 for (i = 0; i < count; i++) {
100 ret = mq_close(g_mqueueId[i]);
101 ICUNIT_GOTO_EQUAL(ret, MQUEUE_NO_ERROR, ret, EXIT);
102
103 ret = mq_unlink(g_mqueueName[i]);
104 ICUNIT_GOTO_EQUAL(ret, MQUEUE_NO_ERROR, ret, EXIT);
105 }
106
107 return NULL;
108 EXIT:
109 for (i = 0; i < count; i++) {
110 mq_close(g_mqueueId[i]);
111 mq_unlink(g_mqueueName[i]);
112 }
113 return NULL;
114 }
115
Testcase(VOID)116 static UINT32 Testcase(VOID)
117 {
118 UINT32 ret;
119 INT32 count = MQUEUE_MAX_NUM_TEST;
120 pthread_t threadA;
121 pthread_attr_t attrA;
122 pthread_t threadB;
123 pthread_attr_t attrB;
124 pthread_t threadC;
125 pthread_attr_t attrC;
126 struct sched_param sp;
127
128 ret = pthread_attr_init(&attrA);
129 ICUNIT_GOTO_EQUAL(ret, MQUEUE_NO_ERROR, ret, EXIT);
130
131 sp.sched_priority = MQUEUE_PTHREAD_PRIORITY_TEST2;
132 ret = pthread_attr_setschedparam(&attrA, &sp);
133 ICUNIT_GOTO_EQUAL(ret, MQUEUE_NO_ERROR, ret, EXIT1);
134
135 ret = pthread_attr_init(&attrB);
136 ICUNIT_GOTO_EQUAL(ret, MQUEUE_NO_ERROR, ret, EXIT1);
137
138 sp.sched_priority = MQUEUE_PTHREAD_PRIORITY_TEST2;
139 ret = pthread_attr_setschedparam(&attrB, &sp);
140 ICUNIT_GOTO_EQUAL(ret, MQUEUE_NO_ERROR, ret, EXIT2);
141
142 ret = pthread_attr_init(&attrC);
143 ICUNIT_GOTO_EQUAL(ret, MQUEUE_NO_ERROR, ret, EXIT2);
144
145 sp.sched_priority = MQUEUE_PTHREAD_PRIORITY_TEST2;
146 ret = pthread_attr_setschedparam(&attrC, &sp);
147 ICUNIT_GOTO_EQUAL(ret, MQUEUE_NO_ERROR, ret, EXIT2);
148
149 for (g_index = 0; g_index < count; g_index++) {
150 ret = pthread_create(&threadA, &attrA, PthreadF01, (void *)count);
151 ICUNIT_GOTO_EQUAL(ret, MQUEUE_NO_ERROR, ret, EXIT2);
152
153 ret = pthread_join(threadA, NULL);
154 ICUNIT_GOTO_EQUAL(ret, MQUEUE_NO_ERROR, ret, EXIT2);
155
156 ret = pthread_create(&threadB, &attrB, PthreadF02, (void *)count);
157 ICUNIT_GOTO_EQUAL(ret, MQUEUE_NO_ERROR, ret, EXIT2);
158
159 ret = pthread_join(threadB, NULL);
160 ICUNIT_GOTO_EQUAL(ret, MQUEUE_NO_ERROR, ret, EXIT2);
161
162 if (g_index % count == (count - 1)) {
163 ret = pthread_create(&threadC, &attrC, PthreadF03, (void *)count);
164 ICUNIT_GOTO_EQUAL(ret, MQUEUE_NO_ERROR, ret, EXIT2);
165
166 ret = pthread_join(threadC, NULL);
167 ICUNIT_GOTO_EQUAL(ret, MQUEUE_NO_ERROR, ret, EXIT2);
168 }
169 }
170
171 ret = pthread_attr_destroy(&attrC);
172 ICUNIT_GOTO_EQUAL(ret, MQUEUE_NO_ERROR, ret, EXIT2);
173
174 ret = pthread_attr_destroy(&attrB);
175 ICUNIT_GOTO_EQUAL(ret, MQUEUE_NO_ERROR, ret, EXIT1);
176
177 ret = pthread_attr_destroy(&attrA);
178 ICUNIT_GOTO_EQUAL(ret, MQUEUE_NO_ERROR, ret, EXIT);
179
180 return MQUEUE_NO_ERROR;
181
182 EXIT2:
183 pthread_attr_destroy(&attrC);
184 EXIT1:
185 pthread_attr_destroy(&attrB);
186 EXIT:
187 pthread_attr_destroy(&attrA);
188 return MQUEUE_NO_ERROR;
189 }
190
ItPosixQueue161(VOID)191 VOID ItPosixQueue161(VOID) // IT_Layer_ModuleORFeature_No
192 {
193 TEST_ADD_CASE("IT_POSIX_QUEUE_161", Testcase, TEST_POSIX, TEST_QUE, TEST_LEVEL2, TEST_FUNCTION);
194 }
195