~ubuntu-branches/ubuntu/precise/kompozer/precise

« back to all changes in this revision

Viewing changes to mozilla/nsprpub/pr/tests/io_timeoutk.c

  • Committer: Bazaar Package Importer
  • Author(s): Anthony Yarusso
  • Date: 2007-08-27 01:11:03 UTC
  • Revision ID: james.westby@ubuntu.com-20070827011103-2jgf4s6532gqu2ka
Tags: upstream-0.7.10
ImportĀ upstreamĀ versionĀ 0.7.10

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
 
2
/* 
 
3
 * The contents of this file are subject to the Mozilla Public
 
4
 * License Version 1.1 (the "License"); you may not use this file
 
5
 * except in compliance with the License. You may obtain a copy of
 
6
 * the License at http://www.mozilla.org/MPL/
 
7
 * 
 
8
 * Software distributed under the License is distributed on an "AS
 
9
 * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
 
10
 * implied. See the License for the specific language governing
 
11
 * rights and limitations under the License.
 
12
 * 
 
13
 * The Original Code is the Netscape Portable Runtime (NSPR).
 
14
 * 
 
15
 * The Initial Developer of the Original Code is Netscape
 
16
 * Communications Corporation.  Portions created by Netscape are 
 
17
 * Copyright (C) 1998-2000 Netscape Communications Corporation.  All
 
18
 * Rights Reserved.
 
19
 * 
 
20
 * Contributor(s):
 
21
 * 
 
22
 * Alternatively, the contents of this file may be used under the
 
23
 * terms of the GNU General Public License Version 2 or later (the
 
24
 * "GPL"), in which case the provisions of the GPL are applicable 
 
25
 * instead of those above.  If you wish to allow use of your 
 
26
 * version of this file only under the terms of the GPL and not to
 
27
 * allow others to use your version of this file under the MPL,
 
28
 * indicate your decision by deleting the provisions above and
 
29
 * replace them with the notice and other provisions required by
 
30
 * the GPL.  If you do not delete the provisions above, a recipient
 
31
 * may use your version of this file under either the MPL or the
 
32
 * GPL.
 
33
 */
 
34
 
 
35
/*
 
36
** name io_timeoutk.c
 
37
** Description:Test socket IO timeouts (kernel level)
 
38
**
 
39
** Modification History:
 
40
** 19-May-97 AGarcia- Converted the test to accomodate the debug_mode flag.
 
41
**               The debug mode will print all of the printfs associated with this test.
 
42
**                       The regress mode will be the default mode. Since the regress tool limits
 
43
**           the output to a one line status:PASS or FAIL,all of the printf statements
 
44
**                       have been handled with an if (debug_mode) statement.
 
45
** 04-June-97 AGarcia removed the Test_Result function. Regress tool has been updated to
 
46
**                      recognize the return code from tha main program.
 
47
***********************************************************************/
 
48
/***********************************************************************
 
49
** Includes
 
50
***********************************************************************/
 
51
/* Used to get the command line option */
 
52
#include "plgetopt.h"
 
53
 
 
54
#include <stdio.h>
 
55
#include "nspr.h"
 
56
 
 
57
#define NUM_THREADS 1
 
58
#define BASE_PORT   8000
 
59
#define DEFAULT_ACCEPT_TIMEOUT 2
 
60
 
 
61
typedef struct threadInfo {
 
62
    PRInt16 id;
 
63
    PRInt16 accept_timeout;
 
64
    PRLock *dead_lock;
 
65
    PRCondVar *dead_cv;
 
66
    PRInt32   *alive;
 
67
} threadInfo;
 
68
 
 
69
PRIntn failed_already=0;
 
70
PRIntn debug_mode;
 
71
 
 
72
 
 
73
void 
 
74
thread_main(void *_info)
 
75
{
 
76
    threadInfo *info = (threadInfo *)_info;
 
77
    PRNetAddr listenAddr;
 
78
    PRNetAddr clientAddr;
 
79
    PRFileDesc *listenSock = NULL;
 
80
    PRFileDesc *clientSock;
 
81
    PRStatus rv;
 
82
 
 
83
    if (debug_mode) printf("thread %d is alive\n", info->id);
 
84
 
 
85
    listenSock = PR_NewTCPSocket();
 
86
    if (!listenSock) {
 
87
        if (debug_mode) printf("unable to create listen socket\n");
 
88
        goto dead;
 
89
    }
 
90
  
 
91
    listenAddr.inet.family = AF_INET;
 
92
    listenAddr.inet.port = PR_htons(BASE_PORT + info->id);
 
93
    listenAddr.inet.ip = PR_htonl(INADDR_ANY);
 
94
    rv = PR_Bind(listenSock, &listenAddr);
 
95
    if (rv == PR_FAILURE) {
 
96
        if (debug_mode) printf("unable to bind\n");
 
97
        goto dead;
 
98
    }
 
99
 
 
100
    rv = PR_Listen(listenSock, 4);
 
101
    if (rv == PR_FAILURE) {
 
102
        if (debug_mode) printf("unable to listen\n");
 
103
        goto dead;
 
104
    }
 
105
 
 
106
    if (debug_mode) printf("thread %d going into accept for %d seconds\n", 
 
107
        info->id, info->accept_timeout + info->id);
 
108
 
 
109
    clientSock = PR_Accept(listenSock, &clientAddr, PR_SecondsToInterval(info->accept_timeout +info->id));
 
110
 
 
111
    if (clientSock == NULL) {
 
112
        if (PR_GetError() == PR_IO_TIMEOUT_ERROR) 
 
113
            if (debug_mode) {
 
114
                                printf("PR_Accept() timeout worked!\n");
 
115
                printf("TEST FAILED! PR_Accept() returned error %d\n",
 
116
                                                                   PR_GetError());
 
117
                        }
 
118
                        else failed_already=1;
 
119
    } else {
 
120
        if (debug_mode) printf ("TEST FAILED! PR_Accept() succeeded?\n");
 
121
                else failed_already=1;
 
122
        PR_Close(clientSock);
 
123
    }
 
124
 
 
125
dead:
 
126
    if (listenSock) {
 
127
        PR_Close(listenSock);
 
128
    }
 
129
    PR_Lock(info->dead_lock);
 
130
    (*info->alive)--;
 
131
    PR_NotifyCondVar(info->dead_cv);
 
132
    PR_Unlock(info->dead_lock);
 
133
 
 
134
    if (debug_mode) printf("thread %d is dead\n", info->id);
 
135
}
 
136
 
 
137
void
 
138
thread_test(PRInt32 scope, PRInt32 num_threads)
 
139
{
 
140
    PRInt32 index;
 
141
    PRThread *thr;
 
142
    PRLock *dead_lock;
 
143
    PRCondVar *dead_cv;
 
144
    PRInt32 alive;
 
145
 
 
146
    if (debug_mode) printf("IO Timeout test started with %d threads\n", num_threads);
 
147
 
 
148
    dead_lock = PR_NewLock();
 
149
    dead_cv = PR_NewCondVar(dead_lock);
 
150
    alive = num_threads;
 
151
    
 
152
    for (index = 0; index < num_threads; index++) {
 
153
        threadInfo *info = (threadInfo *)malloc(sizeof(threadInfo));
 
154
 
 
155
        info->id = index;
 
156
        info->dead_lock = dead_lock;
 
157
        info->dead_cv = dead_cv;
 
158
        info->alive = &alive;
 
159
        info->accept_timeout = DEFAULT_ACCEPT_TIMEOUT;
 
160
        
 
161
        thr = PR_CreateThread( PR_USER_THREAD,
 
162
                               thread_main,
 
163
                               (void *)info,
 
164
                               PR_PRIORITY_NORMAL,
 
165
                               scope,
 
166
                               PR_UNJOINABLE_THREAD,
 
167
                               0);
 
168
 
 
169
        if (!thr) {
 
170
            PR_Lock(dead_lock);
 
171
            alive--;
 
172
            PR_Unlock(dead_lock);
 
173
        }
 
174
    }
 
175
 
 
176
    PR_Lock(dead_lock);
 
177
    while(alive) {
 
178
        if (debug_mode) printf("main loop awake; alive = %d\n", alive);
 
179
        PR_WaitCondVar(dead_cv, PR_INTERVAL_NO_TIMEOUT);
 
180
    }
 
181
    PR_Unlock(dead_lock);
 
182
}
 
183
 
 
184
int main(int argc, char **argv)
 
185
{
 
186
    PRInt32 num_threads;
 
187
 
 
188
        /* The command line argument: -d is used to determine if the test is being run
 
189
        in debug mode. The regress tool requires only one line output:PASS or FAIL.
 
190
        All of the printfs associated with this test has been handled with a if (debug_mode)
 
191
        test.
 
192
        Usage: test_name -d
 
193
        */
 
194
        PLOptStatus os;
 
195
        PLOptState *opt = PL_CreateOptState(argc, argv, "d:");
 
196
        while (PL_OPT_EOL != (os = PL_GetNextOpt(opt)))
 
197
    {
 
198
                if (PL_OPT_BAD == os) continue;
 
199
        switch (opt->option)
 
200
        {
 
201
        case 'd':  /* debug mode */
 
202
                        debug_mode = 1;
 
203
            break;
 
204
         default:
 
205
            break;
 
206
        }
 
207
    }
 
208
        PL_DestroyOptState(opt);
 
209
 
 
210
 /* main test */
 
211
        
 
212
    if (argc > 2)
 
213
        num_threads = atoi(argv[2]);
 
214
    else
 
215
        num_threads = NUM_THREADS;
 
216
 
 
217
    PR_Init(PR_USER_THREAD, PR_PRIORITY_LOW, 0);
 
218
    PR_STDIO_INIT();
 
219
 
 
220
    if (debug_mode)     printf("kernel level test\n");
 
221
    thread_test(PR_GLOBAL_THREAD, num_threads);
 
222
 
 
223
     PR_Cleanup();
 
224
     
 
225
     if(failed_already)    
 
226
        return 1;
 
227
    else
 
228
        return 0;
 
229
 
 
230
}