]> code.delx.au - pulseaudio/blob - src/tests/lock-autospawn-test.c
sink, source: Add hooks for mute changes
[pulseaudio] / src / tests / lock-autospawn-test.c
1 /***
2 This file is part of PulseAudio.
3
4 Copyright 2008 Lennart Poettering
5
6 PulseAudio is free software; you can redistribute it and/or modify
7 it under the terms of the GNU Lesser General Public License as published
8 by the Free Software Foundation; either version 2.1 of the License,
9 or (at your option) any later version.
10
11 PulseAudio is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
15
16 You should have received a copy of the GNU Lesser General Public License
17 along with PulseAudio; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19 USA.
20 ***/
21
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25
26 #include <check.h>
27
28 #include <string.h>
29
30 #include <pulsecore/poll.h>
31 #include <pulsecore/macro.h>
32 #include <pulsecore/thread.h>
33 #include <pulsecore/lock-autospawn.h>
34 #include <pulse/util.h>
35
36 static void thread_func(void*k) {
37 fail_unless(pa_autospawn_lock_init() >= 0);
38
39 pa_log("%i, Trying to acquire lock.", PA_PTR_TO_INT(k));
40
41 fail_unless(pa_autospawn_lock_acquire(true) > 0);
42
43 pa_log("%i, Got the lock!, Sleeping for 5s", PA_PTR_TO_INT(k));
44
45 pa_msleep(5000);
46
47 pa_log("%i, Releasing", PA_PTR_TO_INT(k));
48
49 pa_autospawn_lock_release();
50
51 pa_autospawn_lock_done(false);
52 }
53
54 static void thread_func2(void *k) {
55 int fd;
56
57 fail_unless((fd = pa_autospawn_lock_init()) >= 0);
58
59 pa_log("%i, Trying to acquire lock.", PA_PTR_TO_INT(k));
60
61 for (;;) {
62 struct pollfd pollfd;
63 int j;
64
65 if ((j = pa_autospawn_lock_acquire(false)) > 0)
66 break;
67
68 fail_unless(j == 0);
69
70 memset(&pollfd, 0, sizeof(pollfd));
71 pollfd.fd = fd;
72 pollfd.events = POLLIN;
73
74 fail_unless(pa_poll(&pollfd, 1, -1) == 1);
75
76 pa_log("%i, woke up", PA_PTR_TO_INT(k));
77 }
78
79 pa_log("%i, Got the lock!, Sleeping for 5s", PA_PTR_TO_INT(k));
80
81 pa_msleep(5000);
82
83 pa_log("%i, Releasing", PA_PTR_TO_INT(k));
84
85 pa_autospawn_lock_release();
86
87 pa_autospawn_lock_done(false);
88 }
89
90 START_TEST (lockautospawn_test) {
91 pa_thread *a, *b, *c, *d;
92
93 pa_assert_se((a = pa_thread_new("test1", thread_func, PA_INT_TO_PTR(1))));
94 pa_assert_se((b = pa_thread_new("test2", thread_func2, PA_INT_TO_PTR(2))));
95 pa_assert_se((c = pa_thread_new("test3", thread_func2, PA_INT_TO_PTR(3))));
96 pa_assert_se((d = pa_thread_new("test4", thread_func, PA_INT_TO_PTR(4))));
97
98 pa_thread_join(a);
99 pa_thread_join(b);
100 pa_thread_join(c);
101 pa_thread_join(d);
102
103 pa_thread_free(a);
104 pa_thread_free(b);
105 pa_thread_free(c);
106 pa_thread_free(d);
107 }
108 END_TEST
109
110 int main(int argc, char *argv[]) {
111 int failed = 0;
112 Suite *s;
113 TCase *tc;
114 SRunner *sr;
115
116 s = suite_create("Lock Auto Spawn");
117 tc = tcase_create("lockautospawn");
118 tcase_add_test(tc, lockautospawn_test);
119 /* the default timeout is too small,
120 * set it to a reasonable large one.
121 */
122 tcase_set_timeout(tc, 60 * 60);
123 suite_add_tcase(s, tc);
124
125 sr = srunner_create(s);
126 srunner_run_all(sr, CK_NORMAL);
127 failed = srunner_ntests_failed(sr);
128 srunner_free(sr);
129
130 return (failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
131 }