summaryrefslogtreecommitdiff
path: root/test/testshmconsumer.c
diff options
context:
space:
mode:
authorRyan Bloom <rbb@apache.org>2004-03-13 21:41:04 +0000
committerRyan Bloom <rbb@apache.org>2004-03-13 21:41:04 +0000
commit96a0495b2ab05d495131480fc707b7a712e256f5 (patch)
tree477481cb124661b725e042f404c76e710643ba9b /test/testshmconsumer.c
parent3ab70629c78c854fb495eb6014bd75a31a8273a9 (diff)
downloadapr-96a0495b2ab05d495131480fc707b7a712e256f5.tar.gz
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. git-svn-id: https://svn.apache.org/repos/asf/apr/apr/trunk@64982 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'test/testshmconsumer.c')
-rw-r--r--test/testshmconsumer.c55
1 files changed, 14 insertions, 41 deletions
diff --git a/test/testshmconsumer.c b/test/testshmconsumer.c
index 3aaa5f2ea..0fb78c762 100644
--- a/test/testshmconsumer.c
+++ b/test/testshmconsumer.c
@@ -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 @@ int main(void)
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 */