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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
20 #include "apr_general.h"
22 #include "apr_reslist.h"
23 #include "apr_thread_pool.h"
27 #endif /* APR_HAVE_TIME_H */
35 #define RESLIST_SMAX 10
36 #define RESLIST_HMAX 20
37 #define RESLIST_TTL APR_TIME_C(35000) /* 35 ms */
38 #define CONSUMER_THREADS 25
39 #define CONSUMER_ITERATIONS 250
40 #define CONSTRUCT_SLEEP_TIME APR_TIME_C(25000) /* 25 ms */
41 #define DESTRUCT_SLEEP_TIME APR_TIME_C(10000) /* 10 ms */
42 #define WORK_DELAY_SLEEP_TIME APR_TIME_C(15000) /* 15 ms */
45 apr_interval_time_t sleep_upon_construct;
46 apr_interval_time_t sleep_upon_destruct;
55 /* Linear congruential generator */
56 static apr_uint32_t lgc(apr_uint32_t a)
60 z %= APR_UINT64_C(4294967291);
61 return (apr_uint32_t)z;
64 static apr_status_t my_constructor(void **resource, void *params,
68 my_parameters_t *my_params = params;
70 /* Create some resource */
71 res = apr_palloc(pool, sizeof(*res));
72 res->id = my_params->c_count++;
74 /* Sleep for awhile, to simulate construction overhead. */
75 apr_sleep(my_params->sleep_upon_construct);
77 /* Set the resource so it can be managed by the reslist */
82 static apr_status_t my_destructor(void *resource, void *params,
85 my_resource_t *res = resource;
86 my_parameters_t *my_params = params;
87 res->id = my_params->d_count++;
89 apr_sleep(my_params->sleep_upon_destruct);
97 apr_reslist_t *reslist;
98 apr_interval_time_t work_delay_sleep;
101 /* MAX_UINT * .95 = 2**32 * .95 = 4080218931u */
102 #define PERCENT95th 4080218931u
104 static void * APR_THREAD_FUNC resource_consuming_thread(apr_thread_t *thd,
112 my_thread_info_t *thread_info = data;
113 apr_reslist_t *rl = thread_info->reslist;
116 apr_generate_random_bytes((void*)&chance, sizeof(chance));
118 chance = (apr_uint32_t)(apr_time_now() % APR_TIME_C(4294967291));
121 for (i = 0; i < CONSUMER_ITERATIONS; i++) {
122 rv = apr_reslist_acquire(rl, &vp);
123 ABTS_INT_EQUAL(thread_info->tc, APR_SUCCESS, rv);
125 apr_sleep(thread_info->work_delay_sleep);
127 /* simulate a 5% chance of the resource being bad */
128 chance = lgc(chance);
129 if ( chance < PERCENT95th ) {
130 rv = apr_reslist_release(rl, res);
131 ABTS_INT_EQUAL(thread_info->tc, APR_SUCCESS, rv);
133 rv = apr_reslist_invalidate(rl, res);
134 ABTS_INT_EQUAL(thread_info->tc, APR_SUCCESS, rv);
141 static void test_timeout(abts_case *tc, apr_reslist_t *rl)
144 my_resource_t *resources[RESLIST_HMAX];
148 apr_reslist_timeout_set(rl, 1000);
150 /* deplete all possible resources from the resource list
151 * so that the next call will block until timeout is reached
152 * (since there are no other threads to make a resource
156 for (i = 0; i < RESLIST_HMAX; i++) {
157 rv = apr_reslist_acquire(rl, (void**)&resources[i]);
158 ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
161 /* next call will block until timeout is reached */
162 rv = apr_reslist_acquire(rl, &vp);
163 ABTS_TRUE(tc, APR_STATUS_IS_TIMEUP(rv));
165 /* release the resources; otherwise the destroy operation
168 for (i = 0; i < RESLIST_HMAX; i++) {
169 rv = apr_reslist_release(rl, resources[i]);
170 ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
174 static void test_shrinking(abts_case *tc, apr_reslist_t *rl)
177 my_resource_t *resources[RESLIST_HMAX];
181 int sleep_time = RESLIST_TTL / RESLIST_HMAX;
183 /* deplete all possible resources from the resource list */
184 for (i = 0; i < RESLIST_HMAX; i++) {
185 rv = apr_reslist_acquire(rl, (void**)&resources[i]);
186 ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
189 /* Free all resources above RESLIST_SMAX - 1 */
190 for (i = RESLIST_SMAX - 1; i < RESLIST_HMAX; i++) {
191 rv = apr_reslist_release(rl, resources[i]);
192 ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
195 for (i = 0; i < RESLIST_HMAX; i++) {
196 rv = apr_reslist_acquire(rl, &vp);
197 ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
199 apr_sleep(sleep_time);
200 rv = apr_reslist_release(rl, res);
201 ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
203 apr_sleep(sleep_time);
206 * Now free the remaining elements. This should trigger the shrinking of
209 for (i = 0; i < RESLIST_SMAX - 1; i++) {
210 rv = apr_reslist_release(rl, resources[i]);
211 ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
215 static void test_reslist(abts_case *tc, void *data)
220 my_parameters_t *params;
221 apr_thread_pool_t *thrp;
222 my_thread_info_t thread_info[CONSUMER_THREADS];
224 rv = apr_thread_pool_create(&thrp, CONSUMER_THREADS/2, CONSUMER_THREADS, p);
225 ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
227 /* Create some parameters that will be passed into each
228 * constructor and destructor call. */
229 params = apr_pcalloc(p, sizeof(*params));
230 params->sleep_upon_construct = CONSTRUCT_SLEEP_TIME;
231 params->sleep_upon_destruct = DESTRUCT_SLEEP_TIME;
233 /* We're going to want 10 blocks of data from our target rmm. */
234 rv = apr_reslist_create(&rl, RESLIST_MIN, RESLIST_SMAX, RESLIST_HMAX,
235 RESLIST_TTL, my_constructor, my_destructor,
237 ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
239 for (i = 0; i < CONSUMER_THREADS; i++) {
240 thread_info[i].tid = i;
241 thread_info[i].tc = tc;
242 thread_info[i].reslist = rl;
243 thread_info[i].work_delay_sleep = WORK_DELAY_SLEEP_TIME;
244 rv = apr_thread_pool_push(thrp, resource_consuming_thread,
245 &thread_info[i], 0, NULL);
246 ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
249 rv = apr_thread_pool_destroy(thrp);
250 ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
252 test_timeout(tc, rl);
254 test_shrinking(tc, rl);
255 ABTS_INT_EQUAL(tc, RESLIST_SMAX, params->c_count - params->d_count);
257 rv = apr_reslist_destroy(rl);
258 ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
261 #endif /* APR_HAS_THREADS */
263 abts_suite *testreslist(abts_suite *suite)
265 suite = ADD_SUITE(suite);
268 abts_run_test(suite, test_reslist, NULL);