1 /***
2 This file is part of PulseAudio.
3
4 PulseAudio is free software; you can redistribute it and/or modify
5 it under the terms of the GNU Lesser General Public License as published
6 by the Free Software Foundation; either version 2.1 of the License,
7 or (at your option) any later version.
8
9 PulseAudio is distributed in the hope that it will be useful, but
10 WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 General Public License for more details.
13
14 You should have received a copy of the GNU Lesser General Public License
15 along with PulseAudio; if not, see <http://www.gnu.org/licenses/>.
16 ***/
17
18 #ifdef HAVE_CONFIG_H
19 #include <config.h>
20 #endif
21
22 #include <assert.h>
23 #include <stdlib.h>
24 #include <unistd.h>
25
26 #include <check.h>
27
28 #include <pulse/util.h>
29 #include <pulsecore/asyncq.h>
30 #include <pulsecore/thread.h>
31 #include <pulsecore/log.h>
32 #include <pulsecore/macro.h>
33
producer(void * _q)34 static void producer(void *_q) {
35 pa_asyncq *q = _q;
36 int i;
37
38 for (i = 0; i < 1000; i++) {
39 pa_log_debug("pushing %i", i);
40 pa_asyncq_push(q, PA_UINT_TO_PTR(i+1), 1);
41 }
42
43 pa_asyncq_push(q, PA_UINT_TO_PTR(-1), true);
44 pa_log_debug("pushed end");
45 }
46
consumer(void * _q)47 static void consumer(void *_q) {
48 pa_asyncq *q = _q;
49 void *p;
50 int i;
51
52 pa_msleep(1000);
53
54 for (i = 0;; i++) {
55 p = pa_asyncq_pop(q, true);
56
57 if (p == PA_UINT_TO_PTR(-1))
58 break;
59
60 fail_unless(p == PA_UINT_TO_PTR(i+1));
61
62 pa_log_debug("popped %i", i);
63 }
64
65 pa_log_debug("popped end");
66 }
67
START_TEST(asyncq_test)68 START_TEST (asyncq_test) {
69 pa_asyncq *q;
70 pa_thread *t1, *t2;
71
72 if (!getenv("MAKE_CHECK"))
73 pa_log_set_level(PA_LOG_DEBUG);
74
75 q = pa_asyncq_new(0);
76 fail_unless(q != NULL);
77
78 t1 = pa_thread_new("producer", producer, q);
79 fail_unless(t1 != NULL);
80 t2 = pa_thread_new("consumer", consumer, q);
81 fail_unless(t2 != NULL);
82
83 pa_thread_free(t1);
84 pa_thread_free(t2);
85
86 pa_asyncq_free(q, NULL);
87 }
88 END_TEST
89
main(int argc,char * argv[])90 int main(int argc, char *argv[]) {
91 int failed = 0;
92 Suite *s;
93 TCase *tc;
94 SRunner *sr;
95
96 s = suite_create("Async Queue");
97 tc = tcase_create("asyncq");
98 tcase_add_test(tc, asyncq_test);
99 suite_add_tcase(s, tc);
100
101 sr = srunner_create(s);
102 srunner_run_all(sr, CK_NORMAL);
103 failed = srunner_ntests_failed(sr);
104 srunner_free(sr);
105
106 return (failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
107 }
108