bottleneck testcase based on rubbos
[bottlenecks.git] / rubbos / app / httpd-2.0.64 / srclib / apr / test / testpipe.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 <stdlib.h>
18
19 #include "test_apr.h"
20 #include "apr_file_io.h"
21 #include "apr_errno.h"
22 #include "apr_general.h"
23 #include "apr_lib.h"
24 #include "apr_thread_proc.h"
25 #include "apr_strings.h"
26
27 static apr_file_t *readp = NULL;
28 static apr_file_t *writep = NULL;
29
30 static void create_pipe(CuTest *tc)
31 {
32     apr_status_t rv;
33
34     rv = apr_file_pipe_create(&readp, &writep, p);
35     CuAssertIntEquals(tc, APR_SUCCESS, rv);
36     CuAssertPtrNotNull(tc, readp);
37     CuAssertPtrNotNull(tc, writep);
38 }   
39
40 static void close_pipe(CuTest *tc)
41 {
42     apr_status_t rv;
43     apr_size_t nbytes = 256;
44     char buf[256];
45
46     rv = apr_file_close(readp);
47     rv = apr_file_close(writep);
48     CuAssertIntEquals(tc, APR_SUCCESS, rv);
49
50     rv = apr_file_read(readp, buf, &nbytes);
51     CuAssertIntEquals(tc, 1, APR_STATUS_IS_EBADF(rv));
52 }   
53
54 static void set_timeout(CuTest *tc)
55 {
56     apr_status_t rv;
57     apr_file_t *readp = NULL;
58     apr_file_t *writep = NULL;
59     apr_interval_time_t timeout;
60
61     rv = apr_file_pipe_create(&readp, &writep, p);
62     CuAssertIntEquals(tc, APR_SUCCESS, rv);
63     CuAssertPtrNotNull(tc, readp);
64     CuAssertPtrNotNull(tc, writep);
65
66     rv = apr_file_pipe_timeout_get(readp, &timeout);
67     CuAssertIntEquals(tc, APR_SUCCESS, rv);
68     CuAssert(tc, "Timeout mismatch, expected -1", timeout == -1);
69
70     rv = apr_file_pipe_timeout_set(readp, apr_time_from_sec(1));
71     CuAssertIntEquals(tc, APR_SUCCESS, rv);
72
73     rv = apr_file_pipe_timeout_get(readp, &timeout);
74     CuAssertIntEquals(tc, APR_SUCCESS, rv);
75     CuAssert(tc, "Timeout mismatch, expected 1 second", 
76                      timeout == apr_time_from_sec(1));
77 }
78
79 static void read_write(CuTest *tc)
80 {
81     apr_status_t rv;
82     char *buf;
83     apr_size_t nbytes;
84     
85     nbytes = strlen("this is a test");
86     buf = (char *)apr_palloc(p, nbytes + 1);
87
88     rv = apr_file_pipe_create(&readp, &writep, p);
89     CuAssertIntEquals(tc, APR_SUCCESS, rv);
90     CuAssertPtrNotNull(tc, readp);
91     CuAssertPtrNotNull(tc, writep);
92
93     rv = apr_file_pipe_timeout_set(readp, apr_time_from_sec(1));
94     CuAssertIntEquals(tc, APR_SUCCESS, rv);
95
96     rv = apr_file_read(readp, buf, &nbytes);
97     if (!rv) {
98       CuAssertIntEquals(tc, 1, APR_STATUS_IS_TIMEUP(rv));
99       CuAssertIntEquals(tc, 0, nbytes);
100     }
101 }
102
103 static void read_write_notimeout(CuTest *tc)
104 {
105     apr_status_t rv;
106     char *buf = "this is a test";
107     char *input;
108     apr_size_t nbytes;
109     
110     nbytes = strlen("this is a test");
111
112     rv = apr_file_pipe_create(&readp, &writep, p);
113     CuAssertIntEquals(tc, APR_SUCCESS, rv);
114     CuAssertPtrNotNull(tc, readp);
115     CuAssertPtrNotNull(tc, writep);
116
117     rv = apr_file_write(writep, buf, &nbytes);
118     CuAssertIntEquals(tc, strlen("this is a test"), nbytes);
119     CuAssertIntEquals(tc, APR_SUCCESS, rv);
120
121     nbytes = 256;
122     input = apr_pcalloc(p, nbytes + 1);
123     rv = apr_file_read(readp, input, &nbytes);
124     CuAssertIntEquals(tc, APR_SUCCESS, rv);
125     CuAssertIntEquals(tc, strlen("this is a test"), nbytes);
126     CuAssertStrEquals(tc, "this is a test", input);
127 }
128
129 /* XXX FIXME */
130 #ifdef WIN32
131 #define EXTENSION ".exe"
132 #elif NETWARE
133 #define EXTENSION ".nlm"
134 #else
135 #define EXTENSION
136 #endif
137
138 static void test_pipe_writefull(CuTest *tc)
139 {
140     int iterations = 1000;
141     int i;
142     int bytes_per_iteration = 8000;
143     char *buf = (char *)malloc(bytes_per_iteration);
144     char responsebuf[128];
145     apr_size_t nbytes;
146     int bytes_processed;
147     apr_proc_t proc = {0};
148     apr_procattr_t *procattr;
149     const char *args[2];
150     apr_status_t rv;
151     
152     rv = apr_procattr_create(&procattr, p);
153     CuAssertIntEquals(tc, APR_SUCCESS, rv);
154
155     rv = apr_procattr_io_set(procattr, APR_CHILD_BLOCK, APR_CHILD_BLOCK,
156                              APR_CHILD_BLOCK);
157     CuAssertIntEquals(tc, APR_SUCCESS, rv);
158
159     rv = apr_procattr_error_check_set(procattr, 1);
160     CuAssertIntEquals(tc, APR_SUCCESS, rv);
161
162     args[0] = "readchild" EXTENSION;
163     args[1] = NULL;
164     rv = apr_proc_create(&proc, "./readchild" EXTENSION, args, NULL, procattr, p);
165     CuAssertIntEquals(tc, APR_SUCCESS, rv);
166
167     rv = apr_file_pipe_timeout_set(proc.in, apr_time_from_sec(10));
168     CuAssertIntEquals(tc, APR_SUCCESS, rv);
169
170     rv = apr_file_pipe_timeout_set(proc.out, apr_time_from_sec(10));
171     CuAssertIntEquals(tc, APR_SUCCESS, rv);
172
173     i = iterations;
174     do {
175         rv = apr_file_write_full(proc.in, buf, bytes_per_iteration, NULL);
176         CuAssertIntEquals(tc, APR_SUCCESS, rv);
177     } while (--i);
178
179     free(buf);
180
181     rv = apr_file_close(proc.in);
182     CuAssertIntEquals(tc, APR_SUCCESS, rv);
183     
184     nbytes = sizeof(responsebuf);
185     rv = apr_file_read(proc.out, responsebuf, &nbytes);
186     CuAssertIntEquals(tc, APR_SUCCESS, rv);
187     bytes_processed = (int)apr_strtoi64(responsebuf, NULL, 10);
188     CuAssertIntEquals(tc, iterations * bytes_per_iteration, bytes_processed);
189 }
190
191 CuSuite *testpipe(void)
192 {
193     CuSuite *suite = CuSuiteNew("Pipes");
194
195     SUITE_ADD_TEST(suite, create_pipe);
196     SUITE_ADD_TEST(suite, close_pipe);
197     SUITE_ADD_TEST(suite, set_timeout);
198     SUITE_ADD_TEST(suite, read_write);
199     SUITE_ADD_TEST(suite, read_write_notimeout);
200     SUITE_ADD_TEST(suite, test_pipe_writefull);
201
202     return suite;
203 }
204