Initial release including wifi display based on gst-rtsp-server-1.4.1
[platform/upstream/gst-rtsp-server.git] / tests / check / gst / threadpool.c
1 /* GStreamer
2  * unit tests for GstRTSPThreadPool
3  * Copyright (C) 2013 Axis Communications <dev-gstreamer at axis dot com>
4  * @author Ognyan Tonchev <ognyan at axis dot com>
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Library General Public
8  * License as published by the Free Software Foundation; either
9  * version 2 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Library General Public License for more details.
15  *
16  * You should have received a copy of the GNU Library General Public
17  * License along with this library; if not, write to the
18  * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
19  * Boston, MA 02110-1301, USA.
20  */
21
22 #include <gst/check/gstcheck.h>
23
24 #include <rtsp-thread-pool.h>
25
26 GST_START_TEST (test_pool_get_thread)
27 {
28   GstRTSPThreadPool *pool;
29   GstRTSPThread *thread;
30
31   pool = gst_rtsp_thread_pool_new ();
32   fail_unless (GST_IS_RTSP_THREAD_POOL (pool));
33
34   thread = gst_rtsp_thread_pool_get_thread (pool, GST_RTSP_THREAD_TYPE_CLIENT,
35       NULL);
36   fail_unless (GST_IS_RTSP_THREAD (thread));
37   /* one ref is hold by the pool */
38   fail_unless_equals_int (GST_MINI_OBJECT_REFCOUNT (thread), 2);
39
40   gst_rtsp_thread_stop (thread);
41   g_object_unref (pool);
42   gst_rtsp_thread_pool_cleanup ();
43 }
44
45 GST_END_TEST;
46
47 GST_START_TEST (test_pool_get_media_thread)
48 {
49   GstRTSPThreadPool *pool;
50   GstRTSPThread *thread;
51
52   pool = gst_rtsp_thread_pool_new ();
53   fail_unless (GST_IS_RTSP_THREAD_POOL (pool));
54
55   thread = gst_rtsp_thread_pool_get_thread (pool, GST_RTSP_THREAD_TYPE_MEDIA,
56       NULL);
57   fail_unless (GST_IS_RTSP_THREAD (thread));
58   /* one ref is hold by the pool */
59   fail_unless_equals_int (GST_MINI_OBJECT_REFCOUNT (thread), 2);
60
61   gst_rtsp_thread_stop (thread);
62   g_object_unref (pool);
63   gst_rtsp_thread_pool_cleanup ();
64 }
65
66 GST_END_TEST;
67
68 GST_START_TEST (test_pool_get_thread_reuse)
69 {
70   GstRTSPThreadPool *pool;
71   GstRTSPThread *thread1;
72   GstRTSPThread *thread2;
73
74   pool = gst_rtsp_thread_pool_new ();
75   fail_unless (GST_IS_RTSP_THREAD_POOL (pool));
76
77   gst_rtsp_thread_pool_set_max_threads (pool, 1);
78
79   thread1 = gst_rtsp_thread_pool_get_thread (pool, GST_RTSP_THREAD_TYPE_CLIENT,
80       NULL);
81   fail_unless (GST_IS_RTSP_THREAD (thread1));
82
83   thread2 = gst_rtsp_thread_pool_get_thread (pool, GST_RTSP_THREAD_TYPE_CLIENT,
84       NULL);
85   fail_unless (GST_IS_RTSP_THREAD (thread2));
86
87   fail_unless (thread2 == thread1);
88   /* one ref is hold by the pool */
89   fail_unless_equals_int (GST_MINI_OBJECT_REFCOUNT (thread1), 3);
90
91   gst_rtsp_thread_stop (thread1);
92   gst_rtsp_thread_stop (thread2);
93   g_object_unref (pool);
94
95   gst_rtsp_thread_pool_cleanup ();
96 }
97
98 GST_END_TEST;
99
100 static void
101 do_test_pool_max_thread (gboolean use_property)
102 {
103   GstRTSPThreadPool *pool;
104   GstRTSPThread *thread1;
105   GstRTSPThread *thread2;
106   GstRTSPThread *thread3;
107   gint max_threads;
108
109   pool = gst_rtsp_thread_pool_new ();
110   fail_unless (GST_IS_RTSP_THREAD_POOL (pool));
111
112   if (use_property) {
113     g_object_get (pool, "max-threads", &max_threads, NULL);
114     fail_unless_equals_int (max_threads, 1);
115   } else {
116     fail_unless_equals_int (gst_rtsp_thread_pool_get_max_threads (pool), 1);
117   }
118
119   thread1 = gst_rtsp_thread_pool_get_thread (pool, GST_RTSP_THREAD_TYPE_CLIENT,
120       NULL);
121   fail_unless (GST_IS_RTSP_THREAD (thread1));
122
123   thread2 = gst_rtsp_thread_pool_get_thread (pool, GST_RTSP_THREAD_TYPE_CLIENT,
124       NULL);
125   fail_unless (GST_IS_RTSP_THREAD (thread2));
126
127   fail_unless (thread1 == thread2);
128
129   gst_rtsp_thread_stop (thread1);
130   gst_rtsp_thread_stop (thread2);
131
132   if (use_property) {
133     g_object_set (pool, "max-threads", 2, NULL);
134     g_object_get (pool, "max-threads", &max_threads, NULL);
135     fail_unless_equals_int (max_threads, 2);
136   } else {
137     gst_rtsp_thread_pool_set_max_threads (pool, 2);
138     fail_unless_equals_int (gst_rtsp_thread_pool_get_max_threads (pool), 2);
139   }
140
141   thread1 = gst_rtsp_thread_pool_get_thread (pool, GST_RTSP_THREAD_TYPE_CLIENT,
142       NULL);
143   fail_unless (GST_IS_RTSP_THREAD (thread1));
144
145   thread2 = gst_rtsp_thread_pool_get_thread (pool, GST_RTSP_THREAD_TYPE_CLIENT,
146       NULL);
147   fail_unless (GST_IS_RTSP_THREAD (thread2));
148
149   thread3 = gst_rtsp_thread_pool_get_thread (pool, GST_RTSP_THREAD_TYPE_CLIENT,
150       NULL);
151   fail_unless (GST_IS_RTSP_THREAD (thread3));
152
153   fail_unless (thread2 != thread1);
154   fail_unless (thread3 == thread2 || thread3 == thread1);
155
156   gst_rtsp_thread_stop (thread1);
157   gst_rtsp_thread_stop (thread2);
158   gst_rtsp_thread_stop (thread3);
159
160   if (use_property) {
161     g_object_set (pool, "max-threads", 0, NULL);
162     g_object_get (pool, "max-threads", &max_threads, NULL);
163     fail_unless_equals_int (max_threads, 0);
164   } else {
165     gst_rtsp_thread_pool_set_max_threads (pool, 0);
166     fail_unless_equals_int (gst_rtsp_thread_pool_get_max_threads (pool), 0);
167   }
168
169   thread1 = gst_rtsp_thread_pool_get_thread (pool, GST_RTSP_THREAD_TYPE_CLIENT,
170       NULL);
171   fail_if (GST_IS_RTSP_THREAD (thread1));
172
173   g_object_unref (pool);
174
175   gst_rtsp_thread_pool_cleanup ();
176 }
177
178 GST_START_TEST (test_pool_max_threads)
179 {
180   do_test_pool_max_thread (FALSE);
181 }
182
183 GST_END_TEST;
184
185 GST_START_TEST (test_pool_max_threads_property)
186 {
187   do_test_pool_max_thread (TRUE);
188 }
189
190 GST_END_TEST;
191
192 GST_START_TEST (test_pool_thread_copy)
193 {
194   GstRTSPThreadPool *pool;
195   GstRTSPThread *thread1;
196   GstRTSPThread *thread2;
197
198   pool = gst_rtsp_thread_pool_new ();
199   fail_unless (GST_IS_RTSP_THREAD_POOL (pool));
200
201   thread1 = gst_rtsp_thread_pool_get_thread (pool, GST_RTSP_THREAD_TYPE_CLIENT,
202       NULL);
203   fail_unless (GST_IS_RTSP_THREAD (thread1));
204   fail_unless (GST_IS_MINI_OBJECT_TYPE (thread1, GST_TYPE_RTSP_THREAD));
205
206   thread2 = GST_RTSP_THREAD (gst_mini_object_copy (GST_MINI_OBJECT (thread1)));
207   fail_unless (GST_IS_RTSP_THREAD (thread2));
208   fail_unless (GST_IS_MINI_OBJECT_TYPE (thread2, GST_TYPE_RTSP_THREAD));
209
210   gst_rtsp_thread_stop (thread1);
211   gst_rtsp_thread_stop (thread2);
212   g_object_unref (pool);
213   gst_rtsp_thread_pool_cleanup ();
214 }
215
216 GST_END_TEST;
217
218 static Suite *
219 rtspthreadpool_suite (void)
220 {
221   Suite *s = suite_create ("rtspthreadpool");
222   TCase *tc = tcase_create ("general");
223
224   suite_add_tcase (s, tc);
225   tcase_set_timeout (tc, 20);
226   tcase_add_test (tc, test_pool_get_thread);
227   tcase_add_test (tc, test_pool_get_media_thread);
228   tcase_add_test (tc, test_pool_get_thread_reuse);
229   tcase_add_test (tc, test_pool_max_threads);
230   tcase_add_test (tc, test_pool_max_threads_property);
231   tcase_add_test (tc, test_pool_thread_copy);
232
233   return s;
234 }
235
236 GST_CHECK_MAIN (rtspthreadpool);