1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* ***** BEGIN LICENSE BLOCK *****
3 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
5 * The contents of this file are subject to the Mozilla Public License Version
6 * 1.1 (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 * http://www.mozilla.org/MPL/
10 * Software distributed under the License is distributed on an "AS IS" basis,
11 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 * for the specific language governing rights and limitations under the
15 * The Original Code is the Netscape Portable Runtime (NSPR).
17 * The Initial Developer of the Original Code is
18 * Netscape Communications Corporation.
19 * Portions created by the Initial Developer are Copyright (C) 1999-2000
20 * the Initial Developer. All Rights Reserved.
24 * Alternatively, the contents of this file may be used under the terms of
25 * either the GNU General Public License Version 2 or later (the "GPL"), or
26 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
27 * in which case the provisions of the GPL or the LGPL are applicable instead
28 * of those above. If you wish to allow use of your version of this file only
29 * under the terms of either the GPL or the LGPL, and not to allow others to
30 * use your version of this file under the terms of the MPL, indicate your
31 * decision by deleting the provisions above and replace them with the notice
32 * and other provisions required by the GPL or the LGPL. If you do not delete
33 * the provisions above, a recipient may use your version of this file under
34 * the terms of any one of the MPL, the GPL or the LGPL.
36 * ***** END LICENSE BLOCK ***** */
42 * This test has two threads communicating with each other using
43 * two unidirectional pipes. The primordial thread is the ping
44 * thread and the other thread is the pong thread. The ping
45 * thread writes "ping" to the pong thread and the pong thread
57 #define NUM_ITERATIONS 10
59 static PRFileDesc
*ping_in
, *ping_out
;
60 static PRFileDesc
*pong_in
, *pong_out
;
62 static void PongThreadFunc(void *arg
)
69 for (idx
= 0; idx
< NUM_ITERATIONS
; idx
++) {
70 memset(buf
, 0, sizeof(buf
));
71 nBytes
= PR_Read(pong_in
, buf
, sizeof(buf
));
73 fprintf(stderr
, "PR_Read failed\n");
76 printf("pong thread: received \"%s\"\n", buf
);
78 fprintf(stderr
, "pong thread: expected 5 bytes but got %d bytes\n",
82 if (strcmp(buf
, "ping") != 0) {
83 fprintf(stderr
, "pong thread: expected \"ping\" but got \"%s\"\n",
88 printf("pong thread: sending \"%s\"\n", buf
);
89 nBytes
= PR_Write(pong_out
, buf
, 5);
91 fprintf(stderr
, "PR_Write failed: (%d, %d)\n", PR_GetError(),
97 status
= PR_Close(pong_in
);
98 if (status
== PR_FAILURE
) {
99 fprintf(stderr
, "PR_Close failed\n");
102 status
= PR_Close(pong_out
);
103 if (status
== PR_FAILURE
) {
104 fprintf(stderr
, "PR_Close failed\n");
112 PRThread
*pongThread
;
117 status
= PR_CreatePipe(&ping_in
, &pong_out
);
118 if (status
== PR_FAILURE
) {
119 fprintf(stderr
, "PR_CreatePipe failed\n");
122 status
= PR_CreatePipe(&pong_in
, &ping_out
);
123 if (status
== PR_FAILURE
) {
124 fprintf(stderr
, "PR_CreatePipe failed\n");
128 pongThread
= PR_CreateThread(PR_USER_THREAD
, PongThreadFunc
, NULL
,
129 PR_PRIORITY_NORMAL
, PR_GLOBAL_THREAD
, PR_JOINABLE_THREAD
, 0);
130 if (pongThread
== NULL
) {
131 fprintf(stderr
, "PR_CreateThread failed\n");
135 for (idx
= 0; idx
< NUM_ITERATIONS
; idx
++) {
137 printf("ping thread: sending \"%s\"\n", buf
);
138 nBytes
= PR_Write(ping_out
, buf
, 5);
140 fprintf(stderr
, "PR_Write failed: (%d, %d)\n", PR_GetError(),
144 memset(buf
, 0, sizeof(buf
));
145 nBytes
= PR_Read(ping_in
, buf
, sizeof(buf
));
147 fprintf(stderr
, "PR_Read failed\n");
150 printf("ping thread: received \"%s\"\n", buf
);
152 fprintf(stderr
, "ping thread: expected 5 bytes but got %d bytes\n",
156 if (strcmp(buf
, "pong") != 0) {
157 fprintf(stderr
, "ping thread: expected \"pong\" but got \"%s\"\n",
163 status
= PR_Close(ping_in
);
164 if (status
== PR_FAILURE
) {
165 fprintf(stderr
, "PR_Close failed\n");
168 status
= PR_Close(ping_out
);
169 if (status
== PR_FAILURE
) {
170 fprintf(stderr
, "PR_Close failed\n");
173 status
= PR_JoinThread(pongThread
);
174 if (status
== PR_FAILURE
) {
175 fprintf(stderr
, "PR_JoinThread failed\n");
181 * Test PR_Available for pipes
183 status
= PR_CreatePipe(&ping_in
, &ping_out
);
184 if (status
== PR_FAILURE
) {
185 fprintf(stderr
, "PR_CreatePipe failed\n");
188 nBytes
= PR_Write(ping_out
, buf
, 250);
190 fprintf(stderr
, "PR_Write failed: (%d, %d)\n", PR_GetError(),
194 nBytes
= PR_Available(ping_in
);
196 fprintf(stderr
, "PR_Available failed: (%d, %d)\n", PR_GetError(),
199 } else if (nBytes
!= 250) {
200 fprintf(stderr
, "PR_Available: expected 250 bytes but got %d bytes\n",
204 printf("PR_Available: expected %d, got %d bytes\n",250, nBytes
);
206 nBytes
= PR_Read(ping_in
, buf
, 7);
208 fprintf(stderr
, "PR_Read failed\n");
211 /* check available data */
212 nBytes
= PR_Available(ping_in
);
214 fprintf(stderr
, "PR_Available failed: (%d, %d)\n", PR_GetError(),
217 } else if (nBytes
!= (250 - 7)) {
218 fprintf(stderr
, "PR_Available: expected 243 bytes but got %d bytes\n",
222 printf("PR_Available: expected %d, got %d bytes\n",243, nBytes
);
224 nBytes
= PR_Read(ping_in
, buf
, sizeof(buf
));
226 fprintf(stderr
, "PR_Read failed\n");
228 } else if (nBytes
!= 243) {
229 fprintf(stderr
, "PR_Read failed: expected %d, got %d bytes\n",
233 /* check available data */
234 nBytes
= PR_Available(ping_in
);
236 fprintf(stderr
, "PR_Available failed: (%d, %d)\n", PR_GetError(),
239 } else if (nBytes
!= 0) {
240 fprintf(stderr
, "PR_Available: expected 0 bytes but got %d bytes\n",
244 printf("PR_Available: expected %d, got %d bytes\n", 0, nBytes
);
246 status
= PR_Close(ping_in
);
247 if (status
== PR_FAILURE
) {
248 fprintf(stderr
, "PR_Close failed\n");
251 status
= PR_Close(ping_out
);
252 if (status
== PR_FAILURE
) {
253 fprintf(stderr
, "PR_Close failed\n");