summaryrefslogtreecommitdiff
path: root/test/testprocmutex.c
diff options
context:
space:
mode:
authorJoe Orton <jorton@apache.org>2003-07-02 12:12:30 +0000
committerJoe Orton <jorton@apache.org>2003-07-02 12:12:30 +0000
commit32640ac41096183f777d27de0587ff013ed0f0c3 (patch)
tree5e4f5d49f645f97a248da3943f6d3c610d0ded98 /test/testprocmutex.c
parentbd15d249e1104baea97c0f37ae49a29ccffc83b9 (diff)
downloadapr-32640ac41096183f777d27de0587ff013ed0f0c3.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: https://svn.apache.org/repos/asf/apr/apr/trunk@64555 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'test/testprocmutex.c')
-rw-r--r--test/testprocmutex.c142
1 files changed, 53 insertions, 89 deletions
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;
}