From edafc80b4a73b5193662438f3485d2d01b8fe82a Mon Sep 17 00:00:00 2001 From: rpj Date: Thu, 6 Jan 2000 01:10:11 +0000 Subject: New cleanup handler execution tests. --- tests/ChangeLog | 11 ++++ tests/Makefile | 15 +++-- tests/cleanup1.c | 191 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ tests/cleanup2.c | 170 +++++++++++++++++++++++++++++++++++++++++++++++++ tests/cleanup3.c | 173 +++++++++++++++++++++++++++++++++++++++++++++++++ tests/runall.bat | 11 ++-- 6 files changed, 562 insertions(+), 9 deletions(-) create mode 100644 tests/cleanup1.c create mode 100644 tests/cleanup2.c create mode 100644 tests/cleanup3.c (limited to 'tests') diff --git a/tests/ChangeLog b/tests/ChangeLog index cd155f8..8e65a35 100644 --- a/tests/ChangeLog +++ b/tests/ChangeLog @@ -1,3 +1,14 @@ +2000-01-06 Ross Johnson + + * cleanup1.c: New; Test cleanup handler executes (when thread is + canceled). + + * cleanup2.c: New; Test cleanup handler executes (when thread is + not canceled). + + * cleanup3.c: New; Test cleanup handler does not execute + (when thread is not canceled). + 2000-01-04 Ross Johnson * cancel4.c: New; Test cancelation does not occur in deferred diff --git a/tests/Makefile b/tests/Makefile index 8820aac..ef1d30f 100644 --- a/tests/Makefile +++ b/tests/Makefile @@ -36,20 +36,22 @@ COPYFILES = $(HDR) $(LIB) $(DLL) # If a test case returns a non-zero exit code to the shell, make will # stop. -TESTS = mutex1 condvar1 condvar2 exit1 create1 equal1 \ +TESTS = loadfree \ + mutex1 condvar1 condvar2 exit1 create1 equal1 \ exit2 exit3 \ join1 join2 mutex2 mutex3 \ count1 once1 tsd1 self1 self2 cancel1 eyal1 \ condvar3 condvar4 condvar5 condvar6 condvar7 condvar8 condvar9 \ errno1 \ rwlock1 rwlock2 rwlock3 rwlock4 rwlock5 rwlock6 \ - context1 cancel3 cancel4 + context1 cancel3 cancel4 cleanup1 cleanup2 cleanup3 PASSES = $(TESTS:%=%.pass) all: $(PASSES) @ $(ECHO) ALL TESTS PASSED! Congratulations! +loadfree.pass: mutex1.pass: mutex2.pass: exit1.pass: @@ -72,9 +74,9 @@ condvar3.pass: create1.pass condvar4.pass: create1.pass condvar5.pass: condvar4.pass condvar6.pass: condvar5.pass -condvar7.pass: condvar6.pass -condvar8.pass: condvar6.pass -condvar9.pass: condvar6.pass +condvar7.pass: condvar6.pass cleanup1.pass +condvar8.pass: condvar7.pass +condvar9.pass: condvar8.pass errno1.pass: mutex3.pass rwlock1.pass: condvar6.pass rwlock2.pass: rwlock1.pass @@ -85,6 +87,9 @@ rwlock6.pass: rwlock5.pass context1.pass: cancel2.pass cancel3.pass: context1.pass cancel4.pass: cancel3.pass +cleanup1.pass: cancel4.pass +cleanup2.pass: cleanup1.pass +cleanup3.pass: cleanup2.pass %.pass: %.exe $(LIB) $(DLL) $(HDR) $* diff --git a/tests/cleanup1.c b/tests/cleanup1.c new file mode 100644 index 0000000..4939e6f --- /dev/null +++ b/tests/cleanup1.c @@ -0,0 +1,191 @@ +/* + * File: cleanup1.c + * + * Test Synopsis: Test cleanup handler executes (when thread is canceled). + * + * Test Method (Validation or Falsification): + * - + * + * Requirements Tested: + * - + * + * Features Tested: + * - + * + * Cases Tested: + * - + * + * Description: + * - + * + * Environment: + * - + * + * Input: + * - None. + * + * Output: + * - File name, Line number, and failed expression on failure. + * - No output on success. + * + * Assumptions: + * - have working pthread_create, pthread_self, pthread_mutex_lock/unlock + * pthread_testcancel, pthread_cancel, pthread_join + * + * Pass Criteria: + * - Process returns zero exit status. + * + * Fail Criteria: + * - Process returns non-zero exit status. + */ + +#if defined(_MSC_VER) || defined(__cplusplus) + +#include "test.h" + +/* + * Create NUMTHREADS threads in addition to the Main thread. + */ +enum { + NUMTHREADS = 10 +}; + +typedef struct bag_t_ bag_t; +struct bag_t_ { + int threadnum; + int started; + /* Add more per-thread state variables here */ + int count; +}; + +static bag_t threadbag[NUMTHREADS + 1]; + +static pthread_mutex_t waitLock = PTHREAD_MUTEX_INITIALIZER; + +static int pop_count = 0; + +static void +increment_pop_count(void * arg) +{ + int * c = (int *) arg; + + (*c)++; +} + +void * +mythread(void * arg) +{ + int result = 0; + bag_t * bag = (bag_t *) arg; + + assert(bag == &threadbag[bag->threadnum]); + assert(bag->started == 0); + bag->started = 1; + + /* Set to known state and type */ + + assert(pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL) == 0); + + assert(pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, NULL) == 0); + + pthread_cleanup_push(increment_pop_count, (void *) &pop_count); + /* + * We wait up to 10 seconds, waking every 0.1 seconds, + * for a cancelation to be applied to us. + */ + for (bag->count = 0; bag->count < 100; bag->count++) + Sleep(100); + + pthread_cleanup_pop(0); + + return result; +} + +int +main() +{ + int failed = 0; + int i; + pthread_t t[NUMTHREADS + 1]; + + assert((t[0] = pthread_self()) != NULL); + + for (i = 1; i <= NUMTHREADS; i++) + { + threadbag[i].started = 0; + threadbag[i].threadnum = i; + assert(pthread_create(&t[i], NULL, mythread, (void *) &threadbag[i]) == 0); + } + + /* + * Code to control or munipulate child threads should probably go here. + */ + Sleep(500); + + for (i = 1; i <= NUMTHREADS; i++) + { + assert(pthread_cancel(t[i]) == 0); + } + + /* + * Give threads time to run. + */ + Sleep(NUMTHREADS * 100); + + /* + * Standard check that all threads started. + */ + for (i = 1; i <= NUMTHREADS; i++) + { + if (!threadbag[i].started) + { + failed |= !threadbag[i].started; + fprintf(stderr, "Thread %d: started %d\n", i, threadbag[i].started); + } + } + + assert(!failed); + + /* + * Check any results here. Set "failed" and only print output on failure. + */ + failed = 0; + for (i = 1; i <= NUMTHREADS; i++) + { + int fail = 0; + int result = 0; + + assert(pthread_join(t[i], (void *) &result) == 0); + + fail = (result != (int) PTHREAD_CANCELED); + + if (fail) + { + fprintf(stderr, "Thread %d: started %d: count %d: result %d\n", + i, + threadbag[i].started, + threadbag[i].count, + result); + } + failed = (failed || fail); + } + + assert(!failed); + + assert(pop_count == NUMTHREADS); + + /* + * Success. + */ + return 0; +} + +#else /* defined(_MSC_VER) || defined(__cplusplus) */ + +int +main() +{ + return 0; +} + +#endif /* defined(_MSC_VER) || defined(__cplusplus) */ diff --git a/tests/cleanup2.c b/tests/cleanup2.c new file mode 100644 index 0000000..c6ca529 --- /dev/null +++ b/tests/cleanup2.c @@ -0,0 +1,170 @@ +/* + * File: cleanup2.c + * + * Test Synopsis: Test cleanup handler executes (when thread is not canceled). + * + * Test Method (Validation or Falsification): + * - + * + * Requirements Tested: + * - + * + * Features Tested: + * - + * + * Cases Tested: + * - + * + * Description: + * - + * + * Environment: + * - + * + * Input: + * - None. + * + * Output: + * - File name, Line number, and failed expression on failure. + * - No output on success. + * + * Assumptions: + * - have working pthread_create, pthread_self, pthread_mutex_lock/unlock + * pthread_testcancel, pthread_cancel, pthread_join + * + * Pass Criteria: + * - Process returns zero exit status. + * + * Fail Criteria: + * - Process returns non-zero exit status. + */ + +#if defined(_MSC_VER) || defined(__cplusplus) + +#include "test.h" + +/* + * Create NUMTHREADS threads in addition to the Main thread. + */ +enum { + NUMTHREADS = 10 +}; + +typedef struct bag_t_ bag_t; +struct bag_t_ { + int threadnum; + int started; + /* Add more per-thread state variables here */ + int count; +}; + +static bag_t threadbag[NUMTHREADS + 1]; + +static pthread_mutex_t waitLock = PTHREAD_MUTEX_INITIALIZER; + +static int pop_count = 0; + +static void +increment_pop_count(void * arg) +{ + int * c = (int *) arg; + + (*c)++; +} + +void * +mythread(void * arg) +{ + int result = 0; + bag_t * bag = (bag_t *) arg; + + assert(bag == &threadbag[bag->threadnum]); + assert(bag->started == 0); + bag->started = 1; + + pthread_cleanup_push(increment_pop_count, (void *) &pop_count); + + sched_yield(); + + pthread_cleanup_pop(1); + + return result; +} + +int +main() +{ + int failed = 0; + int i; + pthread_t t[NUMTHREADS + 1]; + + assert((t[0] = pthread_self()) != NULL); + + for (i = 1; i <= NUMTHREADS; i++) + { + threadbag[i].started = 0; + threadbag[i].threadnum = i; + assert(pthread_create(&t[i], NULL, mythread, (void *) &threadbag[i]) == 0); + } + + /* + * Code to control or munipulate child threads should probably go here. + */ + Sleep(1000); + + /* + * Standard check that all threads started. + */ + for (i = 1; i <= NUMTHREADS; i++) + { + if (!threadbag[i].started) + { + failed |= !threadbag[i].started; + fprintf(stderr, "Thread %d: started %d\n", i, threadbag[i].started); + } + } + + assert(!failed); + + /* + * Check any results here. Set "failed" and only print output on failure. + */ + failed = 0; + for (i = 1; i <= NUMTHREADS; i++) + { + int fail = 0; + int result = 0; + + assert(pthread_join(t[i], (void *) &result) == 0); + + fail = (result != 0); + + if (fail) + { + fprintf(stderr, "Thread %d: started %d: result: %d\n", + i, + threadbag[i].started, + result); + } + failed = (failed || fail); + } + + assert(!failed); + + assert(pop_count == NUMTHREADS); + + /* + * Success. + */ + return 0; +} + +#else /* defined(_MSC_VER) || defined(__cplusplus) */ + +int +main() +{ + return 0; +} + +#endif /* defined(_MSC_VER) || defined(__cplusplus) */ diff --git a/tests/cleanup3.c b/tests/cleanup3.c new file mode 100644 index 0000000..ff84c0b --- /dev/null +++ b/tests/cleanup3.c @@ -0,0 +1,173 @@ +/* + * File: cleanup3.c + * + * Test Synopsis: Test cleanup handler does not execute (when thread is + * not canceled). + * + * Test Method (Validation or Falsification): + * - + * + * Requirements Tested: + * - + * + * Features Tested: + * - + * + * Cases Tested: + * - + * + * Description: + * - + * + * Environment: + * - + * + * Input: + * - None. + * + * Output: + * - File name, Line number, and failed expression on failure. + * - No output on success. + * + * Assumptions: + * - have working pthread_create, pthread_self, pthread_mutex_lock/unlock + * pthread_testcancel, pthread_cancel, pthread_join + * + * Pass Criteria: + * - Process returns zero exit status. + * + * Fail Criteria: + * - Process returns non-zero exit status. + */ + +#if defined(_MSC_VER) || defined(__cplusplus) + +#include "test.h" + +/* + * Create NUMTHREADS threads in addition to the Main thread. + */ +enum { + NUMTHREADS = 10 +}; + +typedef struct bag_t_ bag_t; +struct bag_t_ { + int threadnum; + int started; + /* Add more per-thread state variables here */ + int count; +}; + +static bag_t threadbag[NUMTHREADS + 1]; + +static pthread_mutex_t waitLock = PTHREAD_MUTEX_INITIALIZER; + +static int pop_count = 0; + +static void +increment_pop_count(void * arg) +{ + int * c = (int *) arg; + + (*c)++; +} + +void * +mythread(void * arg) +{ + int result = 0; + bag_t * bag = (bag_t *) arg; + + assert(bag == &threadbag[bag->threadnum]); + assert(bag->started == 0); + bag->started = 1; + + pthread_cleanup_push(increment_pop_count, (void *) &pop_count); + + sched_yield(); + + pop_count--; + + pthread_cleanup_pop(0); + + return result; +} + +int +main() +{ + int failed = 0; + int i; + pthread_t t[NUMTHREADS + 1]; + + assert((t[0] = pthread_self()) != NULL); + + for (i = 1; i <= NUMTHREADS; i++) + { + threadbag[i].started = 0; + threadbag[i].threadnum = i; + assert(pthread_create(&t[i], NULL, mythread, (void *) &threadbag[i]) == 0); + } + + /* + * Code to control or munipulate child threads should probably go here. + */ + Sleep(1000); + + /* + * Standard check that all threads started. + */ + for (i = 1; i <= NUMTHREADS; i++) + { + if (!threadbag[i].started) + { + failed |= !threadbag[i].started; + fprintf(stderr, "Thread %d: started %d\n", i, threadbag[i].started); + } + } + + assert(!failed); + + /* + * Check any results here. Set "failed" and only print output on failure. + */ + failed = 0; + for (i = 1; i <= NUMTHREADS; i++) + { + int fail = 0; + int result = 0; + + assert(pthread_join(t[i], (void *) &result) == 0); + + fail = (result != 0); + + if (fail) + { + fprintf(stderr, "Thread %d: started %d: result: %d\n", + i, + threadbag[i].started, + result); + } + failed = (failed || fail); + } + + assert(!failed); + + assert(pop_count == -(NUMTHREADS)); + + /* + * Success. + */ + return 0; +} + +#else /* defined(_MSC_VER) || defined(__cplusplus) */ + +int +main() +{ + return 0; +} + +#endif /* defined(_MSC_VER) || defined(__cplusplus) */ diff --git a/tests/runall.bat b/tests/runall.bat index d8336c3..ace1962 100644 --- a/tests/runall.bat +++ b/tests/runall.bat @@ -6,6 +6,7 @@ if EXIST *.fail echo y | erase *.fail > nul: if EXIST *.notrun echo y | erase *.notrun > nul: :noforce +call runtest cl loadfree _ call runtest cl mutex1 _ call runtest cl mutex2 _ call runtest cl exit1 _ @@ -30,9 +31,6 @@ call runtest cl condvar3 create1 call runtest cl condvar4 create1 call runtest cl condvar5 condvar4 call runtest cl condvar6 condvar5 -call runtest cl condvar7 condvar6 -call runtest cl condvar8 condvar7 -call runtest cl condvar9 condvar8 call runtest cl errno1 mutex3 call runtest cl rwlock1 condvar6 call runtest cl rwlock2 rwlock1 @@ -43,7 +41,12 @@ call runtest cl rwlock6 rwlock5 call runtest cl context1 cancel2 call runtest cl cancel3 context1 call runtest cl cancel4 cancel3 -call runtest cl loadfree _ +call runtest cl cleanup1 cancel4 +call runtest cl cleanup2 cleanup1 +call runtest cl cleanup3 cleanup2 +call runtest cl condvar7 cleanup1 +call runtest cl condvar8 condvar7 +call runtest cl condvar9 condvar8 if NOT EXIST *.notrun goto skip1 echo The following tests did not run (because prerequisite didn't pass?): -- cgit v1.2.3