Home | History | Annotate | Download | only in mq_timedsend
      1 /*
      2  * Copyright (c) 2003, Intel Corporation. All rights reserved.
      3  * Created by:  julie.n.fleischer REMOVE-THIS AT intel DOT com
      4  * This file is licensed under the GPL license.  For the full content
      5  * of this license, see the COPYING file at the top level of this
      6  * source tree.
      7  */
      8 
      9 /*
     10  * Test that mq_timedsend() inserts messages into the message queue according
     11  * to the priority given.
     12  * Test by inserting multiple messages into the queue not in priority order.
     13  * Ensure messages are received in priority order.
     14  *
     15  * 3/13/03 - Added fix from Gregoire Pichon for specifying an attr
     16  *           with a mq_maxmsg >= BUFFER.
     17  *
     18  */
     19 
     20 #include <stdio.h>
     21 #include <mqueue.h>
     22 #include <fcntl.h>
     23 #include <sys/stat.h>
     24 #include <sys/types.h>
     25 #include <unistd.h>
     26 #include <string.h>
     27 #include <time.h>
     28 #include "posixtest.h"
     29 
     30 #define NAMESIZE 50
     31 #define MSG1 "1234567890"
     32 #define PRI1 10
     33 #define MSG2 "234567890"
     34 #define PRI2 8
     35 #define MSG3 "34567890"
     36 #define PRI3 6
     37 #define MSG4 "4567890"
     38 #define PRI4 2
     39 #define MSG5 "567890"
     40 #define PRI5 1
     41 #define BUFFER 40
     42 #define MAXMSG 10
     43 
     44 int main(void)
     45 {
     46 	char qname[NAMESIZE], msgrcd[BUFFER];
     47 	const char *msgptr1 = MSG1;
     48 	const char *msgptr2 = MSG2;
     49 	const char *msgptr3 = MSG3;
     50 	const char *msgptr4 = MSG4;
     51 	const char *msgptr5 = MSG5;
     52 	struct timespec ts;
     53 	struct mq_attr attr;
     54 	mqd_t queue;
     55 	int unresolved = 0, failure = 0;
     56 	unsigned pri;
     57 
     58 	sprintf(qname, "/mq_timedsend_3-1_%d", getpid());
     59 
     60 	attr.mq_msgsize = BUFFER;
     61 	attr.mq_maxmsg = MAXMSG;
     62 	queue = mq_open(qname, O_CREAT | O_RDWR, S_IRUSR | S_IWUSR, &attr);
     63 	if (queue == (mqd_t) - 1) {
     64 		perror("mq_open() did not return success");
     65 		return PTS_UNRESOLVED;
     66 	}
     67 
     68 	ts.tv_sec = time(NULL) + 1;
     69 	ts.tv_nsec = 0;
     70 	if (mq_timedsend(queue, msgptr3, strlen(msgptr3), PRI3, &ts) != 0) {
     71 		perror("mq_timedsend() did not return success");
     72 		failure = 1;
     73 	}
     74 
     75 	ts.tv_sec++;
     76 	if (mq_timedsend(queue, msgptr1, strlen(msgptr1), PRI1, &ts) != 0) {
     77 		perror("mq_timedsend() did not return success");
     78 		failure = 1;
     79 	}
     80 
     81 	ts.tv_sec++;
     82 	if (mq_timedsend(queue, msgptr4, strlen(msgptr4), PRI4, &ts) != 0) {
     83 		perror("mq_timedsend() did not return success");
     84 		failure = 1;
     85 	}
     86 
     87 	ts.tv_sec++;
     88 	if (mq_timedsend(queue, msgptr2, strlen(msgptr2), PRI2, &ts) != 0) {
     89 		perror("mq_timedsend() did not return success");
     90 		failure = 1;
     91 	}
     92 
     93 	ts.tv_sec++;
     94 	if (mq_timedsend(queue, msgptr5, strlen(msgptr5), PRI5, &ts) != 0) {
     95 		perror("mq_timedsend() did not return success");
     96 		failure = 1;
     97 	}
     98 
     99 	if (mq_receive(queue, msgrcd, BUFFER, &pri) == -1) {
    100 		perror("mq_receive() returned failure");
    101 		failure = 1;
    102 	}
    103 
    104 	if (strncmp(msgptr1, msgrcd, strlen(msgptr1)) != 0) {
    105 		printf("FAIL:  expected %s received %s\n", msgptr1, msgrcd);
    106 		failure = 1;
    107 	}
    108 
    109 	if (mq_receive(queue, msgrcd, BUFFER, &pri) == -1) {
    110 		perror("mq_receive() returned failure");
    111 		failure = 1;
    112 	}
    113 
    114 	if (strncmp(msgptr2, msgrcd, strlen(msgptr2)) != 0) {
    115 		printf("FAIL:  expected %s received %s\n", msgptr2, msgrcd);
    116 		failure = 1;
    117 	}
    118 
    119 	if (mq_receive(queue, msgrcd, BUFFER, &pri) == -1) {
    120 		perror("mq_receive() returned failure");
    121 		failure = 1;
    122 	}
    123 
    124 	if (strncmp(msgptr3, msgrcd, strlen(msgptr3)) != 0) {
    125 		printf("FAIL:  expected %s received %s\n", msgptr3, msgrcd);
    126 		failure = 1;
    127 	}
    128 
    129 	if (mq_receive(queue, msgrcd, BUFFER, &pri) == -1) {
    130 		perror("mq_receive() returned failure");
    131 		failure = 1;
    132 	}
    133 
    134 	if (strncmp(msgptr4, msgrcd, strlen(msgptr4)) != 0) {
    135 		printf("FAIL:  expected %s received %s\n", msgptr4, msgrcd);
    136 		failure = 1;
    137 	}
    138 
    139 	if (mq_receive(queue, msgrcd, BUFFER, &pri) == -1) {
    140 		perror("mq_receive() returned failure");
    141 		failure = 1;
    142 	}
    143 
    144 	if (strncmp(msgptr5, msgrcd, strlen(msgptr5)) != 0) {
    145 		printf("FAIL:  expected %s received %s\n", msgptr5, msgrcd);
    146 		failure = 1;
    147 	}
    148 
    149 	if (mq_close(queue) != 0) {
    150 		perror("mq_close() did not return success");
    151 		unresolved = 1;
    152 	}
    153 
    154 	if (mq_unlink(qname) != 0) {
    155 		perror("mq_unlink() did not return success");
    156 		unresolved = 1;
    157 	}
    158 
    159 	if (failure == 1) {
    160 		printf("Test FAILED\n");
    161 		return PTS_FAIL;
    162 	}
    163 
    164 	if (unresolved == 1) {
    165 		printf("Test UNRESOLVED\n");
    166 		return PTS_UNRESOLVED;
    167 	}
    168 
    169 	printf("Test PASSED\n");
    170 	return PTS_PASS;
    171 }
    172