nsprpub/pr/tests/pipeself.c

Fri, 16 Jan 2015 18:13:44 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Fri, 16 Jan 2015 18:13:44 +0100
branch
TOR_BUG_9701
changeset 14
925c144e1f1f
permissions
-rw-r--r--

Integrate suggestion from review to improve consistency with existing code.

michael@0 1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
michael@0 2 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 3 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 5
michael@0 6 /*
michael@0 7 * File: pipeself.c
michael@0 8 *
michael@0 9 * Description:
michael@0 10 * This test has two threads communicating with each other using
michael@0 11 * two unidirectional pipes. The primordial thread is the ping
michael@0 12 * thread and the other thread is the pong thread. The ping
michael@0 13 * thread writes "ping" to the pong thread and the pong thread
michael@0 14 * writes "pong" back.
michael@0 15 */
michael@0 16
michael@0 17 #include "prio.h"
michael@0 18 #include "prerror.h"
michael@0 19 #include "prthread.h"
michael@0 20
michael@0 21 #include <stdio.h>
michael@0 22 #include <stdlib.h>
michael@0 23 #include <string.h>
michael@0 24
michael@0 25 #define NUM_ITERATIONS 10
michael@0 26
michael@0 27 static PRFileDesc *ping_in, *ping_out;
michael@0 28 static PRFileDesc *pong_in, *pong_out;
michael@0 29
michael@0 30 static void PongThreadFunc(void *arg)
michael@0 31 {
michael@0 32 char buf[1024];
michael@0 33 int idx;
michael@0 34 PRInt32 nBytes;
michael@0 35 PRStatus status;
michael@0 36
michael@0 37 for (idx = 0; idx < NUM_ITERATIONS; idx++) {
michael@0 38 memset(buf, 0, sizeof(buf));
michael@0 39 nBytes = PR_Read(pong_in, buf, sizeof(buf));
michael@0 40 if (nBytes == -1) {
michael@0 41 fprintf(stderr, "PR_Read failed\n");
michael@0 42 exit(1);
michael@0 43 }
michael@0 44 printf("pong thread: received \"%s\"\n", buf);
michael@0 45 if (nBytes != 5) {
michael@0 46 fprintf(stderr, "pong thread: expected 5 bytes but got %d bytes\n",
michael@0 47 nBytes);
michael@0 48 exit(1);
michael@0 49 }
michael@0 50 if (strcmp(buf, "ping") != 0) {
michael@0 51 fprintf(stderr, "pong thread: expected \"ping\" but got \"%s\"\n",
michael@0 52 buf);
michael@0 53 exit(1);
michael@0 54 }
michael@0 55 strcpy(buf, "pong");
michael@0 56 printf("pong thread: sending \"%s\"\n", buf);
michael@0 57 nBytes = PR_Write(pong_out, buf, 5);
michael@0 58 if (nBytes == -1) {
michael@0 59 fprintf(stderr, "PR_Write failed: (%d, %d)\n", PR_GetError(),
michael@0 60 PR_GetOSError());
michael@0 61 exit(1);
michael@0 62 }
michael@0 63 }
michael@0 64
michael@0 65 status = PR_Close(pong_in);
michael@0 66 if (status == PR_FAILURE) {
michael@0 67 fprintf(stderr, "PR_Close failed\n");
michael@0 68 exit(1);
michael@0 69 }
michael@0 70 status = PR_Close(pong_out);
michael@0 71 if (status == PR_FAILURE) {
michael@0 72 fprintf(stderr, "PR_Close failed\n");
michael@0 73 exit(1);
michael@0 74 }
michael@0 75 }
michael@0 76
michael@0 77 int main(int argc, char **argv)
michael@0 78 {
michael@0 79 PRStatus status;
michael@0 80 PRThread *pongThread;
michael@0 81 char buf[1024];
michael@0 82 PRInt32 nBytes;
michael@0 83 int idx;
michael@0 84
michael@0 85 status = PR_CreatePipe(&ping_in, &pong_out);
michael@0 86 if (status == PR_FAILURE) {
michael@0 87 fprintf(stderr, "PR_CreatePipe failed\n");
michael@0 88 exit(1);
michael@0 89 }
michael@0 90 status = PR_CreatePipe(&pong_in, &ping_out);
michael@0 91 if (status == PR_FAILURE) {
michael@0 92 fprintf(stderr, "PR_CreatePipe failed\n");
michael@0 93 exit(1);
michael@0 94 }
michael@0 95
michael@0 96 pongThread = PR_CreateThread(PR_USER_THREAD, PongThreadFunc, NULL,
michael@0 97 PR_PRIORITY_NORMAL, PR_GLOBAL_THREAD, PR_JOINABLE_THREAD, 0);
michael@0 98 if (pongThread == NULL) {
michael@0 99 fprintf(stderr, "PR_CreateThread failed\n");
michael@0 100 exit(1);
michael@0 101 }
michael@0 102
michael@0 103 for (idx = 0; idx < NUM_ITERATIONS; idx++) {
michael@0 104 strcpy(buf, "ping");
michael@0 105 printf("ping thread: sending \"%s\"\n", buf);
michael@0 106 nBytes = PR_Write(ping_out, buf, 5);
michael@0 107 if (nBytes == -1) {
michael@0 108 fprintf(stderr, "PR_Write failed: (%d, %d)\n", PR_GetError(),
michael@0 109 PR_GetOSError());
michael@0 110 exit(1);
michael@0 111 }
michael@0 112 memset(buf, 0, sizeof(buf));
michael@0 113 nBytes = PR_Read(ping_in, buf, sizeof(buf));
michael@0 114 if (nBytes == -1) {
michael@0 115 fprintf(stderr, "PR_Read failed\n");
michael@0 116 exit(1);
michael@0 117 }
michael@0 118 printf("ping thread: received \"%s\"\n", buf);
michael@0 119 if (nBytes != 5) {
michael@0 120 fprintf(stderr, "ping thread: expected 5 bytes but got %d bytes\n",
michael@0 121 nBytes);
michael@0 122 exit(1);
michael@0 123 }
michael@0 124 if (strcmp(buf, "pong") != 0) {
michael@0 125 fprintf(stderr, "ping thread: expected \"pong\" but got \"%s\"\n",
michael@0 126 buf);
michael@0 127 exit(1);
michael@0 128 }
michael@0 129 }
michael@0 130
michael@0 131 status = PR_Close(ping_in);
michael@0 132 if (status == PR_FAILURE) {
michael@0 133 fprintf(stderr, "PR_Close failed\n");
michael@0 134 exit(1);
michael@0 135 }
michael@0 136 status = PR_Close(ping_out);
michael@0 137 if (status == PR_FAILURE) {
michael@0 138 fprintf(stderr, "PR_Close failed\n");
michael@0 139 exit(1);
michael@0 140 }
michael@0 141 status = PR_JoinThread(pongThread);
michael@0 142 if (status == PR_FAILURE) {
michael@0 143 fprintf(stderr, "PR_JoinThread failed\n");
michael@0 144 exit(1);
michael@0 145 }
michael@0 146
michael@0 147 #if defined(XP_UNIX) && !defined(SYMBIAN)
michael@0 148 /*
michael@0 149 * Test PR_Available for pipes
michael@0 150 */
michael@0 151 status = PR_CreatePipe(&ping_in, &ping_out);
michael@0 152 if (status == PR_FAILURE) {
michael@0 153 fprintf(stderr, "PR_CreatePipe failed\n");
michael@0 154 exit(1);
michael@0 155 }
michael@0 156 nBytes = PR_Write(ping_out, buf, 250);
michael@0 157 if (nBytes == -1) {
michael@0 158 fprintf(stderr, "PR_Write failed: (%d, %d)\n", PR_GetError(),
michael@0 159 PR_GetOSError());
michael@0 160 exit(1);
michael@0 161 }
michael@0 162 nBytes = PR_Available(ping_in);
michael@0 163 if (nBytes < 0) {
michael@0 164 fprintf(stderr, "PR_Available failed: (%d, %d)\n", PR_GetError(),
michael@0 165 PR_GetOSError());
michael@0 166 exit(1);
michael@0 167 } else if (nBytes != 250) {
michael@0 168 fprintf(stderr, "PR_Available: expected 250 bytes but got %d bytes\n",
michael@0 169 nBytes);
michael@0 170 exit(1);
michael@0 171 }
michael@0 172 printf("PR_Available: expected %d, got %d bytes\n",250, nBytes);
michael@0 173 /* read some data */
michael@0 174 nBytes = PR_Read(ping_in, buf, 7);
michael@0 175 if (nBytes == -1) {
michael@0 176 fprintf(stderr, "PR_Read failed\n");
michael@0 177 exit(1);
michael@0 178 }
michael@0 179 /* check available data */
michael@0 180 nBytes = PR_Available(ping_in);
michael@0 181 if (nBytes < 0) {
michael@0 182 fprintf(stderr, "PR_Available failed: (%d, %d)\n", PR_GetError(),
michael@0 183 PR_GetOSError());
michael@0 184 exit(1);
michael@0 185 } else if (nBytes != (250 - 7)) {
michael@0 186 fprintf(stderr, "PR_Available: expected 243 bytes but got %d bytes\n",
michael@0 187 nBytes);
michael@0 188 exit(1);
michael@0 189 }
michael@0 190 printf("PR_Available: expected %d, got %d bytes\n",243, nBytes);
michael@0 191 /* read all data */
michael@0 192 nBytes = PR_Read(ping_in, buf, sizeof(buf));
michael@0 193 if (nBytes == -1) {
michael@0 194 fprintf(stderr, "PR_Read failed\n");
michael@0 195 exit(1);
michael@0 196 } else if (nBytes != 243) {
michael@0 197 fprintf(stderr, "PR_Read failed: expected %d, got %d bytes\n",
michael@0 198 243, nBytes);
michael@0 199 exit(1);
michael@0 200 }
michael@0 201 /* check available data */
michael@0 202 nBytes = PR_Available(ping_in);
michael@0 203 if (nBytes < 0) {
michael@0 204 fprintf(stderr, "PR_Available failed: (%d, %d)\n", PR_GetError(),
michael@0 205 PR_GetOSError());
michael@0 206 exit(1);
michael@0 207 } else if (nBytes != 0) {
michael@0 208 fprintf(stderr, "PR_Available: expected 0 bytes but got %d bytes\n",
michael@0 209 nBytes);
michael@0 210 exit(1);
michael@0 211 }
michael@0 212 printf("PR_Available: expected %d, got %d bytes\n", 0, nBytes);
michael@0 213
michael@0 214 status = PR_Close(ping_in);
michael@0 215 if (status == PR_FAILURE) {
michael@0 216 fprintf(stderr, "PR_Close failed\n");
michael@0 217 exit(1);
michael@0 218 }
michael@0 219 status = PR_Close(ping_out);
michael@0 220 if (status == PR_FAILURE) {
michael@0 221 fprintf(stderr, "PR_Close failed\n");
michael@0 222 exit(1);
michael@0 223 }
michael@0 224 #endif /* XP_UNIX */
michael@0 225
michael@0 226 printf("PASS\n");
michael@0 227 return 0;
michael@0 228 }

mercurial