| Commit | Line | Data |
|---|---|---|
| 2aa45b0f | 1 | /* |
| 2 | * Copyright (c) 2009, Stathis Kamperis | |
| 3 | * All rights reserved. | |
| 4 | * | |
| 5 | * Redistribution and use in source and binary forms, with or without | |
| 6 | * modification, are permitted provided that the following conditions | |
| 7 | * are met: | |
| 8 | * 1. Redistributions of source code must retain the above copyright | |
| 9 | * notice, this list of conditions and the following disclaimer. | |
| 10 | * 2. Redistributions in binary form must reproduce the above copyright | |
| 11 | * notice, this list of conditions and the following disclaimer in the | |
| 12 | * documentation and/or other materials provided with the distribution. | |
| 13 | * | |
| 14 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | |
| 15 | * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | |
| 16 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS | |
| 17 | * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE | |
| 18 | * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, | |
| 19 | * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING, | |
| 20 | * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; | |
| 21 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED | |
| 22 | * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, | |
| 23 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT | |
| 24 | * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | |
| 25 | * SUCH DAMAGE. | |
| 26 | */ | |
| 27 | ||
| 83311dec | 28 | #define _XOPEN_SOURCE 600 |
| 29 | ||
| 30 | #include <assert.h> | |
| 31 | #include <errno.h> | |
| 32 | #include <mqueue.h> | |
| 33 | #include <stdio.h> | |
| 34 | #include <stdlib.h> | |
| 35 | #include <string.h> /* memset() */ | |
| 36 | #include <time.h> | |
| 37 | #include <sys/time.h> | |
| 38 | ||
| 39 | #define MQNAME "/t_mq_timedsend" | |
| 40 | ||
| 41 | int main(void) | |
| 42 | { | |
| 2aa45b0f | 43 | /* |
| 83311dec | 44 | * We need a full message queue for mq_timedsend() to block on it and |
| 45 | * time out. For this purpose, we start with a queue of capacity equal | |
| 46 | * to one `1', that can be easily filled in. | |
| 2aa45b0f | 47 | */ |
| 83311dec | 48 | struct mq_attr attr; |
| 49 | ||
| 2aa45b0f | 50 | memset(&attr, 0, sizeof(attr)); |
| 51 | attr.mq_maxmsg = 1; /* Maximum number of messages. */ | |
| 52 | attr.mq_msgsize = 1024; /* Maximum message size. */ | |
| 83311dec | 53 | |
| 54 | /* Create the queue. */ | |
| 55 | mqd_t md; | |
| 56 | ||
| 2aa45b0f | 57 | md = mq_open(MQNAME, O_CREAT | O_EXCL | O_WRONLY, 0700, &attr); |
| 58 | assert(md != (mqd_t)-1); | |
| 83311dec | 59 | |
| 2aa45b0f | 60 | /* |
| 83311dec | 61 | * Send a message to exhaust the capacity of the queue. Since noone is |
| 62 | * receiveing the message, the queue will remain filled, which is what | |
| 63 | * we want. | |
| 64 | */ | |
| 2aa45b0f | 65 | assert(mq_send(md, "foo", sizeof("foo"), /* priority */ 0) != -1); |
| 83311dec | 66 | |
| 2aa45b0f | 67 | /* Timeout in 3 seconds, if the queue doesn't become empty (it wont). */ |
| 83311dec | 68 | struct timespec now, timeout; |
| 69 | ||
| 70 | assert(clock_gettime(CLOCK_REALTIME, &now) == 0); | |
| 2aa45b0f | 71 | timeout.tv_sec = now.tv_sec + 3; |
| 72 | timeout.tv_nsec = now.tv_nsec; | |
| 83311dec | 73 | |
| 74 | /* | |
| 75 | * Since we don't trust mq_timedsend(), we are going to manually measure | |
| 76 | * the time ellapsed until the function call times out. | |
| 77 | */ | |
| 78 | struct timeval tv1, tv2; | |
| 79 | ||
| 80 | assert(gettimeofday(&tv1, NULL) == 0); | |
| 81 | ||
| 82 | /* This should timeout in ~3 seconds. */ | |
| 2aa45b0f | 83 | assert(mq_timedsend(md, "foo", sizeof("foo"), /* priority */ 0, |
| 84 | &timeout) == -1 && errno == ETIMEDOUT); | |
| 83311dec | 85 | |
| 86 | /* Mark the end. */ | |
| 87 | assert(gettimeofday(&tv2, NULL) == 0); | |
| 88 | ||
| 21d4cdad | 89 | /* Make sure the time ellapsed is roughly 3sec. */ |
| 90 | long int msec; | |
| 91 | ||
| 92 | msec = (tv2.tv_sec - tv1.tv_sec) * 1000 + | |
| 93 | (tv2.tv_usec - tv1.tv_usec) / 1000; | |
| 94 | assert(msec >= 2900 && msec <= 3100); | |
| 83311dec | 95 | |
| 96 | /* Cleanup. */ | |
| 97 | assert(mq_close(md) != -1); | |
| 98 | assert(mq_unlink(MQNAME) != -1); | |
| 99 | ||
| 62df3fe2 SK |
100 | printf("passed\n"); |
| 101 | ||
| 83311dec | 102 | return (EXIT_SUCCESS); |
| 103 | } |