sink, source: Really set the fixed latency in set_fixed_latency_within_thread(),...
[platform/upstream/pulseaudio.git] / src / tests / thread-mainloop-test.c
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, write to the Free Software
16   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
17   USA.
18 ***/
19
20 #ifdef HAVE_CONFIG_H
21 #include <config.h>
22 #endif
23
24 #include <stdlib.h>
25 #include <unistd.h>
26 #include <stdio.h>
27
28 #include <check.h>
29
30 #include <pulse/rtclock.h>
31 #include <pulse/timeval.h>
32 #include <pulse/util.h>
33 #include <pulse/thread-mainloop.h>
34
35 #include <pulsecore/macro.h>
36 #include <pulsecore/core-rtclock.h>
37
38 static void tcb(pa_mainloop_api *a, pa_time_event *e, const struct timeval *tv, void *userdata) {
39     pa_assert_se(pa_threaded_mainloop_in_thread(userdata));
40     fprintf(stderr, "TIME EVENT START\n");
41     pa_threaded_mainloop_signal(userdata, 1);
42     fprintf(stderr, "TIME EVENT END\n");
43 }
44
45 START_TEST (thread_mainloop_test) {
46     pa_mainloop_api *a;
47     pa_threaded_mainloop *m;
48     struct timeval tv;
49
50     m = pa_threaded_mainloop_new();
51     fail_unless(m != NULL);
52     a = pa_threaded_mainloop_get_api(m);
53     fail_unless(m != NULL);
54
55     fail_unless(pa_threaded_mainloop_start(m) >= 0);
56
57     pa_threaded_mainloop_lock(m);
58
59     fail_unless(!pa_threaded_mainloop_in_thread(m));
60
61     a->time_new(a, pa_timeval_rtstore(&tv, pa_rtclock_now() + 5 * PA_USEC_PER_SEC, TRUE), tcb, m);
62
63     fprintf(stderr, "waiting 5s (signal)\n");
64     pa_threaded_mainloop_wait(m);
65     fprintf(stderr, "wait completed\n");
66     pa_threaded_mainloop_accept(m);
67     fprintf(stderr, "signal accepted\n");
68
69     pa_threaded_mainloop_unlock(m);
70
71     fprintf(stderr, "waiting 5s (sleep)\n");
72     pa_msleep(5000);
73
74     pa_threaded_mainloop_stop(m);
75
76     pa_threaded_mainloop_free(m);
77 }
78 END_TEST
79
80 int main(int argc, char *argv[]) {
81     int failed = 0;
82     Suite *s;
83     TCase *tc;
84     SRunner *sr;
85
86     s = suite_create("Thread MainLoop");
87     tc = tcase_create("threadmainloop");
88     tcase_add_test(tc, thread_mainloop_test);
89     /* the default timeout is too small,
90      * set it to a reasonable large one.
91      */
92     tcase_set_timeout(tc, 60 * 60);
93     suite_add_tcase(s, tc);
94
95     sr = srunner_create(s);
96     srunner_run_all(sr, CK_NORMAL);
97     failed = srunner_ntests_failed(sr);
98     srunner_free(sr);
99
100     return (failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
101 }