Skip to content

Commit b316cca

Browse files
Nicolas Pitreycsin
Nicolas Pitre
authored andcommitted
tests: posix: semaphore: assorted adjustments
- Adjust refcount checks with regards to previous commit. - Remove redundant zassert_not_null() on local pointers after a sem_close(). There is no implicit reference passing in C. Signed-off-by: Nicolas Pitre <[email protected]>
1 parent 62e7378 commit b316cca

File tree

1 file changed

+16
-17
lines changed

1 file changed

+16
-17
lines changed

tests/posix/common/src/semaphore.c

+16-17
Original file line numberDiff line numberDiff line change
@@ -181,14 +181,14 @@ ZTEST(posix_apis, test_named_semaphore)
181181

182182
/* Open named sem */
183183
sem1 = sem_open("sem1", O_CREAT, 0, 0);
184-
zassert_equal(nsem_get_ref_count(sem1), 1);
184+
zassert_equal(nsem_get_ref_count(sem1), 2);
185185
zassert_equal(nsem_get_list_len(), 1);
186186
sem2 = sem_open("sem2", O_CREAT, 0, 0);
187-
zassert_equal(nsem_get_ref_count(sem2), 1);
187+
zassert_equal(nsem_get_ref_count(sem2), 2);
188188
zassert_equal(nsem_get_list_len(), 2);
189189

190190
/* Open created named sem repeatedly */
191-
for (size_t i = 1; i < N_LOOPS; i++) {
191+
for (size_t i = 1; i <= N_LOOPS; i++) {
192192
sem_t *new_sem1, *new_sem2;
193193

194194
/* oflags are ignored (except when both O_CREAT & O_EXCL are set) */
@@ -200,8 +200,8 @@ ZTEST(posix_apis, test_named_semaphore)
200200
zassert_equal_ptr(new_sem2, sem2);
201201

202202
/* ref_count should increment */
203-
zassert_equal(nsem_get_ref_count(sem1), i + 1);
204-
zassert_equal(nsem_get_ref_count(sem2), i + 1);
203+
zassert_equal(nsem_get_ref_count(sem1), 2 + i);
204+
zassert_equal(nsem_get_ref_count(sem2), 2 + i);
205205

206206
/* Should reuse the same named sem instead of creating another one */
207207
zassert_equal(nsem_get_list_len(), 2);
@@ -217,16 +217,14 @@ ZTEST(posix_apis, test_named_semaphore)
217217
zassert_equal(nsem_get_list_len(), 2);
218218

219219
/* Close sem */
220-
for (size_t i = 0;
220+
for (size_t i = N_LOOPS;
221221
/* close until one left, required by the test later */
222-
i < (N_LOOPS - 1); i++) {
222+
i >= 1; i--) {
223223
zassert_ok(sem_close(sem1));
224-
zassert_not_null(sem1);
225-
zassert_equal(nsem_get_ref_count(sem1), N_LOOPS - (i + 1));
224+
zassert_equal(nsem_get_ref_count(sem1), 2 + i - 1);
226225

227226
zassert_ok(sem_close(sem2));
228-
zassert_not_null(sem2);
229-
zassert_equal(nsem_get_ref_count(sem2), N_LOOPS - (i + 1));
227+
zassert_equal(nsem_get_ref_count(sem2), 2 + i - 1);
230228

231229
zassert_equal(nsem_get_list_len(), 2);
232230
}
@@ -254,9 +252,10 @@ ZTEST(posix_apis, test_named_semaphore)
254252
zassert_equal(nsem_get_list_len(), 2);
255253

256254
/* Unlink sem1 when it is still being used */
257-
zassert_equal(nsem_get_ref_count(sem1), 1);
255+
zassert_equal(nsem_get_ref_count(sem1), 2);
258256
zassert_ok(sem_unlink("sem1"));
259257
/* sem won't be destroyed */
258+
zassert_equal(nsem_get_ref_count(sem1), 1);
260259
zassert_equal(nsem_get_list_len(), 2);
261260

262261
/* Create another sem with the name of an unlinked sem */
@@ -274,15 +273,15 @@ ZTEST(posix_apis, test_named_semaphore)
274273

275274
/* Closing a linked sem won't destroy the sem */
276275
zassert_ok(sem_close(sem2));
277-
zassert_equal(nsem_get_ref_count(sem2), 0);
276+
zassert_equal(nsem_get_ref_count(sem2), 1);
278277
zassert_equal(nsem_get_list_len(), 2);
279278

280279
/* Instead the sem will be destroyed upon call to sem_unlink() */
281280
zassert_ok(sem_unlink("sem2"));
282281
zassert_equal(nsem_get_list_len(), 1);
283282

284-
/* What we have left open here is `different_sem` as "sem1", which has 1 ref_count */
285-
zassert_equal(nsem_get_ref_count(different_sem1), 1);
283+
/* What we have left open here is `different_sem` as "sem1", which has a ref_count of 2 */
284+
zassert_equal(nsem_get_ref_count(different_sem1), 2);
286285

287286
/* Stress test: open & close "sem1" repeatedly */
288287
zassert_ok(pthread_create(&thread1, NULL, nsem_open_func, "sem1"));
@@ -298,14 +297,14 @@ ZTEST(posix_apis, test_named_semaphore)
298297
/* Create a new named sem to be used in the normal semaphore test */
299298
sem1 = sem_open("nsem", O_CREAT, 0, 0);
300299
zassert_equal(nsem_get_list_len(), 1);
301-
zassert_equal(nsem_get_ref_count(sem1), 1);
300+
zassert_equal(nsem_get_ref_count(sem1), 2);
302301

303302
/* Run the semaphore test with the created named semaphore */
304303
semaphore_test(sem1);
305304

306305
/* List length and ref_count shouldn't change after the test */
307306
zassert_equal(nsem_get_list_len(), 1);
308-
zassert_equal(nsem_get_ref_count(sem1), 1);
307+
zassert_equal(nsem_get_ref_count(sem1), 2);
309308

310309
/* Unless it is unlinked and closed */
311310
sem_unlink("nsem");

0 commit comments

Comments
 (0)