bottleneck testcase based on rubbos
[bottlenecks.git] / rubbos / app / httpd-2.0.64 / srclib / apr / test / testsock.c
1 /* Licensed to the Apache Software Foundation (ASF) under one or more
2  * contributor license agreements.  See the NOTICE file distributed with
3  * this work for additional information regarding copyright ownership.
4  * The ASF licenses this file to You under the Apache License, Version 2.0
5  * (the "License"); you may not use this file except in compliance with
6  * the License.  You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #include <stdio.h>
18 #include <stdlib.h>
19 #include <signal.h>
20 #include "apr_thread_proc.h"
21 #include "apr_errno.h"
22 #include "apr_general.h"
23 #include "apr_lib.h"
24 #include "apr_strings.h"
25
26 #define STRLEN 15
27
28 static int run_basic_test(apr_pool_t *context)
29 {
30     apr_procattr_t *attr1 = NULL;
31     apr_procattr_t *attr2 = NULL;
32     apr_proc_t proc1;
33     apr_proc_t proc2;
34     apr_status_t s1;
35     apr_status_t s2;
36     const char *args[2];
37
38     fprintf(stdout, "Creating children to run network tests.......\n");
39     s1 = apr_procattr_create(&attr1, context);
40     s2 = apr_procattr_create(&attr2, context);
41
42     if (s1 != APR_SUCCESS || s2 != APR_SUCCESS) {
43         fprintf(stderr, "Problem creating proc attrs\n");
44         exit(-1);
45     }
46
47     args[0] = apr_pstrdup(context, "server");
48     args[1] = NULL; 
49     s1 = apr_proc_create(&proc1, "./server", args, NULL, attr1, context);
50
51     /* Sleep for 5 seconds to ensure the server is setup before we begin */
52     apr_sleep(5000000);
53     args[0] = apr_pstrdup(context, "client");
54     s2 = apr_proc_create(&proc2, "./client", args, NULL, attr2, context);
55
56     if (s1 != APR_SUCCESS || s2 != APR_SUCCESS) {
57         fprintf(stderr, "Problem spawning new process\n");
58         exit(-1);
59     }
60
61     while ((s1 = apr_proc_wait(&proc1, NULL, NULL, APR_NOWAIT)) == APR_CHILD_NOTDONE && 
62            (s2 = apr_proc_wait(&proc2, NULL, NULL, APR_NOWAIT)) == APR_CHILD_NOTDONE) {
63         continue;
64     }
65
66     if (s1 == APR_SUCCESS) {
67         apr_proc_kill(&proc2, SIGTERM);
68         while (apr_proc_wait(&proc2, NULL, NULL, APR_WAIT) == APR_CHILD_NOTDONE);
69     }
70     else {
71         apr_proc_kill(&proc1, SIGTERM);
72         while (apr_proc_wait(&proc1, NULL, NULL, APR_WAIT) == APR_CHILD_NOTDONE);
73     }
74     fprintf(stdout, "Network test completed.\n");   
75
76     return 1;
77 }
78
79 static int run_sendfile(apr_pool_t *context, int number)
80 {
81     apr_procattr_t *attr1 = NULL;
82     apr_procattr_t *attr2 = NULL;
83     apr_proc_t proc1;
84     apr_proc_t proc2;
85     apr_status_t s1;
86     apr_status_t s2;
87     const char *args[4];
88
89     fprintf(stdout, "Creating children to run network tests.......\n");
90     s1 = apr_procattr_create(&attr1, context);
91     s2 = apr_procattr_create(&attr2, context);
92
93     if (s1 != APR_SUCCESS || s2 != APR_SUCCESS) {
94         fprintf(stderr, "Problem creating proc attrs\n");
95         exit(-1);
96     }
97
98     args[0] = apr_pstrdup(context, "sendfile");
99     args[1] = apr_pstrdup(context, "server");
100     args[2] = NULL; 
101     s1 = apr_proc_create(&proc1, "./sendfile", args, NULL, attr1, context);
102
103     /* Sleep for 5 seconds to ensure the server is setup before we begin */
104     apr_sleep(5000000);
105     args[1] = apr_pstrdup(context, "client");
106     switch (number) {
107         case 0: {
108             args[2] = apr_pstrdup(context, "blocking");
109             break;
110         }
111         case 1: {
112             args[2] = apr_pstrdup(context, "nonblocking");
113             break;
114         }
115         case 2: {
116             args[2] = apr_pstrdup(context, "timeout");
117             break;
118         }
119     }
120     args[3] = NULL;
121     s2 = apr_proc_create(&proc2, "./sendfile", args, NULL, attr2, context);
122
123     if (s1 != APR_SUCCESS || s2 != APR_SUCCESS) {
124         fprintf(stderr, "Problem spawning new process\n");
125         exit(-1);
126     }
127
128     while ((s1 = apr_proc_wait(&proc1, NULL, NULL, APR_NOWAIT)) == APR_CHILD_NOTDONE && 
129            (s2 = apr_proc_wait(&proc2, NULL, NULL, APR_NOWAIT)) == APR_CHILD_NOTDONE) {
130         continue;
131     }
132
133     if (s1 == APR_SUCCESS) {
134         apr_proc_kill(&proc2, SIGTERM);
135         while (apr_proc_wait(&proc2, NULL, NULL, APR_WAIT) == APR_CHILD_NOTDONE);
136     }
137     else {
138         apr_proc_kill(&proc1, SIGTERM);
139         while (apr_proc_wait(&proc1, NULL, NULL, APR_WAIT) == APR_CHILD_NOTDONE);
140     }
141     fprintf(stdout, "Network test completed.\n");   
142
143     return 1;
144 }
145
146 int main(int argc, char *argv[])
147 {
148     apr_pool_t *context = NULL;
149
150     fprintf(stdout, "Initializing.........");
151     if (apr_initialize() != APR_SUCCESS) {
152         fprintf(stderr, "Something went wrong\n");
153         exit(-1);
154     }
155     fprintf(stdout, "OK\n");
156     atexit(apr_terminate);
157
158     fprintf(stdout, "Creating context.......");
159     if (apr_pool_create(&context, NULL) != APR_SUCCESS) {
160         fprintf(stderr, "Could not create context\n");
161         exit(-1);
162     }
163     fprintf(stdout, "OK\n");
164
165     fprintf(stdout, "This test relies on the process test working.  Please\n");
166     fprintf(stdout, "run that test first, and only run this test when it\n");
167     fprintf(stdout, "completes successfully.  Alternatively, you could run\n");
168     fprintf(stdout, "server and client by yourself.\n");
169     run_basic_test(context);
170     run_sendfile(context, 0);
171     run_sendfile(context, 1);
172     run_sendfile(context, 2);
173
174     return 0;
175 }