|
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/. */ |
|
5 |
|
6 /* |
|
7 ** name io_timeoutk.c |
|
8 ** Description:Test socket IO timeouts (kernel level) |
|
9 ** |
|
10 ** Modification History: |
|
11 ** 19-May-97 AGarcia- Converted the test to accomodate the debug_mode flag. |
|
12 ** The debug mode will print all of the printfs associated with this test. |
|
13 ** The regress mode will be the default mode. Since the regress tool limits |
|
14 ** the output to a one line status:PASS or FAIL,all of the printf statements |
|
15 ** have been handled with an if (debug_mode) statement. |
|
16 ** 04-June-97 AGarcia removed the Test_Result function. Regress tool has been updated to |
|
17 ** recognize the return code from tha main program. |
|
18 ***********************************************************************/ |
|
19 /*********************************************************************** |
|
20 ** Includes |
|
21 ***********************************************************************/ |
|
22 /* Used to get the command line option */ |
|
23 #include "plgetopt.h" |
|
24 |
|
25 #include <stdio.h> |
|
26 #include "nspr.h" |
|
27 |
|
28 #define NUM_THREADS 1 |
|
29 #define BASE_PORT 8000 |
|
30 #define DEFAULT_ACCEPT_TIMEOUT 2 |
|
31 |
|
32 typedef struct threadInfo { |
|
33 PRInt16 id; |
|
34 PRInt16 accept_timeout; |
|
35 PRLock *dead_lock; |
|
36 PRCondVar *dead_cv; |
|
37 PRInt32 *alive; |
|
38 } threadInfo; |
|
39 |
|
40 PRIntn failed_already=0; |
|
41 PRIntn debug_mode; |
|
42 |
|
43 |
|
44 void |
|
45 thread_main(void *_info) |
|
46 { |
|
47 threadInfo *info = (threadInfo *)_info; |
|
48 PRNetAddr listenAddr; |
|
49 PRNetAddr clientAddr; |
|
50 PRFileDesc *listenSock = NULL; |
|
51 PRFileDesc *clientSock; |
|
52 PRStatus rv; |
|
53 |
|
54 if (debug_mode) printf("thread %d is alive\n", info->id); |
|
55 |
|
56 listenSock = PR_NewTCPSocket(); |
|
57 if (!listenSock) { |
|
58 if (debug_mode) printf("unable to create listen socket\n"); |
|
59 goto dead; |
|
60 } |
|
61 |
|
62 listenAddr.inet.family = AF_INET; |
|
63 listenAddr.inet.port = PR_htons(BASE_PORT + info->id); |
|
64 listenAddr.inet.ip = PR_htonl(INADDR_ANY); |
|
65 rv = PR_Bind(listenSock, &listenAddr); |
|
66 if (rv == PR_FAILURE) { |
|
67 if (debug_mode) printf("unable to bind\n"); |
|
68 goto dead; |
|
69 } |
|
70 |
|
71 rv = PR_Listen(listenSock, 4); |
|
72 if (rv == PR_FAILURE) { |
|
73 if (debug_mode) printf("unable to listen\n"); |
|
74 goto dead; |
|
75 } |
|
76 |
|
77 if (debug_mode) printf("thread %d going into accept for %d seconds\n", |
|
78 info->id, info->accept_timeout + info->id); |
|
79 |
|
80 clientSock = PR_Accept(listenSock, &clientAddr, PR_SecondsToInterval(info->accept_timeout +info->id)); |
|
81 |
|
82 if (clientSock == NULL) { |
|
83 if (PR_GetError() == PR_IO_TIMEOUT_ERROR) |
|
84 if (debug_mode) { |
|
85 printf("PR_Accept() timeout worked!\n"); |
|
86 printf("TEST FAILED! PR_Accept() returned error %d\n", |
|
87 PR_GetError()); |
|
88 } |
|
89 else failed_already=1; |
|
90 } else { |
|
91 if (debug_mode) printf ("TEST FAILED! PR_Accept() succeeded?\n"); |
|
92 else failed_already=1; |
|
93 PR_Close(clientSock); |
|
94 } |
|
95 |
|
96 dead: |
|
97 if (listenSock) { |
|
98 PR_Close(listenSock); |
|
99 } |
|
100 PR_Lock(info->dead_lock); |
|
101 (*info->alive)--; |
|
102 PR_NotifyCondVar(info->dead_cv); |
|
103 PR_Unlock(info->dead_lock); |
|
104 |
|
105 if (debug_mode) printf("thread %d is dead\n", info->id); |
|
106 } |
|
107 |
|
108 void |
|
109 thread_test(PRInt32 scope, PRInt32 num_threads) |
|
110 { |
|
111 PRInt32 index; |
|
112 PRThread *thr; |
|
113 PRLock *dead_lock; |
|
114 PRCondVar *dead_cv; |
|
115 PRInt32 alive; |
|
116 |
|
117 if (debug_mode) printf("IO Timeout test started with %d threads\n", num_threads); |
|
118 |
|
119 dead_lock = PR_NewLock(); |
|
120 dead_cv = PR_NewCondVar(dead_lock); |
|
121 alive = num_threads; |
|
122 |
|
123 for (index = 0; index < num_threads; index++) { |
|
124 threadInfo *info = (threadInfo *)malloc(sizeof(threadInfo)); |
|
125 |
|
126 info->id = index; |
|
127 info->dead_lock = dead_lock; |
|
128 info->dead_cv = dead_cv; |
|
129 info->alive = &alive; |
|
130 info->accept_timeout = DEFAULT_ACCEPT_TIMEOUT; |
|
131 |
|
132 thr = PR_CreateThread( PR_USER_THREAD, |
|
133 thread_main, |
|
134 (void *)info, |
|
135 PR_PRIORITY_NORMAL, |
|
136 scope, |
|
137 PR_UNJOINABLE_THREAD, |
|
138 0); |
|
139 |
|
140 if (!thr) { |
|
141 PR_Lock(dead_lock); |
|
142 alive--; |
|
143 PR_Unlock(dead_lock); |
|
144 } |
|
145 } |
|
146 |
|
147 PR_Lock(dead_lock); |
|
148 while(alive) { |
|
149 if (debug_mode) printf("main loop awake; alive = %d\n", alive); |
|
150 PR_WaitCondVar(dead_cv, PR_INTERVAL_NO_TIMEOUT); |
|
151 } |
|
152 PR_Unlock(dead_lock); |
|
153 } |
|
154 |
|
155 int main(int argc, char **argv) |
|
156 { |
|
157 PRInt32 num_threads; |
|
158 |
|
159 /* The command line argument: -d is used to determine if the test is being run |
|
160 in debug mode. The regress tool requires only one line output:PASS or FAIL. |
|
161 All of the printfs associated with this test has been handled with a if (debug_mode) |
|
162 test. |
|
163 Usage: test_name -d |
|
164 */ |
|
165 PLOptStatus os; |
|
166 PLOptState *opt = PL_CreateOptState(argc, argv, "d:"); |
|
167 while (PL_OPT_EOL != (os = PL_GetNextOpt(opt))) |
|
168 { |
|
169 if (PL_OPT_BAD == os) continue; |
|
170 switch (opt->option) |
|
171 { |
|
172 case 'd': /* debug mode */ |
|
173 debug_mode = 1; |
|
174 break; |
|
175 default: |
|
176 break; |
|
177 } |
|
178 } |
|
179 PL_DestroyOptState(opt); |
|
180 |
|
181 /* main test */ |
|
182 |
|
183 if (argc > 2) |
|
184 num_threads = atoi(argv[2]); |
|
185 else |
|
186 num_threads = NUM_THREADS; |
|
187 |
|
188 PR_Init(PR_USER_THREAD, PR_PRIORITY_LOW, 0); |
|
189 PR_STDIO_INIT(); |
|
190 |
|
191 if (debug_mode) printf("kernel level test\n"); |
|
192 thread_test(PR_GLOBAL_THREAD, num_threads); |
|
193 |
|
194 PR_Cleanup(); |
|
195 |
|
196 if(failed_already) |
|
197 return 1; |
|
198 else |
|
199 return 0; |
|
200 |
|
201 } |