1 /* GStreamer
2 *
3 * unit test for GstObject
4 *
5 * Copyright (C) <2004> Thomas Vander Stichele <thomas at apestaart dot org>
6 *
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Library General Public
9 * License as published by the Free Software Foundation; either
10 * version 2 of the License, or (at your option) any later version.
11 *
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Library General Public License for more details.
16 *
17 * You should have received a copy of the GNU Library General Public
18 * License along with this library; if not, write to the
19 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 * Boston, MA 02111-1307, USA.
21 */
23 #include "../gstcheck.h"
25 /*
26 Create a fake subclass
27 */
28 typedef struct _GstFakeObjectClass GstFakeObjectClass;
29 typedef struct _GstFakeObject GstFakeObject;
31 struct _GstFakeObject
32 {
33 GstObject object;
34 };
36 struct _GstFakeObjectClass
37 {
38 GstObjectClass parent_class;
39 };
41 GType _gst_fake_object_type = 0;
43 //static GstObjectClass *parent_class = NULL;
44 //static guint gst_fake_object_signals[LAST_SIGNAL] = { 0 };
46 GType
47 gst_fake_object_get_type (void)
48 {
49 if (!_gst_fake_object_type) {
50 static const GTypeInfo fake_object_info = {
51 sizeof (GstFakeObjectClass),
52 NULL, //gst_fake_object_base_class_init,
53 NULL, //gst_fake_object_base_class_finalize,
54 NULL, //(GClassInitFunc) gst_fake_object_class_init,
55 NULL,
56 NULL,
57 sizeof (GstFakeObject),
58 0,
59 NULL, //(GInstanceInitFunc) gst_fake_object_init,
60 NULL
61 };
63 _gst_fake_object_type = g_type_register_static (GST_TYPE_OBJECT,
64 "GstFakeObject", &fake_object_info, 0);
65 }
66 return _gst_fake_object_type;
67 }
69 /* g_object_new on abstract GstObject should fail */
70 START_TEST (test_fail_abstract_new)
71 {
72 GstObject *object;
74 ASSERT_CRITICAL (object = g_object_new (gst_object_get_type (), NULL));
75 fail_unless (object == NULL, "Created an instance of abstract GstObject");
76 }
78 END_TEST
79 /* g_object_new on GstFakeObject should succeed */
80 START_TEST (test_fake_object_new)
81 {
82 GstObject *object;
84 object = g_object_new (gst_fake_object_get_type (), NULL);
85 fail_if (object == NULL, "Failed to create instance of GstFakeObject");
86 fail_unless (GST_IS_OBJECT (object),
87 "GstFakeObject instance is not a GstObject");
88 }
90 END_TEST
91 /* GstFakeObject name tests */
92 START_TEST (test_fake_object_name)
93 {
94 GstObject *object;
95 gchar *name;
96 gchar *name2;
98 object = g_object_new (gst_fake_object_get_type (), NULL);
100 name = gst_object_get_name (object);
101 fail_if (name == NULL, "Newly created object has no name");
102 fail_if (strncmp (name, "fakeobject", 10) != 0,
103 "Random name %s does not start with Gst", name);
105 /* give a random name by setting with NULL;
106 * GstFakeObject class -> fakeobject%d */
107 gst_object_set_name (object, NULL);
108 name = gst_object_get_name (object);
109 fail_if (name == NULL, "Random name was not assigned");
110 fail_if (strncmp (name, "fakeobject", 10) != 0,
111 "Random name %s does not start with Gst", name);
112 g_free (name);
114 gst_object_set_name (object, "fake");
115 name = gst_object_get_name (object);
116 fail_if (name == NULL, "Failed to get name of GstFakeObject");
117 fail_if (strcmp (name, "fake") != 0, "Name of GstFakeObject is not 'fake'");
119 /* change the gotten name to see that it's a copy and not the original */
120 name[0] = 'm';
121 name2 = gst_object_get_name (object);
122 fail_if (strcmp (name2, "fake") != 0,
123 "Copy of object name affected actual object name");
124 g_free (name);
125 g_free (name2);
126 }
128 END_TEST
129 /* thread function for threaded name change test */
130 gpointer thread_name_object (GstObject * object)
131 {
132 gchar *thread_id = g_strdup_printf ("%p", g_thread_self ());
134 THREAD_START ();
136 /* give main thread a head start */
137 g_usleep (100000);
139 /* write our name repeatedly */
140 g_message ("THREAD %s: starting loop\n", thread_id);
141 while (THREAD_TEST_RUNNING ()) {
142 gst_object_set_name (object, thread_id);
143 /* a minimal sleep invokes a thread switch */
144 THREAD_SWITCH ();
145 }
147 /* thread is done, so let's return */
148 g_message ("THREAD %s: set name\n", thread_id);
149 g_free (thread_id);
151 return NULL;
152 }
154 /*
155 * main thread sets and gets name while other threads set the name
156 * constantly; fails because lock is released inbetween set and get
157 */
159 START_TEST (test_fake_object_name_threaded_wrong)
160 {
161 GstObject *object;
162 gchar *name;
163 gint i;
164 gboolean expected_failure = FALSE;
166 g_message ("\nTEST: set/get without lock\n");
168 object = g_object_new (gst_fake_object_get_type (), NULL);
169 gst_object_set_name (object, "main");
171 MAIN_START_THREADS (5, thread_name_object, object);
173 /* start looping and set/get name repeatedly */
174 for (i = 0; i < 1000; ++i) {
175 gst_object_set_name (object, "main");
176 THREAD_SWITCH ();
177 name = gst_object_get_name (object);
178 if (strcmp (name, "main") != 0) {
179 g_message ("MAIN: expected failure during run %d\n", i);
180 expected_failure = TRUE;
181 g_free (name);
182 break;
183 }
184 g_free (name);
185 }
186 MAIN_STOP_THREADS ();
188 fail_unless (expected_failure, "name did not get changed");
189 }
191 END_TEST
192 /*
193 * main thread sets and gets name directly on struct inside the object lock
194 * succeed because lock is held during set/get, and threads are locked out
195 */
196 START_TEST (test_fake_object_name_threaded_right)
197 {
198 GstObject *object;
199 gchar *name;
200 gint i;
202 g_message ("\nTEST: set/get inside lock\n");
204 object = g_object_new (gst_fake_object_get_type (), NULL);
205 gst_object_set_name (object, "main");
207 MAIN_START_THREADS (5, thread_name_object, object);
209 /* start looping and set/get name repeatedly */
210 for (i = 0; i < 1000; ++i) {
211 GST_LOCK (object);
212 g_free (GST_OBJECT_NAME (object));
213 GST_OBJECT_NAME (object) = g_strdup ("main");
214 THREAD_SWITCH ();
215 name = g_strdup (GST_OBJECT_NAME (object));
216 GST_UNLOCK (object);
218 fail_unless (strcmp (name, "main") == 0,
219 "Name got changed while lock held during run %d", i);
220 g_free (name);
221 }
222 MAIN_STOP_THREADS ();
223 }
225 END_TEST
226 /*
227 * main thread creates lots of objects
228 * child threads sets default names on objects
229 * then main thread checks uniqueness of object names
230 */
231 GList * object_list = NULL;
232 gint num_objects = 1000;
233 gint num_threads = 5;
235 /* thread function for threaded default name change test */
236 gpointer
237 thread_name_object_default (int *i)
238 {
239 int j;
241 THREAD_START ();
243 for (j = *i; j < num_objects; j += num_threads) {
244 GstObject *o = GST_OBJECT (g_list_nth_data (object_list, j));
246 /* g_message ("THREAD %p: setting default name on object %d\n",
247 g_thread_self (), j); */
248 gst_object_set_name (o, NULL);
249 THREAD_SWITCH ();
250 }
252 /* thread is done, so let's return */
253 g_message ("THREAD %p: set name\n", g_thread_self ());
254 g_free (i);
256 return NULL;
257 }
259 static gint
260 gst_object_name_compare (GstObject * o, GstObject * p)
261 {
262 gint result;
264 GST_LOCK (o);
265 GST_LOCK (p);
267 if (o->name == NULL && p->name == NULL) {
268 result = 0;
269 } else if (o->name == NULL) {
270 result = -1;
271 } else if (p->name == NULL) {
272 result = 1;
273 } else {
274 result = strcmp (o->name, p->name);
275 }
277 GST_UNLOCK (p);
278 GST_UNLOCK (o);
280 return result;
281 }
283 START_TEST (test_fake_object_name_threaded_unique)
284 {
285 GstObject *object;
286 gint i;
287 gint *ip;
288 gchar *name1, *name2;
289 GList *l;
291 g_message ("\nTEST: uniqueness of default names\n");
293 for (i = 0; i < num_objects; ++i) {
294 object = g_object_new (gst_fake_object_get_type (), NULL);
295 object_list = g_list_append (object_list, object);
296 }
298 MAIN_INIT ();
300 mark_point ();
301 for (i = 0; i < num_threads; ++i) {
302 ip = g_new (gint, 1);
303 *ip = i;
304 MAIN_START_THREAD_FUNCTION (i, thread_name_object_default, ip);
305 }
307 mark_point ();
308 MAIN_SYNCHRONIZE ();
309 mark_point ();
310 MAIN_STOP_THREADS ();
312 /* sort GList based on object name */
313 /* FIXME: sort and test */
314 g_list_sort (object_list, (GCompareFunc) gst_object_name_compare);
316 name1 = gst_object_get_name (GST_OBJECT (object_list->data));
317 for (l = object_list->next; l->next; l = l->next) {
318 g_message ("object with name %s\n", name1);
319 name2 = gst_object_get_name (GST_OBJECT (l->data));
320 fail_if (strcmp (name1, name2) == 0, "Two objects with name %s", name2);
321 g_free (name1);
322 name1 = name2;
323 }
325 /* free stuff */
326 g_list_foreach (object_list, (GFunc) g_object_unref, NULL);
327 }
329 END_TEST
330 /* test: try renaming a parented object, make sure it fails */
331 Suite * gst_object_suite (void)
332 {
333 Suite *s = suite_create ("GstObject");
334 TCase *tc_chain = tcase_create ("general");
336 suite_add_tcase (s, tc_chain);
337 tcase_add_test (tc_chain, test_fake_object_new);
338 tcase_add_test (tc_chain, test_fake_object_name);
339 tcase_add_test (tc_chain, test_fake_object_name_threaded_wrong);
340 tcase_add_test (tc_chain, test_fake_object_name_threaded_right);
341 tcase_add_test (tc_chain, test_fake_object_name_threaded_unique);
342 //tcase_add_checked_fixture (tc_chain, setup, teardown);
344 /* SEGV tests go last so we can debug the others */
345 tcase_add_test_raise_signal (tc_chain, test_fail_abstract_new, SIGSEGV);
346 return s;
347 }
349 int
350 main (int argc, char **argv)
351 {
352 int nf;
354 Suite *s = gst_object_suite ();
355 SRunner *sr = srunner_create (s);
357 gst_check_init (&argc, &argv);
359 srunner_run_all (sr, CK_NORMAL);
360 nf = srunner_ntests_failed (sr);
361 srunner_free (sr);
363 return nf;
364 }