diff options
author | jorton <jorton@13f79535-47bb-0310-9956-ffa450edef68> | 2003-07-02 12:12:30 +0000 |
---|---|---|
committer | jorton <jorton@13f79535-47bb-0310-9956-ffa450edef68> | 2003-07-02 12:12:30 +0000 |
commit | 91a38af0edd750f9dbf7381ac822c2d822601437 (patch) | |
tree | 5e4f5d49f645f97a248da3943f6d3c610d0ded98 /test | |
parent | a2234e56f60f4ceded0f53fef45515a967bc78e0 (diff) | |
download | libapr-91a38af0edd750f9dbf7381ac822c2d822601437.tar.gz |
CuTest-ify and clean up the testprocmutex test:
- make the increment operation use a slow load/increment/store cycle.
- dropped support for specifying a lock filename, hopefully that is
not critical
- try using anonymous shm then name-based shm; fail gracefully
rather than segfaulting if neither works.
git-svn-id: http://svn.apache.org/repos/asf/apr/apr/trunk@64555 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'test')
-rw-r--r-- | test/Makefile.in | 3 | ||||
-rw-r--r-- | test/test_apr.h | 1 | ||||
-rw-r--r-- | test/testall.c | 1 | ||||
-rw-r--r-- | test/testprocmutex.c | 142 |
4 files changed, 56 insertions, 91 deletions
diff --git a/test/Makefile.in b/test/Makefile.in index 096cf89d0..f61f7d5f5 100644 --- a/test/Makefile.in +++ b/test/Makefile.in @@ -22,7 +22,6 @@ STDTEST_PORTABLE = \ STDTEST_NONPORTABLE = \ testshm@EXEEXT@ \ - testprocmutex@EXEEXT@ \ testglobalmutex@EXEEXT@ OTHER_PROGRAMS = client@EXEEXT@ sendfile@EXEEXT@ \ @@ -118,7 +117,7 @@ TESTS = testall.lo testtime.lo teststr.lo testvsn.lo testipsub.lo \ testdso.lo testoc.lo testdup.lo testsockets.lo testproc.lo \ testpoll.lo testlock.lo testsockopt.lo testpipe.lo testthread.lo \ testhash.lo testargs.lo testnames.lo testuser.lo testpath.lo \ - testenv.lo + testenv.lo testprocmutex.lo testall: $(TESTS) mod_test.la libmod_test.la occhild@EXEEXT@ \ readchild@EXEEXT@ CuTest.lo proc_child@EXEEXT@ $(LOCAL_LIBS) diff --git a/test/test_apr.h b/test/test_apr.h index 90c6ffabe..b378d0cbc 100644 --- a/test/test_apr.h +++ b/test/test_apr.h @@ -86,6 +86,7 @@ CuSuite *testoc(void); CuSuite *testdup(void); CuSuite *testsockets(void); CuSuite *testproc(void); +CuSuite *testprocmutex(void); CuSuite *testpoll(void); CuSuite *testlock(void); CuSuite *testsockopt(void); diff --git a/test/testall.c b/test/testall.c index a7d803064..169188d9e 100644 --- a/test/testall.c +++ b/test/testall.c @@ -100,6 +100,7 @@ static const struct testlist { {"testsockets", testsockets}, {"testsockopt", testsockopt}, {"testproc", testproc}, + {"testprocmutex", testprocmutex}, {"testpoll", testpoll}, {"testlock", testlock}, {"testthread", testthread}, diff --git a/test/testprocmutex.c b/test/testprocmutex.c index fe8067c04..2cf1ef8c7 100644 --- a/test/testprocmutex.c +++ b/test/testprocmutex.c @@ -64,15 +64,22 @@ #include <stdlib.h> #include "test_apr.h" +#if APR_HAS_FORK -#define MAX_ITER 4000 +#define MAX_ITER 200 #define MAX_COUNTER (MAX_ITER * 4) -apr_proc_mutex_t *proc_lock; -apr_pool_t *pool; -int *x; +static apr_proc_mutex_t *proc_lock; +static volatile int *x; -static int make_child(apr_proc_t **proc, apr_pool_t *p) +/* a slower more racy may to implement (*x)++ */ +static int increment(int n) +{ + apr_sleep(1); + return n+1; +} + +static void make_child(apr_proc_t **proc, apr_pool_t *p) { int i = 0; *proc = apr_pcalloc(p, sizeof(**proc)); @@ -91,112 +98,69 @@ static int make_child(apr_proc_t **proc, apr_pool_t *p) */ apr_initialize(); - while (1) { + do { apr_proc_mutex_lock(proc_lock); - if (i == MAX_ITER) { - apr_proc_mutex_unlock(proc_lock); - exit(1); - } i++; - (*x)++; + *x = increment(*x); apr_proc_mutex_unlock(proc_lock); - } + } while (i < MAX_ITER); exit(1); } - return APR_SUCCESS; } -static apr_status_t test_exclusive(const char *lockname) +static void test_exclusive(CuTest *tc, const char *lockname) { apr_proc_t *p1, *p2, *p3, *p4; - apr_status_t s1, s2, s3, s4; - - printf("Exclusive lock test\n"); - printf("%-60s", " Initializing the lock"); - s1 = apr_proc_mutex_create(&proc_lock, lockname, APR_LOCK_DEFAULT, pool); - - if (s1 != APR_SUCCESS) { - printf("Failed!\n"); - return s1; - } - printf("OK\n"); + apr_status_t rv; - printf("%-60s", " Starting all of the processes"); - fflush(stdout); - s1 = make_child(&p1, pool); - s2 = make_child(&p2, pool); - s3 = make_child(&p3, pool); - s4 = make_child(&p4, pool); - if (s1 != APR_SUCCESS || s2 != APR_SUCCESS || - s3 != APR_SUCCESS || s4 != APR_SUCCESS) { - printf("Failed!\n"); - return s1; - } - printf("OK\n"); + rv = apr_proc_mutex_create(&proc_lock, lockname, APR_LOCK_DEFAULT, p); + apr_assert_success(tc, "create the mutex", rv); - printf("%-60s", " Waiting for processes to exit"); - s1 = apr_proc_wait(p1, NULL, NULL, APR_WAIT); - s2 = apr_proc_wait(p2, NULL, NULL, APR_WAIT); - s3 = apr_proc_wait(p3, NULL, NULL, APR_WAIT); - s4 = apr_proc_wait(p4, NULL, NULL, APR_WAIT); - printf("OK\n"); + make_child(&p1, p); + make_child(&p2, p); + make_child(&p3, p); + make_child(&p4, p); - if ((*x) != MAX_COUNTER) { - fprintf(stderr, "Locks don't appear to work! x = %d instead of %d\n", - (*x), MAX_COUNTER); - } - else { - printf("Test passed\n"); - } - return APR_SUCCESS; + apr_proc_wait(p1, NULL, NULL, APR_WAIT); + apr_proc_wait(p2, NULL, NULL, APR_WAIT); + apr_proc_wait(p3, NULL, NULL, APR_WAIT); + apr_proc_wait(p4, NULL, NULL, APR_WAIT); + + CuAssert(tc, "Locks don't appear to work", *x == MAX_COUNTER); } +#endif -int main(int argc, const char * const *argv) +static void proc_mutex(CuTest *tc) { +#if APR_HAS_FORK apr_status_t rv; - char errmsg[200]; - const char *lockname = NULL; - const char *shmname = "shm.file"; - apr_getopt_t *opt; - char optchar; - const char *optarg; + const char *lockname = "tpm.lock"; + const char *shmname = "tpm.shm"; apr_shm_t *shm; - printf("APR Proc Mutex Test\n==============\n\n"); - - apr_initialize(); - atexit(apr_terminate); - - if (apr_pool_create(&pool, NULL) != APR_SUCCESS) - exit(-1); - - if ((rv = apr_getopt_init(&opt, pool, argc, argv)) != APR_SUCCESS) { - fprintf(stderr, "Could not set up to parse options: [%d] %s\n", - rv, apr_strerror(rv, errmsg, sizeof errmsg)); - exit(-1); - } - - while ((rv = apr_getopt(opt, "f:", &optchar, &optarg)) == APR_SUCCESS) { - if (optchar == 'f') { - lockname = optarg; - } + /* Use anonymous shm if available. */ + rv = apr_shm_create(&shm, sizeof(int), NULL, p); + if (rv == APR_ENOTIMPL) { + apr_file_remove(shmname, p); + rv = apr_shm_create(&shm, sizeof(int), shmname, p); } - if (rv != APR_SUCCESS && rv != APR_EOF) { - fprintf(stderr, "Could not parse options: [%d] %s\n", - rv, apr_strerror(rv, errmsg, sizeof errmsg)); - exit(-1); - } + apr_assert_success(tc, "create shm segment", rv); - apr_shm_create(&shm, sizeof(int), shmname, pool); x = apr_shm_baseaddr_get(shm); + test_exclusive(tc, lockname); +#else + CuNotImpl(tc, "APR lacks fork() support"); +#endif +} - if ((rv = test_exclusive(lockname)) != APR_SUCCESS) { - fprintf(stderr,"Exclusive Lock test failed : [%d] %s\n", - rv, apr_strerror(rv, (char*)errmsg, 200)); - exit(-2); - } - - return 0; + +CuSuite *testprocmutex(void) +{ + CuSuite *suite = CuSuiteNew("Cross-Process Mutexes"); + + SUITE_ADD_TEST(suite, proc_mutex); + + return suite; } |