apr-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From r..@apache.org
Subject cvs commit: apr/test Makefile.in Makefile.win test_apr.h testall.c testshm.c testshmconsumer.c testshmproducer.c
Date Sat, 13 Mar 2004 21:41:05 GMT
rbb         2004/03/13 13:41:05

  Modified:    test     Makefile.in Makefile.win test_apr.h testall.c
                        testshm.c testshmconsumer.c testshmproducer.c
  Log:
  Port testshm and all of it's component parts (testshmconsumer and
  testshmproducer) so that they are a part of the unified test suite.  They
  are also now all portable programs, which means that we are actually
  using APR as a part of the tests.
  
  Also, just generally improve the tests.  Now, we not only check that
  msgavail flag is set, we also check that the actuall message is transferred
  correctly.  Finally, we ensure that the receiver received the same
  number of messages that the sender sent.
  
  Revision  Changes    Path
  1.150     +1 -5      apr/test/Makefile.in
  
  Index: Makefile.in
  ===================================================================
  RCS file: /home/cvs/apr/test/Makefile.in,v
  retrieving revision 1.149
  retrieving revision 1.150
  diff -u -r1.149 -r1.150
  --- Makefile.in	13 Mar 2004 12:34:49 -0000	1.149
  +++ Makefile.in	13 Mar 2004 21:41:04 -0000	1.150
  @@ -19,7 +19,6 @@
   	testall@EXEEXT@
   
   STDTEST_NONPORTABLE = \
  -	testshm@EXEEXT@ \
   	testglobalmutex@EXEEXT@
   
   OTHER_PROGRAMS = client@EXEEXT@ sendfile@EXEEXT@ \
  @@ -91,9 +90,6 @@
   sendfile@EXEEXT@: sendfile.lo $(LOCAL_LIBS)
   	$(LINK_PROG) sendfile.lo $(LOCAL_LIBS) $(ALL_LIBS)
   
  -testshm@EXEEXT@: testshm.lo $(LOCAL_LIBS) testshmproducer@EXEEXT@ testshmconsumer@EXEEXT@
  -	$(LINK_PROG) testshm.lo $(LOCAL_LIBS) $(ALL_LIBS)
  -
   testshmproducer@EXEEXT@: testshmproducer.lo $(LOCAL_LIBS)
   	$(LINK_PROG) testshmproducer.lo $(LOCAL_LIBS) $(ALL_LIBS)
   
  @@ -116,7 +112,7 @@
   	testpoll.lo testlock.lo testsockopt.lo testpipe.lo testthread.lo \
   	testhash.lo testargs.lo testnames.lo testuser.lo testpath.lo \
   	testenv.lo testprocmutex.lo testrand2.lo testfnmatch.lo \
  -        testatomic.lo testflock.lo
  +        testatomic.lo testflock.lo testshm.lo
   
   testall: $(TESTS) mod_test.la libmod_test.la occhild@EXEEXT@ \
   	 readchild@EXEEXT@ CuTest.lo proc_child@EXEEXT@ \
  
  
  
  1.15      +1 -2      apr/test/Makefile.win
  
  Index: Makefile.win
  ===================================================================
  RCS file: /home/cvs/apr/test/Makefile.win,v
  retrieving revision 1.14
  retrieving revision 1.15
  diff -u -r1.14 -r1.15
  --- Makefile.win	13 Mar 2004 12:38:16 -0000	1.14
  +++ Makefile.win	13 Mar 2004 21:41:04 -0000	1.15
  @@ -2,7 +2,6 @@
   LINK=link /nologo
   
   NONPORTABLE = \
  -	testshm.exe \
   	testglobalmutex.exe
   
   PROGRAMS = \
  @@ -96,7 +95,7 @@
   	testpoll.obj testlock.obj testsockopt.obj testpipe.obj testthread.obj \
   	testhash.obj testargs.obj testnames.obj testuser.obj testpath.obj \
   	testenv.obj testprocmutex.obj testrand2.obj testfnmatch.obj \
  -        testatomic.obj testflock.obj
  +        testatomic.obj testflock.obj testshm.obj
   
   testall.exe: $(TESTS) CuTest.obj $(LOCAL_LIBS)
   	$(LINK) /debug /subsystem:console /machine:I386 $(TESTS) CuTest.obj \
  
  
  
  1.49      +1 -0      apr/test/test_apr.h
  
  Index: test_apr.h
  ===================================================================
  RCS file: /home/cvs/apr/test/test_apr.h,v
  retrieving revision 1.48
  retrieving revision 1.49
  diff -u -r1.48 -r1.49
  --- test_apr.h	13 Mar 2004 13:02:35 -0000	1.48
  +++ test_apr.h	13 Mar 2004 21:41:04 -0000	1.49
  @@ -53,6 +53,7 @@
   CuSuite *testrand(void);
   CuSuite *testrand2(void);
   CuSuite *testsleep(void);
  +CuSuite *testshm(void);
   CuSuite *testsockopt(void);
   CuSuite *testsockets(void);
   CuSuite *teststr(void);
  
  
  
  1.54      +1 -0      apr/test/testall.c
  
  Index: testall.c
  ===================================================================
  RCS file: /home/cvs/apr/test/testall.c,v
  retrieving revision 1.53
  retrieving revision 1.54
  diff -u -r1.53 -r1.54
  --- testall.c	13 Mar 2004 13:02:35 -0000	1.53
  +++ testall.c	13 Mar 2004 21:41:04 -0000	1.54
  @@ -65,6 +65,7 @@
       {"testrand", testrand},
       {"testrand2", testrand2},
       {"testsleep", testsleep},
  +    {"testshm", testshm},
       {"testsockets", testsockets},
       {"testsockopt", testsockopt},
       {"teststr", teststr},
  
  
  
  1.11      +140 -207  apr/test/testshm.c
  
  Index: testshm.c
  ===================================================================
  RCS file: /home/cvs/apr/test/testshm.c,v
  retrieving revision 1.10
  retrieving revision 1.11
  diff -u -r1.10 -r1.11
  --- testshm.c	13 Feb 2004 09:38:34 -0000	1.10
  +++ testshm.c	13 Mar 2004 21:41:04 -0000	1.11
  @@ -13,281 +13,214 @@
    * limitations under the License.
    */
   
  +#include "test_apr.h"
   #include "apr_shm.h"
   #include "apr_errno.h"
   #include "apr_general.h"
   #include "apr_lib.h"
   #include "apr_strings.h"
  +#include "apr_thread_proc.h"
   #include "apr_time.h"
  -#include <errno.h>
  -#include <stdio.h>
  -#include <stdlib.h>
  -#if APR_HAVE_UNISTD_H
  -#include <unistd.h>
  -#endif
  +#include "testshm.h"
   
   #if APR_HAS_SHARED_MEMORY
   
  -typedef struct mbox {
  -    char msg[1024]; 
  -    int msgavail; 
  -} mbox;
  -mbox *boxes;
  -
  -#define N_BOXES 10
  -#define N_MESSAGES 100
  -#define SHARED_SIZE (apr_size_t)(N_BOXES * sizeof(mbox))
  -#define SHARED_FILENAME "/tmp/apr.testshm.shm"
  -
  -static void msgwait(int sleep_sec, int first_box, int last_box)
  +static int msgwait(int sleep_sec, int first_box, int last_box)
   {
       int i;
  +    int recvd = 0;
       apr_time_t start = apr_time_now();
       apr_interval_time_t sleep_duration = apr_time_from_sec(sleep_sec);
       while (apr_time_now() - start < sleep_duration) {
           for (i = first_box; i < last_box; i++) {
  -            if (boxes[i].msgavail) {
  -                fprintf(stdout, "received a message in box %d, message was: %s\n", 
  -                        i, boxes[i].msg); 
  +            if (boxes[i].msgavail && !strcmp(boxes[i].msg, MSG)) {
  +                recvd++;
                   boxes[i].msgavail = 0; /* reset back to 0 */
  +                /* reset the msg field.  1024 is a magic number and it should
  +                 * be a macro, but I am being lazy.
  +                 */
  +                memset(boxes[i].msg, 0, 1024);
               }
           }
           apr_sleep(apr_time_make(0, 10000)); /* 10ms */
       }
  -    fprintf(stdout, "done waiting on mailboxes...\n");
  +    return recvd;
   }
   
   static void msgput(int boxnum, char *msg)
   {
  -    fprintf(stdout, "Sending message to box %d\n", boxnum);
  -    apr_cpystrn(boxes[boxnum].msg, msg, strlen(msg));
  +    apr_cpystrn(boxes[boxnum].msg, msg, strlen(msg) + 1);
       boxes[boxnum].msgavail = 1;
   }
   
  -static apr_status_t test_anon(apr_pool_t *parpool)
  +static void test_anon_create(CuTest *tc)
   {
       apr_status_t rv;
  -    apr_pool_t *pool;
  -    apr_shm_t *shm;
  -    apr_size_t retsize;
  -    pid_t pid;
  -    int cnt, i, exit_int;
  +    apr_shm_t *shm = NULL;
   
  -    rv = apr_pool_create(&pool, parpool);
  -    if (rv != APR_SUCCESS) {
  -        fprintf(stderr, "Error creating child pool\n");
  -        return rv;
  -    }
  +    rv = apr_shm_create(&shm, SHARED_SIZE, NULL, p);
  +    apr_assert_success(tc, "Error allocating shared memory block", rv);
  +    CuAssertPtrNotNull(tc, shm);
   
  -    printf("Creating anonymous shared memory block (%"
  -           APR_SIZE_T_FMT " bytes)........", SHARED_SIZE); 
  -    rv = apr_shm_create(&shm, SHARED_SIZE, NULL, pool);
  -    if (rv != APR_SUCCESS) { 
  -        fprintf(stderr, "Error allocating shared memory block\n");
  -        return rv;
  -    }
  -    fprintf(stdout, "OK\n");
  +    rv = apr_shm_destroy(shm);
  +    apr_assert_success(tc, "Error destroying shared memory block", rv);
  +}
   
  -    printf("Checking size...%" APR_SIZE_T_FMT " bytes...",
  -            retsize = apr_shm_size_get(shm));
  -    if (retsize != SHARED_SIZE) {
  -        fprintf(stderr, "Error allocating shared memory block\n");
  -        return rv;
  -    }
  -    fprintf(stdout, "OK\n");
  +static void test_check_size(CuTest *tc)
  +{
  +    apr_status_t rv;
  +    apr_shm_t *shm = NULL;
  +    apr_size_t retsize;
   
  -    printf("Allocating shared mbox memory for %d boxes ..............",
  -           N_BOXES); 
  -    boxes = apr_shm_baseaddr_get(shm);
  -    if (boxes == NULL) { 
  -        fprintf(stderr, "Error creating message boxes.\n");
  -        return rv;
  -    }
  -    fprintf(stdout, "OK\n");
  +    rv = apr_shm_create(&shm, SHARED_SIZE, NULL, p);
  +    apr_assert_success(tc, "Error allocating shared memory block", rv);
  +    CuAssertPtrNotNull(tc, shm);
   
  -    printf("Shared Process Test (child/parent)\n");
  -    pid = fork();
  -    if (pid == 0) { /* child */
  -        msgwait(5, 0, N_BOXES);
  -        exit(0);
  -    }
  -    else if (pid > 0) { /* parent */
  -        i = N_BOXES;
  -        cnt = N_MESSAGES;
  -        while (--cnt > 0) {
  -            if ((i-=3) < 0) {
  -                i += N_BOXES; /* start over at the top */
  -            }
  -            msgput(i, "Sending a message\n");
  -            apr_sleep(apr_time_make(0, 10000));
  -        }
  -    }
  -    else {
  -        printf("Error creating a child process\n");
  -        return errno;
  -    }
  -    /* wait for the child */
  -    printf("Waiting for child to exit.\n");
  -    if (waitpid(pid, &exit_int, 0) < 0) {
  -        return errno;
  -    }
  +    retsize = apr_shm_size_get(shm);
  +    CuAssertIntEquals(tc, SHARED_SIZE, retsize);
   
  -    printf("Destroying shared memory segment...");
       rv = apr_shm_destroy(shm);
  -    if (rv != APR_SUCCESS) {
  -        printf("FAILED\n");
  -        return rv;
  -    }
  -    printf("OK\n");
  +    apr_assert_success(tc, "Error destroying shared memory block", rv);
  +}
   
  -    apr_pool_destroy(pool);
  +static void test_shm_allocate(CuTest *tc)
  +{
  +    apr_status_t rv;
  +    apr_shm_t *shm = NULL;
  +    apr_size_t retsize;
  +
  +    rv = apr_shm_create(&shm, SHARED_SIZE, NULL, p);
  +    apr_assert_success(tc, "Error allocating shared memory block", rv);
  +    CuAssertPtrNotNull(tc, shm);
  +
  +    boxes = apr_shm_baseaddr_get(shm);
  +    CuAssertPtrNotNull(tc, boxes);
   
  -    return APR_SUCCESS;
  +    rv = apr_shm_destroy(shm);
  +    apr_assert_success(tc, "Error destroying shared memory block", rv);
   }
   
  -static apr_status_t test_named(apr_pool_t *parpool)
  +static void test_anon(CuTest *tc)
   {
  +    apr_proc_t proc;
       apr_status_t rv;
  -    apr_pool_t *pool;
       apr_shm_t *shm;
       apr_size_t retsize;
  -    pid_t pidproducer, pidconsumer;
  -    int exit_int;
  +    pid_t pid;
  +    int cnt, i, exit_int;
  +    int recvd;
   
  -    rv = apr_pool_create(&pool, parpool);
  -    if (rv != APR_SUCCESS) {
  -        fprintf(stderr, "Error creating child pool\n");
  -        return rv;
  -    }
  +    rv = apr_shm_create(&shm, SHARED_SIZE, NULL, p);
  +    apr_assert_success(tc, "Error allocating shared memory block", rv);
  +    CuAssertPtrNotNull(tc, shm);
   
  -    printf("Creating named shared memory block (%"
  -           APR_SIZE_T_FMT " bytes)........", SHARED_SIZE); 
  -    rv = apr_shm_create(&shm, SHARED_SIZE, SHARED_FILENAME, pool);
  -    if (rv != APR_SUCCESS) { 
  -        fprintf(stderr, "Error allocating shared memory block\n");
  -        return rv;
  -    }
  -    fprintf(stdout, "OK\n");
  +    retsize = apr_shm_size_get(shm);
  +    CuAssertIntEquals(tc, SHARED_SIZE, retsize);
   
  -    printf("Checking size...%" APR_SIZE_T_FMT " bytes...",
  -            retsize = apr_shm_size_get(shm));
  -    if (retsize != SHARED_SIZE) {
  -        fprintf(stderr, "Error allocating shared memory block\n");
  -        return rv;
  -    }
  -    fprintf(stdout, "OK\n");
  -
  -    printf("Allocating shared mbox memory for %d boxes ..............",
  -           N_BOXES); 
       boxes = apr_shm_baseaddr_get(shm);
  -    if (boxes == NULL) { 
  -        fprintf(stderr, "Error creating message boxes.\n");
  -        return rv;
  -    }
  -    fprintf(stdout, "OK\n");
  +    CuAssertPtrNotNull(tc, boxes);
   
  -    printf("fork()ing and exec()ing children:\n");
  -    pidproducer = fork();
  -    if (pidproducer == 0) { /* child */
  -        /* FIXME: exec a producer */
  -        printf("starting consumer.....\n");
  -        if (execlp("testshmconsumer", "testshmconsumer", (char*)0) < 0) {
  -            return errno;
  -        }
  -	return 0; /* not reached - avoid warnings */
  +    rv = apr_proc_fork(&proc, p);
  +    if (rv == APR_INCHILD) { /* child */
  +        int num = msgwait(5, 0, N_BOXES);
  +        /* exit with the number of messages received so that the parent
  +         * can check that all messages were received.
  +         */
  +        exit(num);
       }
  -    else if (pidproducer > 0) { /* parent */
  -        /* fork another child */
  -        pidconsumer = fork();
  -        if (pidconsumer == 0) { /* child */
  -            /* FIXME: exec a producer */
  -            printf("starting producer.....\n");
  -            if (execlp("testshmproducer", "testshmproducer", (char*)0) < 0) {
  -                return errno;
  +    else if (rv == APR_INPARENT) { /* parent */
  +        i = N_BOXES;
  +        cnt = 0;
  +        while (cnt++ < N_MESSAGES) {
  +            if ((i-=3) < 0) {
  +                i += N_BOXES; /* start over at the top */
               }
  -        }
  -        else if (pidconsumer < 0) { /* parent */
  -            printf("Error creating a child process\n");
  -            return errno;
  +            msgput(i, MSG);
  +            apr_sleep(apr_time_make(0, 10000));
           }
       }
       else {
  -        printf("Error creating a child process\n");
  -        return errno;
  +        CuFail(tc, "apr_proc_fork failed");
       }
       /* wait for the child */
  -    printf("Waiting for producer to exit.\n");
  -    if (waitpid(pidconsumer, &exit_int, 0) < 0) {
  -        return errno;
  -    }
  -    if (!WIFEXITED(exit_int)) {
  -        printf("Producer was unsuccessful.\n");
  -        return APR_EGENERAL;
  -    }
  -    printf("Waiting for consumer to exit.\n");
  -    if (waitpid(pidproducer, &exit_int, 0) < 0) {
  -        return errno;
  -    }
  -    if (!WIFEXITED(exit_int)) {
  -        printf("Consumer was unsuccessful.\n");
  -        return APR_EGENERAL;
  -    }
  +    rv = apr_proc_wait(&proc, &recvd, NULL, APR_WAIT);
  +    CuAssertIntEquals(tc, N_MESSAGES, recvd);
   
  -    printf("Destroying shared memory segment...");
       rv = apr_shm_destroy(shm);
  -    if (rv != APR_SUCCESS) {
  -        printf("FAILED\n");
  -        return rv;
  -    }
  -    printf("OK\n");
  -
  -    apr_pool_destroy(pool);
  -
  -    return APR_SUCCESS;
  +    apr_assert_success(tc, "Error destroying shared memory block", rv);
   }
   
  -int main(void)
  +static void test_named(CuTest *tc)
   {
       apr_status_t rv;
  -    apr_pool_t *pool;
  -    char errmsg[200];
  +    apr_shm_t *shm = NULL;
  +    apr_size_t retsize;
  +    apr_proc_t pidproducer, pidconsumer;
  +    apr_procattr_t *attr1 = NULL, *attr2 = NULL;
  +    int sent, received;
  +    apr_exit_why_e why;
  +    const char *args[4];
  +
  +    rv = apr_shm_create(&shm, SHARED_SIZE, SHARED_FILENAME, p);
  +    apr_assert_success(tc, "Error allocating shared memory block", rv);
  +    CuAssertPtrNotNull(tc, shm);
  +
  +    retsize = apr_shm_size_get(shm);
  +    CuAssertIntEquals(tc, SHARED_SIZE, retsize);
   
  -    apr_initialize();
  +    boxes = apr_shm_baseaddr_get(shm);
  +    CuAssertPtrNotNull(tc, boxes);
  +
  +    rv = apr_procattr_create(&attr1, p);
  +    CuAssertPtrNotNull(tc, attr1);
  +    apr_assert_success(tc, "Couldn't create attr1", rv);
  +    args[0] = apr_pstrdup(p, "testshmproducer");
  +    args[1] = NULL;
  +    rv = apr_proc_create(&pidproducer, "./testshmproducer", args, NULL,
  +                         attr1, p);
  +    apr_assert_success(tc, "Couldn't launch producer", rv);
  +
  +    rv = apr_procattr_create(&attr2, p);
  +    CuAssertPtrNotNull(tc, attr2);
  +    apr_assert_success(tc, "Couldn't create attr2", rv);
  +    args[0] = apr_pstrdup(p, "testshmconsumer");
  +    rv = apr_proc_create(&pidconsumer, "./testshmconsumer", args, NULL,
  +                         attr2, p);
  +    apr_assert_success(tc, "Couldn't launch consumer", rv);
  +
  +    rv = apr_proc_wait(&pidconsumer, &received, &why, APR_WAIT);
  +    CuAssertIntEquals(tc, APR_CHILD_DONE, rv);
  +    CuAssertIntEquals(tc, APR_PROC_EXIT, why);
  +
  +    rv = apr_proc_wait(&pidproducer, &sent, &why, APR_WAIT);
  +    CuAssertIntEquals(tc, APR_CHILD_DONE, rv);
  +    CuAssertIntEquals(tc, APR_PROC_EXIT, why);
  +
  +    /* Cleanup before testing that producer and consumer worked correctly.
  +     * This way, if they didn't succeed, we can just run this test again
  +     * without having to cleanup manually.
  +     */
  +    apr_assert_success(tc, "Error destroying shared memory", 
  +                       apr_shm_destroy(shm));
       
  -    printf("APR Shared Memory Test\n");
  -    printf("======================\n\n");
  +    CuAssertIntEquals(tc, sent, received);
   
  -    printf("Initializing the pool............................"); 
  -    if (apr_pool_create(&pool, NULL) != APR_SUCCESS) {
  -        printf("could not initialize pool\n");
  -        exit(-1);
  -    }
  -    printf("OK\n");
  +}
  +#endif
   
  -    rv = test_anon(pool);
  -    if (rv != APR_SUCCESS) {
  -        if (rv == APR_ENOTIMPL) {
  -            printf("Anonymous shared memory unavailable on this platform.\n");
  -        }
  -        else {
  -            printf("Anonymous shared memory test FAILED: [%d] %s\n",
  -                   rv, apr_strerror(rv, errmsg, sizeof(errmsg)));
  -            exit(-2);
  -        }
  -    }
  -    printf("Anonymous shared memory test passed!\n");
  +CuSuite *testshm(void)
  +{
  +    CuSuite *suite = CuSuiteNew("Shared Memory");
   
  -    if ((rv = test_named(pool)) != APR_SUCCESS) {
  -        printf("Name-based shared memory test FAILED: [%d] %s \n",
  -               rv, apr_strerror(rv, errmsg, sizeof(errmsg)));
  -        exit(-3);
  -    }
  -    printf("Named shared memory test passed!\n");
  +#if APR_HAS_SHARED_MEMORY
  +    SUITE_ADD_TEST(suite, test_anon_create);
  +    SUITE_ADD_TEST(suite, test_check_size);
  +    SUITE_ADD_TEST(suite, test_shm_allocate);
  +    SUITE_ADD_TEST(suite, test_anon);
  +    SUITE_ADD_TEST(suite, test_named);
  +#endif
   
  -    return 0;
  +    return suite;
   }
   
  -#else /* APR_HAS_SHARED_MEMORY */
  -#error shmem is not supported on this platform
  -#endif /* APR_HAS_SHARED_MEMORY */
   
  
  
  
  1.8       +14 -41    apr/test/testshmconsumer.c
  
  Index: testshmconsumer.c
  ===================================================================
  RCS file: /home/cvs/apr/test/testshmconsumer.c,v
  retrieving revision 1.7
  retrieving revision 1.8
  diff -u -r1.7 -r1.8
  --- testshmconsumer.c	13 Feb 2004 09:38:34 -0000	1.7
  +++ testshmconsumer.c	13 Mar 2004 21:41:04 -0000	1.8
  @@ -19,41 +19,27 @@
   #include "apr_lib.h"
   #include "apr_strings.h"
   #include "apr_time.h"
  -#include <errno.h>
  -#include <stdio.h>
  -#include <stdlib.h>
  -#if APR_HAVE_UNISTD_H
  -#include <unistd.h>
  -#endif
  +#include "testshm.h"
   
   #if APR_HAS_SHARED_MEMORY
   
  -typedef struct mbox {
  -    char msg[1024]; 
  -    int msgavail; 
  -} mbox;
  -mbox *boxes;
  -
  -#define N_BOXES 10
  -#define SHARED_SIZE (apr_size_t)(N_BOXES * sizeof(mbox))
  -#define SHARED_FILENAME "/tmp/apr.testshm.shm"
  -
  -static void msgwait(int sleep_sec, int first_box, int last_box)
  +static int msgwait(int sleep_sec, int first_box, int last_box)
   {
       int i;
  +    int recvd = 0;
       apr_time_t start = apr_time_now();
       apr_interval_time_t sleep_duration = apr_time_from_sec(sleep_sec);
       while (apr_time_now() - start < sleep_duration) {
           for (i = first_box; i < last_box; i++) {
  -            if (boxes[i].msgavail) {
  -                fprintf(stdout, "Consumer: received a message in box %d, message was: %s\n",

  -                        i, boxes[i].msg); 
  +            if (boxes[i].msgavail && !strcmp(boxes[i].msg, MSG)) {
  +                recvd++;
                   boxes[i].msgavail = 0; /* reset back to 0 */
  +                memset(boxes[i].msg, 0, 1024);
               }
           }
           apr_sleep(apr_time_from_sec(1));
       }
  -    fprintf(stdout, "Consumer: done waiting on mailboxes...\n");
  +    return recvd;
   }
   
   int main(void)
  @@ -62,53 +48,40 @@
       apr_pool_t *pool;
       apr_shm_t *shm;
       char errmsg[200];
  +    int recvd;
   
       apr_initialize();
       
  -    printf("APR Shared Memory Test: CONSUMER\n");
  -
  -    printf("Initializing the pool............................"); 
       if (apr_pool_create(&pool, NULL) != APR_SUCCESS) {
  -        printf("could not initialize pool\n");
           exit(-1);
       }
  -    printf("OK\n");
   
  -    printf("Consumer attaching to name-based shared memory....");
       rv = apr_shm_attach(&shm, SHARED_FILENAME, pool);
       if (rv != APR_SUCCESS) {
  -        printf("Consumer unable to attach to name-based shared memory "
  -               "segment: [%d] %s \n", rv,
  -               apr_strerror(rv, errmsg, sizeof(errmsg)));
           exit(-2);
       }
  -    printf("OK\n");
   
       boxes = apr_shm_baseaddr_get(shm);
   
       /* consume messages on all of the boxes */
  -    msgwait(30, 0, N_BOXES); /* wait for 30 seconds for messages */
  +    recvd = msgwait(30, 0, N_BOXES); /* wait for 30 seconds for messages */
   
  -    printf("Consumer detaching from name-based shared memory....");
       rv = apr_shm_detach(shm);
       if (rv != APR_SUCCESS) {
  -        printf("Consumer unable to detach from name-based shared memory "
  -               "segment: [%d] %s \n", rv,
  -               apr_strerror(rv, errmsg, sizeof(errmsg)));
           exit(-3);
       }
  -    printf("OK\n");
   
  -    return 0;
  +    return recvd;
   }
   
   #else /* APR_HAS_SHARED_MEMORY */
   
   int main(void)
   {
  -    printf("APR SHMEM test not run!\n");
  -    printf("shmem is not supported on this platform\n"); 
  -    return -1;
  +    /* Just return, this program will never be called, so we don't need
  +     * to print a message 
  +     */
  +    return 0;
   }
   
   #endif /* APR_HAS_SHARED_MEMORY */
  
  
  
  1.8       +16 -41    apr/test/testshmproducer.c
  
  Index: testshmproducer.c
  ===================================================================
  RCS file: /home/cvs/apr/test/testshmproducer.c,v
  retrieving revision 1.7
  retrieving revision 1.8
  diff -u -r1.7 -r1.8
  --- testshmproducer.c	13 Feb 2004 09:38:34 -0000	1.7
  +++ testshmproducer.c	13 Mar 2004 21:41:04 -0000	1.8
  @@ -19,29 +19,12 @@
   #include "apr_lib.h"
   #include "apr_strings.h"
   #include "apr_time.h"
  -#include <errno.h>
  -#include <stdio.h>
  -#include <stdlib.h>
  -#if APR_HAVE_UNISTD_H
  -#include <unistd.h>
  -#endif
  +#include "testshm.h"
   
   #if APR_HAS_SHARED_MEMORY
  -
  -typedef struct mbox {
  -    char msg[1024]; 
  -    int msgavail; 
  -} mbox;
  -mbox *boxes;
  -
  -#define N_BOXES 10
  -#define SHARED_SIZE (apr_size_t)(N_BOXES * sizeof(mbox))
  -#define SHARED_FILENAME "/tmp/apr.testshm.shm"
  -
   static void msgput(int boxnum, char *msg)
   {
  -    fprintf(stdout, "Producer: Sending message to box %d\n", boxnum);
  -    apr_cpystrn(boxes[boxnum].msg, msg, strlen(msg));
  +    apr_cpystrn(boxes[boxnum].msg, msg, strlen(msg) + 1);
       boxes[boxnum].msgavail = 1;
   }
   
  @@ -51,57 +34,49 @@
       apr_pool_t *pool;
       apr_shm_t *shm;
       int i;
  +    int sent = 0;
       char errmsg[200];
   
       apr_initialize();
       
  -    printf("APR Shared Memory Test: PRODUCER\n");
  -
  -    printf("Initializing the pool............................"); 
       if (apr_pool_create(&pool, NULL) != APR_SUCCESS) {
  -        printf("could not initialize pool\n");
           exit(-1);
       }
  -    printf("OK\n");
   
  -    printf("Producer attaching to name-based shared memory....");
       rv = apr_shm_attach(&shm, SHARED_FILENAME, pool);
       if (rv != APR_SUCCESS) {
  -        printf("Producer unable to attach to name-based shared memory "
  -               "segment: [%d] %s \n", rv,
  -               apr_strerror(rv, errmsg, sizeof(errmsg)));
           exit(-2);
       }
  -    printf("OK\n");
   
       boxes = apr_shm_baseaddr_get(shm);
   
  -    /* produce messages on all of the boxes, in descending order */
  -    for (i = N_BOXES - 1; i > 0; i--) {
  -        msgput(i, "Sending a message\n");
  +    /* produce messages on all of the boxes, in descending order,
  +     * Yes, we could just return N_BOXES, but I want to have a double-check
  +     * in this code.  The original code actually sent N_BOXES - 1 messages,
  +     * so rather than rely on possibly buggy code, this way we know that we
  +     * are returning the right number.
  +     */
  +    for (i = N_BOXES - 1, sent = 0; i >= 0; i--, sent++) {
  +        msgput(i, MSG);
           apr_sleep(apr_time_from_sec(1));
       }
   
  -    printf("Producer detaching from name-based shared memory....");
       rv = apr_shm_detach(shm);
       if (rv != APR_SUCCESS) {
  -        printf("Producer unable to detach from name-based shared memory "
  -               "segment: [%d] %s \n", rv,
  -               apr_strerror(rv, errmsg, sizeof(errmsg)));
           exit(-3);
       }
  -    printf("OK\n");
   
  -    return 0;
  +    return sent;
   }
   
   #else /* APR_HAS_SHARED_MEMORY */
   
   int main(void)
   {
  -    printf("APR SHMEM test not run!\n");
  -    printf("shmem is not supported on this platform\n"); 
  -    return -1;
  +    /* Just return, this program will never be launched, so there is no
  +     * reason to print a message.
  +     */
  +    return 0;
   }
   
   #endif /* APR_HAS_SHARED_MEMORY */
  
  
  

Mime
View raw message