From eb107a08cba496550cda32bf6952be10053df328 Mon Sep 17 00:00:00 2001 From: Francisco Molina Date: Tue, 21 Jul 2020 17:08:50 +0200 Subject: [PATCH] tests/pthread_flood: cleanup test - Cleans up pthread initialization, checks for ret code !=0, instead of -1. For some platforms the first thread was not started and therefore `pthread_join` could not succeed. - Always specify stackaddr and stacksize to not use malloc - Formatting and uncrustify --- tests/pthread_flood/main.c | 56 ++++++++++++----------------- tests/pthread_flood/tests/01-run.py | 11 +++--- 2 files changed, 29 insertions(+), 38 deletions(-) diff --git a/tests/pthread_flood/main.c b/tests/pthread_flood/main.c index e0ada3427d..1f13848b52 100644 --- a/tests/pthread_flood/main.c +++ b/tests/pthread_flood/main.c @@ -28,6 +28,7 @@ #include "mutex.h" static char dummy_stack[MAXTHREADS][THREAD_STACKSIZE_IDLE]; +static pthread_t pthread_ids[MAXTHREADS]; static mutex_t testing_mutex; static void *thread_func(void *arg) @@ -43,56 +44,43 @@ int main(void) mutex_init(&testing_mutex); mutex_lock(&testing_mutex); - - int pthread_cnt = 0; - - pthread_t pthread_ids[MAXTHREADS]; - pthread_attr_t th_attr; pthread_attr_init(&th_attr); - if (-1 == - pthread_create(&(pthread_ids[pthread_cnt % MAXTHREADS]), &th_attr, thread_func, - NULL)) { - puts("[ERROR] cannot create pthreads"); - return 0; - } - volatile int numthread_check = sched_num_threads; + int numthread_check = sched_num_threads; - int exit_loop = -1; - - puts("[START] Spawning threads"); - do { - pthread_attr_setstackaddr(&th_attr, - &(dummy_stack[pthread_cnt % MAXTHREADS])); + puts("Spawning pthreads"); + int pthread_cnt = 0; + for (uint8_t i = 0; i < MAXTHREADS; i++) { + pthread_attr_setstackaddr(&th_attr, &(dummy_stack[i])); pthread_attr_setstacksize(&th_attr, THREAD_STACKSIZE_IDLE); - exit_loop = pthread_create(&(pthread_ids[pthread_cnt + 1 % MAXTHREADS]), &th_attr, - thread_func, NULL); - if (exit_loop == 0) { - ++pthread_cnt; - printf("."); + if (pthread_create(&(pthread_ids[i]), &th_attr, thread_func, NULL)) { + break; } - } while (-1 != exit_loop); - volatile int numthread_check_after = sched_num_threads; - + printf("."); + pthread_cnt++; + } puts(""); - if (numthread_check_after - numthread_check == pthread_cnt && - numthread_check_after == MAXTHREADS) { - printf("[SUCCESS]\n"); - } - else { - printf("[ERROR] expected %d,", (MAXTHREADS - numthread_check)); + + int numthread_check_after = sched_num_threads; + + if ((numthread_check_after - numthread_check) != pthread_cnt || + numthread_check_after != MAXTHREADS) { + printf("[ERROR] expected %d, ", (MAXTHREADS - numthread_check)); } + printf("created %d pthreads\n", pthread_cnt); printf("created %d threads\n", numthread_check_after - numthread_check); + mutex_unlock(&testing_mutex); - for (int i = 0; i < pthread_cnt; i++) { + puts("wait for created pthreads to exit..."); + for (uint8_t i = 0; i < pthread_cnt; i++) { if (pthread_ids[i] != 0) { pthread_join(pthread_ids[i], NULL); } } - puts("test end"); + puts("SUCCESS"); return 0; } diff --git a/tests/pthread_flood/tests/01-run.py b/tests/pthread_flood/tests/01-run.py index b303ebc299..d50a4f7112 100755 --- a/tests/pthread_flood/tests/01-run.py +++ b/tests/pthread_flood/tests/01-run.py @@ -5,10 +5,13 @@ from testrunner import run def testfunc(child): - child.expect_exact(u'[START] Spawning threads') - child.expect(r'\.+') - child.expect("[SUCCESS]") - child.expect("test end") + child.expect_exact("Spawning pthreads") + child.expect(r"created (\d+) pthreads") + pthread_num = int(child.match.group(1)) + child.expect(r"created (\d+) threads") + thread_num = int(child.match.group(1)) + assert thread_num == pthread_num + child.expect_exact("SUCCESS") if __name__ == "__main__":