Skip to content
Snippets Groups Projects
Unverified Commit 9a71886b authored by Gaëtan Harter's avatar Gaëtan Harter Committed by GitHub
Browse files

Merge pull request #7906 from aabadie/pr/tests_pexpect_pthread

tests/pthread*: adapt to testrunner scripts
parents 509ad6e4 ca9c8a5f
No related branches found
No related tags found
No related merge requests found
Showing
with 238 additions and 120 deletions
......@@ -8,3 +8,6 @@ USEMODULE += posix
USEMODULE += pthread
include $(RIOTBASE)/Makefile.include
test:
tests/01-run.py
......@@ -23,6 +23,9 @@
#include "pthread.h"
#define FACTORIAL_PARAM (6U)
#define FACTORIAL_EXPECTED (720U)
void *run(void *parameter) {
size_t n = (size_t) parameter;
size_t factorial = 1;
......@@ -45,14 +48,21 @@ int main(void) {
pthread_t th_id;
pthread_attr_t th_attr;
size_t arg = 6;
printf("main parameter = %u\n", (unsigned int) arg);
size_t arg = FACTORIAL_PARAM;
printf("main: parameter = %u\n", (unsigned int) arg);
pthread_attr_init(&th_attr);
pthread_create(&th_id, &th_attr, run, (void *) arg);
size_t res;
pthread_join(th_id, (void **) &res);
printf("main: factorial = %u\n", (unsigned int) res);
puts("main: finished");
if (res == FACTORIAL_EXPECTED) {
puts("SUCCESS");
}
else {
puts("FAILURE");
}
return 0;
}
#!/usr/bin/env python3
import os
import sys
import math
sys.path.append(os.path.join(os.environ['RIOTBASE'], 'dist/tools/testrunner'))
import testrunner
FACTORIAL_PARAM = 6
def testfunc(child):
child.expect('main: parameter = {}'.format(FACTORIAL_PARAM))
child.expect('pthread: parameter = {}'.format(FACTORIAL_PARAM))
child.expect('pthread: factorial = {}'
.format(math.factorial(FACTORIAL_PARAM)))
child.expect('main: factorial = {}'
.format(math.factorial(FACTORIAL_PARAM)))
child.expect('SUCCESS')
if __name__ == "__main__":
sys.exit(testrunner.run(testfunc))
......@@ -6,7 +6,7 @@ BOARD_BLACKLIST := arduino-mega2560 waspmote-pro arduino-uno arduino-duemilanove
# arduino mega2560 uno duemilanove: unknown type name: clockid_t
# exclude boards with insufficient memory
BOARD_INSUFFICIENT_MEMORY := nucleo32-f031 stm32f0discovery
BOARD_INSUFFICIENT_MEMORY := nucleo32-f031
# Modules to include.
USEMODULE += pthread
......@@ -14,3 +14,6 @@ USEMODULE += random
USEMODULE += xtimer
include $(RIOTBASE)/Makefile.include
test:
tests/01-run.py
......@@ -56,7 +56,7 @@ int main(void)
{
random_init(RAND_SEED);
puts("Start.\n");
puts("START\n");
pthread_barrier_init(&barrier, NULL, NUM_CHILDREN);
pthread_t children[NUM_CHILDREN];
......@@ -70,6 +70,7 @@ int main(void)
}
pthread_barrier_destroy(&barrier);
puts("\nDone.");
puts("");
puts("SUCCESS");
return 0;
}
#!/usr/bin/env python3
import os
import sys
sys.path.append(os.path.join(os.environ['RIOTBASE'], 'dist/tools/testrunner'))
import testrunner
def testfunc(child):
child.expect('START')
for i in range(4):
child.expect('Start {}'.format(i + 1))
child.expect('Done 2')
child.expect('Done 1')
child.expect('Done 3')
child.expect('Done 4')
child.expect('SUCCESS')
if __name__ == "__main__":
sys.exit(testrunner.run(testfunc))
......@@ -7,3 +7,6 @@ BOARD_BLACKLIST := arduino-mega2560 waspmote-pro arduino-uno arduino-duemilanove
USEMODULE += pthread
include $(RIOTBASE)/Makefile.include
test:
tests/01-run.py
......@@ -60,7 +60,7 @@ static void *run(void *unused) {
}
int main(void) {
puts("Start.");
puts("START");
pthread_t th_id;
pthread_create(&th_id, NULL, run, NULL);
......@@ -69,6 +69,12 @@ int main(void) {
pthread_join(th_id, (void **) &res);
printf("Result: %i\n", (int) (intptr_t) res);
puts("Done.");
if (res == RET_EXIT) {
puts("SUCCESS");
}
else {
puts("FAILURE");
}
return 0;
}
#!/usr/bin/env python3
import os
import sys
sys.path.append(os.path.join(os.environ['RIOTBASE'], 'dist/tools/testrunner'))
import testrunner
def testfunc(child):
child.expect('START')
for i in range(5):
child.expect_exact('<SCOPE {}{}>'
.format(i + 1, ' /' if i == 4 else ''))
child.expect_exact('Cleanup: <5>')
child.expect_exact('</SCOPE 4>')
child.expect_exact('</SCOPE 3>')
for i in (3, 2, 1):
child.expect_exact('Cleanup: <{}>'.format(i))
child.expect_exact('Result: 1234')
child.expect('SUCCESS')
if __name__ == "__main__":
sys.exit(testrunner.run(testfunc))
#!/usr/bin/env expect
set timeout 5
set pid [spawn make term]
puts "-*- Spawened $pid -*-\n"
set once 0
set result 1
while { $once == 0 } {
set once 1
expect {
"Start." {}
timeout { break }
}
expect {
"<SCOPE 0>" {}
timeout { break }
}
expect {
"<SCOPE 1>" {}
timeout { break }
}
expect {
"<SCOPE 2>" {}
timeout { break }
}
expect {
"<SCOPE 3>" {}
timeout { break }
}
expect {
"<SCOPE 4>" {}
timeout { break }
}
expect {
"<SCOPE 5 />" {}
timeout { break }
}
expect {
"Cleanup: <5>" {}
timeout { break }
}
expect {
"</SCOPE 4>" {}
timeout { break }
}
# Cleanup 4 has execute == 0.
expect {
"</SCOPE 3>" {}
timeout { break }
}
# pthread_exit is called here
expect {
"Cleanup: <3>" {}
timeout { break }
}
expect {
"Cleanup: <2>" {}
timeout { break }
}
expect {
"Cleanup: <1>" {}
timeout { break }
}
expect {
"Result: 1234" {}
timeout { break }
}
expect {
"Done." {}
timeout { break }
}
set result 0
}
if { $result == 0 } {
puts "\n-*- Test successful! -*-\n"
} else {
puts "\n-*- TEST HAD ERRORS! -*-\n"
}
spawn kill -9 $pid
wait
close
exit $result
......@@ -4,7 +4,7 @@ include ../Makefile.tests_common
BOARD_BLACKLIST := arduino-mega2560 waspmote-pro arduino-uno arduino-duemilanove
# arduino mega2560 uno duemilanove: unknown type name: clockid_t
BOARD_INSUFFICIENT_MEMORY := nucleo32-f031 stm32f0discovery
BOARD_INSUFFICIENT_MEMORY := nucleo32-f031
USEMODULE += posix
USEMODULE += pthread
......@@ -13,3 +13,6 @@ USEMODULE += xtimer
CFLAGS += -DNATIVE_AUTO_EXIT
include $(RIOTBASE)/Makefile.include
test:
tests/01-run.py
......@@ -52,6 +52,7 @@ static void *second_thread(void *arg)
int main(void)
{
puts("START");
count = 0;
is_finished = 0;
expected_value = 1000ul * 1000ul;
......@@ -74,10 +75,14 @@ int main(void)
puts("condition fulfilled.");
is_finished = 1;
mutex_unlock(&mutex);
return 0;
break;
}
pthread_cond_wait(&cv, &mutex);
mutex_unlock(&mutex);
}
puts("SUCCESS");
return 0;
}
#!/usr/bin/env python3
import os
import sys
sys.path.append(os.path.join(os.environ['RIOTBASE'], 'dist/tools/testrunner'))
import testrunner
def testfunc(child):
child.expect('START')
child.expect('condition fulfilled.')
child.expect('SUCCESS')
if __name__ == "__main__":
# This test can take some time to complete when testing on hardware (e.g
# on samr21-xpro) and the default timeout (10s) is not enough.
sys.exit(testrunner.run(testfunc, timeout=60))
......@@ -10,3 +10,6 @@ USEMODULE += posix
USEMODULE += pthread
include $(RIOTBASE)/Makefile.include
test:
tests/01-run.py
......@@ -23,6 +23,8 @@
#define NUM_THREADS 12
#define FACTORIAL_EXPECTED (479001600UL)
pthread_t ths[NUM_THREADS];
pthread_mutex_t mtx;
......@@ -49,6 +51,7 @@ void *run(void *parameter)
int main(void)
{
puts("START");
pthread_attr_t th_attr;
pthread_attr_init(&th_attr);
pthread_mutex_init(&mtx, NULL);
......@@ -59,19 +62,21 @@ int main(void)
}
for (int i = 0; i < NUM_THREADS; ++i) {
printf("join\n");
printf("join thread %d\n", (i + 1));
pthread_join(ths[i], NULL);
}
printf("Factorial: %d\n", storage);
if (storage != 479001600) {
puts("[!!!] Error, expected: 12!= 479001600.");
}
pthread_mutex_destroy(&mtx);
pthread_attr_destroy(&th_attr);
puts("finished");
if (storage == FACTORIAL_EXPECTED) {
puts("SUCCESS");
}
else {
puts("FAILURE: Error, expected: 12!= 479001600.");
}
return 0;
}
#!/usr/bin/env python3
import os
import sys
sys.path.append(os.path.join(os.environ['RIOTBASE'], 'dist/tools/testrunner'))
import testrunner
def testfunc(child):
child.expect('START')
for i in range(12):
child.expect('Creating thread with arg {}'.format(i + 1))
for i in range(12):
child.expect('join thread {}'.format(i + 1))
child.expect('SUCCESS')
if __name__ == "__main__":
sys.exit(testrunner.run(testfunc))
......@@ -8,11 +8,11 @@ USEMODULE += pthread
USEMODULE += xtimer
USEMODULE += random
CFLAGS += -DNATIVE_AUTO_EXIT
BOARD_INSUFFICIENT_MEMORY += airfy-beacon chronos mbed_lpc1768 msb-430 msb-430h \
nrf51dongle nrf6310 nucleo32-f031 nucleo32-f042 nucleo32-l031 \
nucleo-f030 nucleo-f334 nucleo-l053 pca10000 pca10005 \
spark-core stm32f0discovery yunjia-nrf51822
BOARD_INSUFFICIENT_MEMORY += chronos msb-430 msb-430h nucleo32-f031 \
nucleo32-f042 nucleo32-l031 nucleo-f030 \
nucleo-f334 nucleo-l053 stm32f0discovery
include $(RIOTBASE)/Makefile.include
test:
tests/01-run.py
......@@ -53,21 +53,32 @@
static pthread_rwlock_t rwlock;
static volatile unsigned counter;
#define PRINTF(FMT, ...) \
printf("%c%" PRIkernel_pid " (prio=%u): " FMT "\n", __func__[0], sched_active_pid, sched_active_thread->priority, __VA_ARGS__)
static kernel_pid_t main_thread_pid;
#define PRINTF(FMT, ...) \
printf("%c%" PRIkernel_pid " (prio=%u): " FMT "\n", \
__func__[0], sched_active_pid, \
sched_active_thread->priority, \
(int)__VA_ARGS__)
static void _notify_main_thread(void)
{
msg_t msg;
msg_send(&msg, main_thread_pid);
}
static void do_sleep(int factor)
{
uint32_t timeout_us = (random_uint32() % 100000) * factor;
/* PRINTF("sleep for % 8i µs.", timeout_us); */
PRINTF("sleep for % 8i µs.", timeout_us);
xtimer_usleep(timeout_us);
}
static void *writer(void *arg)
{
(void) arg;
/* PRINTF("%s", "start"); */
for (int i = 0; i < NUM_ITERATIONS; ++i) {
puts("start");
for (unsigned i = 0; i < NUM_ITERATIONS; ++i) {
pthread_rwlock_wrlock(&rwlock);
unsigned cur = ++counter;
do_sleep(3); /* simulate time that it takes to write the value */
......@@ -75,15 +86,16 @@ static void *writer(void *arg)
pthread_rwlock_unlock(&rwlock);
do_sleep(2);
}
/* PRINTF("%s", "done"); */
puts("done");
_notify_main_thread();
return NULL;
}
static void *reader(void *arg)
{
(void) arg;
/* PRINTF("%s", "start"); */
for (int i = 0; i < NUM_ITERATIONS; ++i) {
puts("start");
for (unsigned i = 0; i < NUM_ITERATIONS; ++i) {
pthread_rwlock_rdlock(&rwlock);
unsigned cur = counter;
do_sleep(1); /* simulate time that it takes to read the value */
......@@ -91,7 +103,9 @@ static void *reader(void *arg)
pthread_rwlock_unlock(&rwlock);
do_sleep(1);
}
/* PRINTF("%s", "done"); */
puts("done");
_notify_main_thread();
return NULL;
}
......@@ -99,7 +113,9 @@ int main(void)
{
static char stacks[NUM_CHILDREN][THREAD_STACKSIZE_MAIN];
puts("Main start.");
puts("START");
/* Get main thread pid */
main_thread_pid = thread_getpid();
for (unsigned i = 0; i < NUM_CHILDREN; ++i) {
int prio;
......@@ -132,6 +148,13 @@ int main(void)
fun, NULL, name);
}
puts("Main done.");
/* Block until all children threads are done */
for (unsigned i = 0; i < NUM_CHILDREN; ++i) {
msg_t msg;
msg_receive(&msg);
}
puts("SUCCESS");
return 0;
}
#!/usr/bin/env python3
import os
import sys
sys.path.append(os.path.join(os.environ['RIOTBASE'], 'dist/tools/testrunner'))
import testrunner
def testfunc(child):
child.expect('START')
for _ in range(8):
child.expect('start')
for _ in range(8):
child.expect('done')
child.expect('SUCCESS')
if __name__ == "__main__":
sys.exit(testrunner.run(testfunc))
......@@ -8,3 +8,6 @@ USEMODULE += posix
USEMODULE += pthread
include $(RIOTBASE)/Makefile.include
test:
tests/01-run.py
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment