Bug 1852754: part 9) Add tests for dynamically loading <link rel="prefetch"> elements...
[gecko.git] / nsprpub / pr / tests / nonblock.c
blobf24ffb02d07e2dd1bcff61d89f71654c9612ec13
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 #include "nspr.h"
7 #include "prio.h"
8 #include "prerror.h"
9 #include "prlog.h"
10 #include "prprf.h"
11 #include "prnetdb.h"
12 #include "plerror.h"
13 #include "obsolete/probslet.h"
15 #include <stdio.h>
16 #include <string.h>
17 #include <stdlib.h>
19 #define NUMBER_ROUNDS 5
21 #if defined(WIN16)
23 ** Make win16 unit_time interval 300 milliseconds, others get 100
25 #define UNIT_TIME 200 /* unit time in milliseconds */
26 #else
27 #define UNIT_TIME 100 /* unit time in milliseconds */
28 #endif
29 #define CHUNK_SIZE 10
30 #undef USE_PR_SELECT /* If defined, we use PR_Select.
31 * If not defined, use PR_Poll instead. */
33 #if defined(USE_PR_SELECT)
34 #include "pprio.h"
35 #endif
37 static void PR_CALLBACK
38 clientThreadFunc(void *arg)
40 PRUintn port = (PRUintn)arg;
41 PRFileDesc *sock;
42 PRNetAddr addr;
43 char buf[CHUNK_SIZE];
44 int i;
45 PRIntervalTime unitTime = PR_MillisecondsToInterval(UNIT_TIME);
46 PRSocketOptionData optval;
47 PRStatus retVal;
48 PRInt32 nBytes;
50 /* Initialize the buffer so that Purify won't complain */
51 memset(buf, 0, sizeof(buf));
53 addr.inet.family = PR_AF_INET;
54 addr.inet.port = PR_htons((PRUint16)port);
55 addr.inet.ip = PR_htonl(PR_INADDR_LOOPBACK);
56 PR_snprintf(buf, sizeof(buf), "%hu", addr.inet.ip);
58 /* time 1 */
59 PR_Sleep(unitTime);
60 sock = PR_NewTCPSocket();
61 optval.option = PR_SockOpt_Nonblocking;
62 optval.value.non_blocking = PR_TRUE;
63 PR_SetSocketOption(sock, &optval);
64 retVal = PR_Connect(sock, &addr, PR_INTERVAL_NO_TIMEOUT);
65 if (retVal == PR_FAILURE && PR_GetError() == PR_IN_PROGRESS_ERROR) {
66 #if !defined(USE_PR_SELECT)
67 PRPollDesc pd;
68 PRInt32 n;
69 fprintf(stderr, "connect: EWOULDBLOCK, good\n");
70 pd.fd = sock;
71 pd.in_flags = PR_POLL_WRITE;
72 n = PR_Poll(&pd, 1, PR_INTERVAL_NO_TIMEOUT);
73 PR_ASSERT(n == 1);
74 PR_ASSERT(pd.out_flags == PR_POLL_WRITE);
75 #else
76 PR_fd_set writeSet;
77 PRInt32 n;
78 fprintf(stderr, "connect: EWOULDBLOCK, good\n");
79 PR_FD_ZERO(&writeSet);
80 PR_FD_SET(sock, &writeSet);
81 n = PR_Select(0, NULL, &writeSet, NULL, PR_INTERVAL_NO_TIMEOUT);
82 PR_ASSERT(n == 1);
83 PR_ASSERT(PR_FD_ISSET(sock, &writeSet));
84 #endif
86 printf("client connected\n");
87 fflush(stdout);
89 /* time 4, 7, 11, etc. */
90 for (i = 0; i < NUMBER_ROUNDS; i++) {
91 PR_Sleep(3 * unitTime);
92 nBytes = PR_Write(sock, buf, sizeof(buf));
93 if (nBytes == -1) {
94 if (PR_GetError() == PR_WOULD_BLOCK_ERROR) {
95 fprintf(stderr, "write: EWOULDBLOCK\n");
96 exit(1);
97 } else {
98 fprintf(stderr, "write: failed\n");
101 printf("client sent %d bytes\n", nBytes);
102 fflush(stdout);
105 PR_Close(sock);
108 static PRIntn PR_CALLBACK RealMain( PRIntn argc, char **argv )
110 PRFileDesc *listenSock, *sock;
111 PRUint16 listenPort;
112 PRNetAddr addr;
113 char buf[CHUNK_SIZE];
114 PRThread *clientThread;
115 PRInt32 retVal;
116 PRSocketOptionData optval;
117 PRIntn i;
118 PRIntervalTime unitTime = PR_MillisecondsToInterval(UNIT_TIME);
120 /* Create a listening socket */
121 if ((listenSock = PR_NewTCPSocket()) == NULL) {
122 fprintf(stderr, "Can't create a new TCP socket\n");
123 exit(1);
125 addr.inet.family = PR_AF_INET;
126 addr.inet.ip = PR_htonl(PR_INADDR_ANY);
127 addr.inet.port = PR_htons(0);
128 if (PR_Bind(listenSock, &addr) == PR_FAILURE) {
129 fprintf(stderr, "Can't bind socket\n");
130 exit(1);
132 if (PR_GetSockName(listenSock, &addr) == PR_FAILURE) {
133 fprintf(stderr, "PR_GetSockName failed\n");
134 exit(1);
136 listenPort = PR_ntohs(addr.inet.port);
137 if (PR_Listen(listenSock, 5) == PR_FAILURE) {
138 fprintf(stderr, "Can't listen on a socket\n");
139 exit(1);
142 PR_snprintf(buf, sizeof(buf),
143 "The server thread is listening on port %hu\n\n",
144 listenPort);
145 printf("%s", buf);
147 clientThread = PR_CreateThread(PR_USER_THREAD,
148 clientThreadFunc, (void *) listenPort,
149 PR_PRIORITY_NORMAL, PR_LOCAL_THREAD,
150 PR_UNJOINABLE_THREAD, 0);
151 if (clientThread == NULL) {
152 fprintf(stderr, "can't create thread\n");
153 exit(1);
156 printf("client thread created.\n");
158 optval.option = PR_SockOpt_Nonblocking;
159 optval.value.non_blocking = PR_TRUE;
160 PR_SetSocketOption(listenSock, &optval);
161 /* time 0 */
162 sock = PR_Accept(listenSock, NULL, PR_INTERVAL_NO_TIMEOUT);
163 if (sock != NULL || PR_GetError() != PR_WOULD_BLOCK_ERROR) {
164 PL_PrintError("First Accept\n");
165 fprintf(stderr, "First PR_Accept() xxx\n" );
166 exit(1);
168 printf("accept: EWOULDBLOCK, good\n");
169 fflush(stdout);
170 /* time 2 */
171 PR_Sleep(2 * unitTime);
172 sock = PR_Accept(listenSock, NULL, PR_INTERVAL_NO_TIMEOUT);
173 if (sock == NULL) {
174 PL_PrintError("Second Accept\n");
175 fprintf(stderr, "Second PR_Accept() failed: (%d, %d)\n",
176 PR_GetError(), PR_GetOSError());
177 exit(1);
179 printf("accept: succeeded, good\n");
180 fflush(stdout);
181 PR_Close(listenSock);
183 PR_SetSocketOption(sock, &optval);
185 /* time 3, 5, 6, 8, etc. */
186 for (i = 0; i < NUMBER_ROUNDS; i++) {
187 PR_Sleep(unitTime);
188 retVal = PR_Recv(sock, buf, sizeof(buf), 0, PR_INTERVAL_NO_TIMEOUT);
189 if (retVal != -1 || PR_GetError() != PR_WOULD_BLOCK_ERROR) {
190 PL_PrintError("First Receive:\n");
191 fprintf(stderr, "First PR_Recv: retVal: %ld, Error: %ld\n",
192 retVal, PR_GetError());
193 exit(1);
195 printf("read: EWOULDBLOCK, good\n");
196 fflush(stdout);
197 PR_Sleep(2 * unitTime);
198 retVal = PR_Recv(sock, buf, sizeof(buf), 0, PR_INTERVAL_NO_TIMEOUT);
199 if (retVal != CHUNK_SIZE) {
200 PL_PrintError("Second Receive:\n");
201 fprintf(stderr, "Second PR_Recv: retVal: %ld, Error: %ld\n",
202 retVal, PR_GetError());
203 exit(1);
205 printf("read: %d bytes, good\n", retVal);
206 fflush(stdout);
208 PR_Close(sock);
210 printf("All tests finished\n");
211 printf("PASS\n");
212 return 0;
215 int main(int argc, char **argv)
217 PRIntn rv;
219 PR_STDIO_INIT();
220 rv = PR_Initialize(RealMain, argc, argv, 0);
221 return rv;
222 } /* main */