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.
18 #include "apr_queue.h"
19 #include "apr_thread_pool.h"
26 #define NUMBER_CONSUMERS 3
27 #define CONSUMER_ACTIVITY 4
28 #define NUMBER_PRODUCERS 4
29 #define PRODUCER_ACTIVITY 5
30 #define QUEUE_SIZE 100
32 static apr_queue_t *queue;
34 static void * APR_THREAD_FUNC consumer(apr_thread_t *thd, void *data)
41 sleeprate = 1000000/CONSUMER_ACTIVITY;
42 apr_sleep((rand() % 4) * 1000000); /* sleep random seconds */
46 rv = apr_queue_pop(queue, &v);
54 ABTS_TRUE(tc, v == NULL);
55 ABTS_TRUE(tc, rv == APR_SUCCESS);
57 apr_sleep(sleeprate); /* sleep this long to acheive our rate */
63 static void * APR_THREAD_FUNC producer(apr_thread_t *thd, void *data)
69 sleeprate = 1000000/PRODUCER_ACTIVITY;
70 apr_sleep((rand() % 4) * 1000000); /* sleep random seconds */
74 rv = apr_queue_push(queue, NULL);
82 ABTS_TRUE(tc, rv == APR_SUCCESS);
84 apr_sleep(sleeprate); /* sleep this long to acheive our rate */
90 static void test_queue_producer_consumer(abts_case *tc, void *data)
94 apr_thread_pool_t *thrp;
96 /* XXX: non-portable */
97 srand((unsigned int)apr_time_now());
99 rv = apr_queue_create(&queue, QUEUE_SIZE, p);
100 ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
102 rv = apr_thread_pool_create(&thrp, 0, NUMBER_CONSUMERS + NUMBER_PRODUCERS, p);
103 ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
105 for (i = 0; i < NUMBER_CONSUMERS; i++) {
106 rv = apr_thread_pool_push(thrp, consumer, tc, 0, NULL);
107 ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
110 for (i = 0; i < NUMBER_PRODUCERS; i++) {
111 rv = apr_thread_pool_push(thrp, producer, tc, 0, NULL);
112 ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
115 apr_sleep(5000000); /* sleep 5 seconds */
117 rv = apr_queue_term(queue);
118 ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
120 rv = apr_thread_pool_destroy(thrp);
121 ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
124 #endif /* APR_HAS_THREADS */
126 abts_suite *testqueue(abts_suite *suite)
128 suite = ADD_SUITE(suite);
131 abts_run_test(suite, test_queue_producer_consumer, NULL);
132 #endif /* APR_HAS_THREADS */