Revert change to testdate.c (commited by accident)
[apr-util.git] / test / testqueue.c
blob3bec67c13e356ce8830fa6c9794c4d52ffe5d008
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.
17 #include "apu.h"
18 #include "apr_queue.h"
19 #include "apr_thread_pool.h"
20 #include "apr_time.h"
21 #include "abts.h"
22 #include "testutil.h"
24 #if APR_HAS_THREADS
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
31 #define SLEEP_TIME 30
33 static apr_queue_t *queue;
35 static void * APR_THREAD_FUNC consumer(apr_thread_t *thd, void *data)
37 long sleeprate;
38 abts_case *tc = data;
39 apr_status_t rv;
40 void *v;
42 sleeprate = 1000000/CONSUMER_ACTIVITY;
43 apr_sleep((rand() % 4) * 1000000); /* sleep random seconds */
45 while (1) {
46 rv = apr_queue_pop(queue, &v);
48 if (rv == APR_EINTR) {
49 continue;
50 } else if (rv == APR_EOF) {
51 break;
54 ABTS_TRUE(tc, v == NULL);
55 ABTS_TRUE(tc, rv == APR_SUCCESS);
57 apr_sleep(sleeprate); /* sleep this long to acheive our rate */
60 return NULL;
63 static void * APR_THREAD_FUNC producer(apr_thread_t *thd, void *data)
65 long sleeprate;
66 apr_status_t rv;
68 sleeprate = 1000000/PRODUCER_ACTIVITY;
69 apr_sleep((rand() % 4) * 1000000); /* sleep random seconds */
71 while (1) {
72 do {
73 rv = apr_queue_push(queue, NULL);
74 } while (rv == APR_EINTR);
76 if (rv == APR_EOF) {
77 break;
80 apr_sleep(sleeprate); /* sleep this long to acheive our rate */
83 return NULL;
86 static void test_queue_producer_consumer(abts_case *tc, void *data)
88 unsigned int i;
89 apr_status_t rv;
90 apr_thread_pool_t *thrp;
92 /* XXX: non-portable */
93 srand((unsigned int)apr_time_now());
95 rv = apr_queue_create(&queue, QUEUE_SIZE, p);
96 ABTS_INT_EQUAL(tc, rv, APR_SUCCESS);
98 rv = apr_thread_pool_create(&thrp, 0, NUMBER_CONSUMERS + NUMBER_PRODUCERS, p);
99 ABTS_INT_EQUAL(tc, rv, APR_SUCCESS);
101 for (i = 0; i < NUMBER_CONSUMERS; i++) {
102 rv = apr_thread_pool_push(thrp, consumer, tc, 0, NULL);
103 ABTS_INT_EQUAL(tc, rv, APR_SUCCESS);
106 for (i = 0; i < NUMBER_PRODUCERS; i++) {
107 rv = apr_thread_pool_push(thrp, producer, tc, 0, NULL);
108 ABTS_INT_EQUAL(tc, rv, APR_SUCCESS);
111 apr_sleep(SLEEP_TIME * 1000000); /* sleep 10 seconds */
113 rv = apr_queue_term(queue);
114 ABTS_INT_EQUAL(tc, rv, APR_SUCCESS);
116 rv = apr_thread_pool_destroy(thrp);
117 ABTS_INT_EQUAL(tc, rv, APR_SUCCESS);
120 #endif /* APR_HAS_THREADS */
122 abts_suite *testqueue(abts_suite *suite)
124 suite = ADD_SUITE(suite);
126 #if APR_HAS_THREADS
127 abts_run_test(suite, test_queue_producer_consumer, NULL);
128 #endif /* APR_HAS_THREADS */
130 return suite;