You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@apr.apache.org by jo...@apache.org on 2003/07/02 14:12:30 UTC
cvs commit: apr/test Makefile.in test_apr.h testall.c testprocmutex.c
jorton 2003/07/02 05:12:30
Modified: test Makefile.in test_apr.h testall.c testprocmutex.c
Log:
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.
Revision Changes Path
1.140 +1 -2 apr/test/Makefile.in
Index: Makefile.in
===================================================================
RCS file: /home/cvs/apr/test/Makefile.in,v
retrieving revision 1.139
retrieving revision 1.140
diff -u -u -r1.139 -r1.140
--- Makefile.in 5 Jun 2003 02:14:08 -0000 1.139
+++ Makefile.in 2 Jul 2003 12:12:28 -0000 1.140
@@ -22,7 +22,6 @@
STDTEST_NONPORTABLE = \
testshm@EXEEXT@ \
- testprocmutex@EXEEXT@ \
testglobalmutex@EXEEXT@
OTHER_PROGRAMS = client@EXEEXT@ sendfile@EXEEXT@ \
@@ -118,7 +117,7 @@
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)
1.42 +1 -0 apr/test/test_apr.h
Index: test_apr.h
===================================================================
RCS file: /home/cvs/apr/test/test_apr.h,v
retrieving revision 1.41
retrieving revision 1.42
diff -u -u -r1.41 -r1.42
--- test_apr.h 17 Feb 2003 03:47:09 -0000 1.41
+++ test_apr.h 2 Jul 2003 12:12:28 -0000 1.42
@@ -86,6 +86,7 @@
CuSuite *testdup(void);
CuSuite *testsockets(void);
CuSuite *testproc(void);
+CuSuite *testprocmutex(void);
CuSuite *testpoll(void);
CuSuite *testlock(void);
CuSuite *testsockopt(void);
1.44 +1 -0 apr/test/testall.c
Index: testall.c
===================================================================
RCS file: /home/cvs/apr/test/testall.c,v
retrieving revision 1.43
retrieving revision 1.44
diff -u -u -r1.43 -r1.44
--- testall.c 18 May 2003 12:50:49 -0000 1.43
+++ testall.c 2 Jul 2003 12:12:30 -0000 1.44
@@ -100,6 +100,7 @@
{"testsockets", testsockets},
{"testsockopt", testsockopt},
{"testproc", testproc},
+ {"testprocmutex", testprocmutex},
{"testpoll", testpoll},
{"testlock", testlock},
{"testthread", testthread},
1.13 +53 -89 apr/test/testprocmutex.c
Index: testprocmutex.c
===================================================================
RCS file: /home/cvs/apr/test/testprocmutex.c,v
retrieving revision 1.12
retrieving revision 1.13
diff -u -u -r1.12 -r1.13
--- testprocmutex.c 1 Jan 2003 00:01:56 -0000 1.12
+++ testprocmutex.c 2 Jul 2003 12:12:30 -0000 1.13
@@ -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 @@
*/
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;
}
Re: cvs commit: apr/test Makefile.in test_apr.h testall.c testprocmutex.c
Posted by Joe Orton <jo...@manyfish.co.uk>.
On Wed, Jul 02, 2003 at 08:59:19AM -0400, Jeff Trawick wrote:
> jorton@apache.org wrote:
> >jorton 2003/07/02 05:12:30
> >
> > Modified: test Makefile.in test_apr.h testall.c testprocmutex.c
> > Log:
> > - dropped support for specifying a lock filename, hopefully that is
> > not critical
>
> At one point, the test could not be run naturally on a machine with home
> dir over NFS.
>
> Maybe if you pass NULL for lockname it would work since APR would create
> it in /tmp (ISTR)?
>
> It looks like the previous code normally passed NULL, which may be a
> change after the lockname option was added which negated the need for
> the lockname to be specified.
Ah, good point. Yes, from looking at the code NULL does indeed look like
the better default choice; the header said a filename should always be
used so I just did that.
> Also
>
> + const char *lockname = "tpm.lock";
> + const char *shmname = "tpm.shm";
>
> Did you intend "tmp.lock" and "tmp.shm"?
No, "tpm" was my little acronym for "test process mutex" :)
Thanks for the review.
Re: cvs commit: apr/test Makefile.in test_apr.h testall.c testprocmutex.c
Posted by Jeff Trawick <tr...@attglobal.net>.
jorton@apache.org wrote:
> jorton 2003/07/02 05:12:30
>
> Modified: test Makefile.in test_apr.h testall.c testprocmutex.c
> Log:
> - dropped support for specifying a lock filename, hopefully that is
> not critical
At one point, the test could not be run naturally on a machine with home
dir over NFS.
Maybe if you pass NULL for lockname it would work since APR would create
it in /tmp (ISTR)?
It looks like the previous code normally passed NULL, which may be a
change after the lockname option was added which negated the need for
the lockname to be specified.
Also
+ const char *lockname = "tpm.lock";
+ const char *shmname = "tpm.shm";
Did you intend "tmp.lock" and "tmp.shm"?