From 2a929796b961708c84cc64b7ca72cf8abae2f8c0 Mon Sep 17 00:00:00 2001 From: patacongo Date: Thu, 29 Mar 2007 23:44:55 +0000 Subject: [PATCH] Add timed mqueue test. git-svn-id: svn://svn.code.sf.net/p/nuttx/code/trunk@179 42af7a65-404d-4744-a932-0658087f49c3 --- ChangeLog | 3 + Documentation/NuttX.html | 3 + examples/ostest/Makefile | 5 + examples/ostest/main.c | 10 +- examples/ostest/ostest.h | 6 +- examples/ostest/timedmqueue.c | 384 ++++++++++++++++++++++++++++++++++ 6 files changed, 409 insertions(+), 2 deletions(-) create mode 100644 examples/ostest/timedmqueue.c diff --git a/ChangeLog b/ChangeLog index 9e21a92cde..002a40b9d6 100644 --- a/ChangeLog +++ b/ChangeLog @@ -104,5 +104,8 @@ higher level, the printf output is more readable. * Fixed a bug in file system cleanup: A list was being deleted before the buffers contained in the list. + * Fixed a bug in the wait-for-message-queue-not-empty logic. + * Added a test of timed mqueue operations; detected and corrected + some mqueue errors. * Started m68322 diff --git a/Documentation/NuttX.html b/Documentation/NuttX.html index 50172c06d5..3df09d3c41 100644 --- a/Documentation/NuttX.html +++ b/Documentation/NuttX.html @@ -465,6 +465,9 @@ Other memory: higher level, the printf output is more readable. * Fixed a bug in file system cleanup: A list was being deleted before the buffers contained in the list. + * Fixed a bug in the wait-for-message-queue-not-empty logic. + * Added a test of timed mqueue operations; detected and corrected + some mqueue errors. * Started m68322 diff --git a/examples/ostest/Makefile b/examples/ostest/Makefile index 9c8f0d6d7c..161ddafe2e 100644 --- a/examples/ostest/Makefile +++ b/examples/ostest/Makefile @@ -47,12 +47,17 @@ endif ifneq ($(CONFIG_DISABLE_SIGNALS),y) CSRCS += sighand.c ifneq ($(CONFIG_DISABLE_PTHREAD),y) +ifneq ($(CONFIG_DISABLE_CLOCK),y) CSRCS += timedwait.c endif endif +endif ifneq ($(CONFIG_DISABLE_MQUEUE),y) ifneq ($(CONFIG_DISABLE_PTHREAD),y) CSRCS += mqueue.c +ifneq ($(CONFIG_DISABLE_CLOCK),y) +CSRCS += timedmqueue.c +endif endif endif ifneq ($(CONFIG_DISABLE_POSIX_TIMERS),y) diff --git a/examples/ostest/main.c b/examples/ostest/main.c index c06ab86dfc..30dd8c9264 100644 --- a/examples/ostest/main.c +++ b/examples/ostest/main.c @@ -237,7 +237,7 @@ static int user_main(int argc, char *argv[]) check_test_memory_usage(); #endif -#if !defined(CONFIG_DISABLE_SIGNALS) && !defined(CONFIG_DISABLE_PTHREAD) +#if !defined(CONFIG_DISABLE_SIGNALS) && !defined(CONFIG_DISABLE_PTHREAD) && !defined(CONFIG_DISABLE_CLOCK) /* Verify pthreads and condition variable timed waits */ printf("\nuser_main: timed wait test\n"); @@ -253,6 +253,14 @@ static int user_main(int argc, char *argv[]) check_test_memory_usage(); #endif +#if !defined(CONFIG_DISABLE_MQUEUE) && !defined(CONFIG_DISABLE_PTHREAD) && !defined(CONFIG_DISABLE_CLOCK) + /* Verify pthreads and message queues */ + + printf("\nuser_main: timed message queue test\n"); + timedmqueue_test(); + check_test_memory_usage(); +#endif + #ifndef CONFIG_DISABLE_SIGNALS /* Verify signal handlers */ diff --git a/examples/ostest/ostest.h b/examples/ostest/ostest.h index b76297ebed..dd1fbf00e4 100644 --- a/examples/ostest/ostest.h +++ b/examples/ostest/ostest.h @@ -76,10 +76,14 @@ extern void sem_test(void); extern void cond_test(void); -/* queue.c **************************************************/ +/* mqueue.c *************************************************/ extern void mqueue_test(void); +/* timedmqueue.c ********************************************/ + +extern void timedmqueue_test(void); + /* cancel.c *************************************************/ extern void cancel_test(void); diff --git a/examples/ostest/timedmqueue.c b/examples/ostest/timedmqueue.c new file mode 100644 index 0000000000..186296a06a --- /dev/null +++ b/examples/ostest/timedmqueue.c @@ -0,0 +1,384 @@ +/************************************************************************** + * mqueue.c + * + * Copyright (C) 2007 Gregory Nutt. All rights reserved. + * Author: Gregory Nutt + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * 3. Neither the name Gregory Nutt nor the names of its contributors may be + * used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, + * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS + * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED + * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN + * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + * + **************************************************************************/ + +/************************************************************************** + * Included Files + **************************************************************************/ + +#include + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "ostest.h" + +/************************************************************************** + * Private Definitions + **************************************************************************/ + +#define TEST_MESSAGE "This is a test and only a test" + +#ifdef SDCC +#define TEST_MSGLEN (31) +#else +#define TEST_MSGLEN (strlen(TEST_MESSAGE)+1) +#endif + +#define TEST_SEND_NMSGS (10) +#define TEST_RECEIVE_NMSGS (10) + +/************************************************************************** + * Private Types + **************************************************************************/ + +/************************************************************************** + * Private Function Prototypes + **************************************************************************/ + +/************************************************************************** + * Global Variables + **************************************************************************/ + +/************************************************************************** + * Private Variables + **************************************************************************/ + +/************************************************************************** + * Private Functions + **************************************************************************/ + +/************************************************************************** + * Public Functions + **************************************************************************/ + +static void *sender_thread(void *arg) +{ + mqd_t mqfd; + char msg_buffer[TEST_MSGLEN]; + struct mq_attr attr; + int status = 0; + int nerrors = 0; + int i; + + printf("sender_thread: Starting\n"); + + /* Fill in attributes for message queue */ + + attr.mq_maxmsg = TEST_SEND_NMSGS-1; + attr.mq_msgsize = TEST_MSGLEN; + attr.mq_flags = 0; + + /* Set the flags for the open of the queue. + * Make it a blocking open on the queue, meaning it will block if + * this process tries to send to the queue and the queue is full. + * + * O_CREAT - the queue will get created if it does not already exist. + * O_WRONLY - we are only planning to write to the queue. + * + * Open the queue, and create it if the receiving process hasn't + * already created it. + */ + + mqfd = mq_open("testmq", O_WRONLY|O_CREAT, 0666, &attr); + if (mqfd < 0) + { + printf("sender_thread: ERROR mq_open failed\n"); + pthread_exit((pthread_addr_t)1); + } + + /* Fill in a test message buffer to send */ + + memcpy(msg_buffer, TEST_MESSAGE, TEST_MSGLEN); + + /* Perform the send TEST_SEND_NMSGS times */ + + for (i = 0; i < TEST_SEND_NMSGS; i++) + { + struct timespec time; + status = clock_gettime(CLOCK_REALTIME, &time); + if (status != 0) + { + printf("sender_thread: ERROR clock_gettime failed\n"); + } + time.tv_sec += 5; + + /* The first TEST_SEND_NMSGS-1 send should succeed. The last + * one should fail with errno == ETIMEDOUT + */ + + status = mq_timedsend(mqfd, msg_buffer, TEST_MSGLEN, 42, &time); + if (status < 0) + { + if (i == TEST_SEND_NMSGS-1 && *get_errno_ptr() == ETIMEDOUT) + { + printf("sender_thread: mq_timedsend %d timed out as expected\n", i); + } + else + { + printf("sender_thread: ERROR mq_timedsend failure=%d on msg %d\n", *get_errno_ptr(), i); + nerrors++; + } + } + else + { + if (i == TEST_SEND_NMSGS-1) + { + printf("sender_thread: ERROR mq_timedsend of msg %d succeeded\n", i); + nerrors++; + } + else + { + printf("sender_thread: mq_timedsend succeeded on msg %d\n", i); + } + } + } + + /* Close the queue and return success */ + + if (mq_close(mqfd) < 0) + { + printf("sender_thread: ERROR mq_close failed\n"); + } + + printf("sender_thread: returning nerrors=%d\n", nerrors); + fflush(stdout); + return (pthread_addr_t)nerrors; +} + +static void *receiver_thread(void *arg) +{ + mqd_t mqfd; + char msg_buffer[TEST_MSGLEN]; + struct mq_attr attr; + int nbytes; + int nerrors = 0; + int i; + + printf("receiver_thread: Starting\n"); + + /* Fill in attributes for message queue */ + + attr.mq_maxmsg = TEST_SEND_NMSGS-1; + attr.mq_msgsize = TEST_MSGLEN; + attr.mq_flags = 0; + + /* Set the flags for the open of the queue. + * Make it a blocking open on the queue, meaning it will block if + * this process tries to* send to the queue and the queue is full. + * + * O_CREAT - the queue will get created if it does not already exist. + * O_RDONLY - we are only planning to write to the queue. + * + * Open the queue, and create it if the sending process hasn't + * already created it. + */ + + mqfd = mq_open("testmq", O_RDONLY|O_CREAT, 0666, &attr); + if (mqfd < 0) + { + printf("receiver_thread: ERROR mq_open failed\n"); + pthread_exit((pthread_addr_t)1); + } + + /* Perform the receive TEST_RECEIVE_NMSGS times */ + + for (i = 0; i < TEST_RECEIVE_NMSGS; i++) + { + struct timespec time; + int status = clock_gettime(CLOCK_REALTIME, &time); + if (status != 0) + { + printf("sender_thread: ERROR clock_gettime failed\n"); + } + time.tv_sec += 5; + + /* The first TEST_SEND_NMSGS-1 send should succeed. The last + * one should fail with errno == ETIMEDOUT + */ + + memset(msg_buffer, 0xaa, TEST_MSGLEN); + nbytes = mq_timedreceive(mqfd, msg_buffer, TEST_MSGLEN, 0, &time); + if (nbytes < 0) + { + if (i == TEST_SEND_NMSGS-1 && *get_errno_ptr() == ETIMEDOUT) + { + printf("receiver_thread: Receive %d timed out as expected\n", i); + } + else + { + printf("receiver_thread: ERROR mq_timedreceive failure=%d on msg %d\n", *get_errno_ptr(), i); + nerrors++; + } + } + else if (nbytes != TEST_MSGLEN) + { + printf("receiver_thread: mq_timedreceive return bad size %d on msg %d\n", nbytes, i); + nerrors++; + } + else if (memcmp(TEST_MESSAGE, msg_buffer, nbytes) != 0) + { + int j; + + printf("receiver_thread: mq_timedreceive returned corrupt message on msg %d\n", i); + printf("receiver_thread: i Expected Received\n"); + + for (j = 0; j < TEST_MSGLEN-1; j++) + { + if (isprint(msg_buffer[j])) + { + printf("receiver_thread: %2d %02x (%c) %02x (%c)\n", + j, TEST_MESSAGE[j], TEST_MESSAGE[j], msg_buffer[j], msg_buffer[j]); + } + else + { + printf("receiver_thread: %2d %02x (%c) %02x\n", + j, TEST_MESSAGE[j], TEST_MESSAGE[j], msg_buffer[j]); + } + } + printf("receiver_thread: %2d 00 %02x\n", + j, msg_buffer[j]); + } + else if (i == TEST_SEND_NMSGS-1) + { + printf("receiver_thread: ERROR mq_timedreceive of msg %d succeeded\n", i); + nerrors++; + } + else + { + printf("receiver_thread: mq_timedreceive succeeded on msg %d\n", i); + } + } + + /* Close the queue and return success */ + + if (mq_close(mqfd) < 0) + { + printf("receiver_thread: ERROR mq_close failed\n"); + nerrors++; + } + + /* Destroy the queue */ + + if (mq_unlink("testmq") < 0) + { + printf("receiver_thread: ERROR mq_close failed\n"); + nerrors++; + } + + printf("receiver_thread: returning nerrors=%d\n", nerrors); + fflush(stdout); + pthread_exit((pthread_addr_t)nerrors); + return (pthread_addr_t)nerrors; +} + +void timedmqueue_test(void) +{ + pthread_t sender; + pthread_t receiver; + void *result; + pthread_attr_t attr; + int status; + + /* Start the sending thread at the default priority */ + + printf("timedmqueue_test: Starting sender\n"); + status = pthread_attr_init(&attr); + if (status != 0) + { + printf("timedmqueue_test: pthread_attr_init failed, status=%d\n", status); + } + + status = pthread_attr_setstacksize(&attr, 16384); + if (status != 0) + { + printf("timedmqueue_test: pthread_attr_setstacksize failed, status=%d\n", status); + } + + status = pthread_create(&sender, &attr, sender_thread, NULL); + if (status != 0) + { + printf("timedmqueue_test: pthread_create failed, status=%d\n", status); + } + + /* Wait for the sending thread to complete */ + + printf("timedmqueue_test: Waiting for sender to complete\n"); + pthread_join(sender, &result); + if (result != (void*)0) + { + printf("timedmqueue_test: ERROR sender thread exited with %d errors\n", (int)result); + } + + /* Start the receiving thread at the default priority */ + + printf("timedmqueue_test: Starting receiver\n"); + status = pthread_attr_init(&attr); + if (status != 0) + { + printf("timedmqueue_test: pthread_attr_init failed, status=%d\n", status); + } + + status = pthread_attr_setstacksize(&attr, 16384); + if (status != 0) + { + printf("timedmqueue_test: pthread_attr_setstacksize failed, status=%d\n", status); + } + + status = pthread_create(&receiver, &attr, receiver_thread, NULL); + if (status != 0) + { + printf("timedmqueue_test: pthread_create failed, status=%d\n", status); + } + + /* Wait for the receiving thread to complete */ + + printf("timedmqueue_test: Waiting for sender to complete\n"); + pthread_join(receiver, &result); + if (result != (void*)0) + { + printf("timedmqueue_test: ERROR receiver thread exited with %d errors\n", (int)result); + } + + printf("timedmqueue_test: Test complete\n"); +} + +