]> Gitweb @ Texas Instruments - Open Source Git Repositories - git.TI.com/gitweb - glsdk/gstreamer0-10.git/blob - tests/check/gst/gstobject.c
tests/check/gst/gstobject.c: Disable silly racy test that always fails on this combin...
[glsdk/gstreamer0-10.git] / tests / check / gst / gstobject.c
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 <gst/check/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 GST_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 GST_END_TEST
79 /* g_object_new on GstFakeObject should succeed */
80 GST_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   gst_object_unref (object);
89 }
91 GST_END_TEST
92 /* GstFakeObject name tests */
93 GST_START_TEST (test_fake_object_name)
94 {
95   GstObject *object;
96   gchar *name;
97   gchar *name2;
99   object = g_object_new (gst_fake_object_get_type (), NULL);
101   name = gst_object_get_name (object);
102   fail_if (name == NULL, "Newly created object has no name");
103   fail_if (strncmp (name, "fakeobject", 10) != 0,
104       "Random name %s does not start with Gst", name);
105   g_free (name);
107   /* give a random name by setting with NULL;
108    * GstFakeObject class -> fakeobject%d */
109   gst_object_set_name (object, NULL);
110   name = gst_object_get_name (object);
111   fail_if (name == NULL, "Random name was not assigned");
112   fail_if (strncmp (name, "fakeobject", 10) != 0,
113       "Random name %s does not start with Gst", name);
114   g_free (name);
116   gst_object_set_name (object, "fake");
117   name = gst_object_get_name (object);
118   fail_if (name == NULL, "Failed to get name of GstFakeObject");
119   fail_if (strcmp (name, "fake") != 0, "Name of GstFakeObject is not 'fake'");
121   /* change the gotten name to see that it's a copy and not the original */
122   name[0] = 'm';
123   name2 = gst_object_get_name (object);
124   fail_if (strcmp (name2, "fake") != 0,
125       "Copy of object name affected actual object name");
126   g_free (name);
127   g_free (name2);
129   gst_object_unref (object);
132 GST_END_TEST
133 /* thread function for threaded name change test */
134     gpointer thread_name_object (GstObject * object)
136   gchar *thread_id = g_strdup_printf ("%p", g_thread_self ());
138   THREAD_START ();
140   /* give main thread a head start */
141   g_usleep (100000);
143   /* write our name repeatedly */
144   g_message ("THREAD %s: starting loop\n", thread_id);
145   while (THREAD_TEST_RUNNING ()) {
146     gst_object_set_name (object, thread_id);
147     /* a minimal sleep invokes a thread switch */
148     THREAD_SWITCH ();
149   }
151   /* thread is done, so let's return */
152   g_message ("THREAD %s: set name\n", thread_id);
153   g_free (thread_id);
155   return NULL;
158 #if 0
159 GST_START_TEST (test_fake_object_name_threaded_wrong)
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   gst_object_unref (object);
190   fail_unless (expected_failure, "name did not get changed");
193 GST_END_TEST;
194 #endif
196 /*
197  * main thread sets and gets name directly on struct inside the object lock
198  * succeed because lock is held during set/get, and threads are locked out
199  */
200 GST_START_TEST (test_fake_object_name_threaded_right)
202   GstObject *object;
203   gchar *name;
204   gint i;
206   g_message ("\nTEST: set/get inside lock\n");
208   object = g_object_new (gst_fake_object_get_type (), NULL);
209   gst_object_set_name (object, "main");
211   MAIN_START_THREADS (5, thread_name_object, object);
213   /* start looping and set/get name repeatedly */
214   for (i = 0; i < 1000; ++i) {
215     GST_OBJECT_LOCK (object);
216     g_free (GST_OBJECT_NAME (object));
217     GST_OBJECT_NAME (object) = g_strdup ("main");
218     THREAD_SWITCH ();
219     name = g_strdup (GST_OBJECT_NAME (object));
220     GST_OBJECT_UNLOCK (object);
222     fail_unless (strcmp (name, "main") == 0,
223         "Name got changed while lock held during run %d", i);
224     g_free (name);
225   }
226   MAIN_STOP_THREADS ();
227   gst_object_unref (object);
230 GST_END_TEST
231 /*
232  * main thread creates lots of objects
233  * child threads sets default names on objects
234  * then main thread checks uniqueness of object names
235  */
236     GList * object_list = NULL;
237 gint num_objects = 1000;
238 gint num_threads = 5;
240 /* thread function for threaded default name change test */
241 gpointer
242 thread_name_object_default (int *i)
244   int j;
246   THREAD_START ();
248   for (j = *i; j < num_objects; j += num_threads) {
249     GstObject *o = GST_OBJECT (g_list_nth_data (object_list, j));
251     /* g_message ("THREAD %p: setting default name on object %d\n",
252        g_thread_self (), j); */
253     gst_object_set_name (o, NULL);
254     THREAD_SWITCH ();
255   }
257   /* thread is done, so let's return */
258   g_message ("THREAD %p: set name\n", g_thread_self ());
259   g_free (i);
261   return NULL;
264 static gint
265 gst_object_name_compare (GstObject * o, GstObject * p)
267   gint result;
269   GST_OBJECT_LOCK (o);
270   GST_OBJECT_LOCK (p);
272   if (o->name == NULL && p->name == NULL) {
273     result = 0;
274   } else if (o->name == NULL) {
275     result = -1;
276   } else if (p->name == NULL) {
277     result = 1;
278   } else {
279     result = strcmp (o->name, p->name);
280   }
282   GST_OBJECT_UNLOCK (p);
283   GST_OBJECT_UNLOCK (o);
285   return result;
288 GST_START_TEST (test_fake_object_name_threaded_unique)
290   GstObject *object;
291   gint i;
292   gint *ip;
293   gchar *name1, *name2;
294   GList *l;
296   g_message ("\nTEST: uniqueness of default names\n");
298   for (i = 0; i < num_objects; ++i) {
299     object = g_object_new (gst_fake_object_get_type (), NULL);
300     object_list = g_list_append (object_list, object);
301   }
303   MAIN_INIT ();
305   mark_point ();
306   for (i = 0; i < num_threads; ++i) {
307     ip = g_new (gint, 1);
308     *ip = i;
309     MAIN_START_THREAD_FUNCTION (i, thread_name_object_default, ip);
310   }
312   mark_point ();
313   MAIN_SYNCHRONIZE ();
314   mark_point ();
315   MAIN_STOP_THREADS ();
317   /* sort GList based on object name */
318   /* FIXME: sort and test */
319   object_list =
320       g_list_sort (object_list, (GCompareFunc) gst_object_name_compare);
322   name1 = gst_object_get_name (GST_OBJECT (object_list->data));
323   for (l = object_list->next; l->next; l = l->next) {
324     g_message ("object with name %s\n", name1);
325     name2 = gst_object_get_name (GST_OBJECT (l->data));
326     fail_if (strcmp (name1, name2) == 0, "Two objects with name %s", name2);
327     g_free (name1);
328     name1 = name2;
329   }
330   g_free (name1);
332   /* free stuff */
333   g_list_foreach (object_list, (GFunc) g_object_unref, NULL);
336 GST_END_TEST
337 /* parentage test on GstFakeObject */
338 GST_START_TEST (test_fake_object_parentage)
340   GstObject *object1, *object2;
341   GstObject *parent;
342   gboolean result;
344   /* create new object */
345   object1 = g_object_new (gst_fake_object_get_type (), NULL);
346   fail_if (object1 == NULL, "Failed to create instance of GstFakeObject");
347   fail_unless (GST_IS_OBJECT (object1),
348       "GstFakeObject instance is not a GstObject");
349   fail_unless (GST_OBJECT_IS_FLOATING (object1),
350       "GstFakeObject instance is not floating");
352   /* check the parent */
353   parent = gst_object_get_parent (object1);
354   fail_if (parent != NULL, "GstFakeObject has parent");
355   /* try to set a NULL parent, this should give a warning */
356   ASSERT_CRITICAL (result = gst_object_set_parent (object1, NULL));
357   fail_if (result == TRUE, "GstFakeObject accepted NULL parent");
358   /* try to set itself as parent, we expect a warning here */
359   ASSERT_CRITICAL (result = gst_object_set_parent (object1, object1));
360   fail_if (result == TRUE, "GstFakeObject accepted itself as parent");
362   /* should still be floating */
363   fail_unless (GST_OBJECT_IS_FLOATING (object1),
364       "GstFakeObject instance is not floating");
366   /* create another object */
367   object2 = g_object_new (gst_fake_object_get_type (), NULL);
368   fail_if (object2 == NULL,
369       "Failed to create another instance of GstFakeObject");
370   fail_unless (GST_IS_OBJECT (object2),
371       "second GstFakeObject instance is not a GstObject");
372   fail_unless (GST_OBJECT_IS_FLOATING (object1),
373       "GstFakeObject instance is not floating");
375   /* try to set other object as parent */
376   result = gst_object_set_parent (object1, object2);
377   fail_if (result == FALSE,
378       "GstFakeObject could not accept other object as parent");
380   /* should not be floating anymore */
381   fail_if (GST_OBJECT_IS_FLOATING (object1),
382       "GstFakeObject instance is still floating");
383   /* parent should still be floating */
384   fail_unless (GST_OBJECT_IS_FLOATING (object2),
385       "GstFakeObject instance is not floating");
387   /* check the parent */
388   parent = gst_object_get_parent (object1);
389   fail_if (parent != object2, "GstFakeObject has wrong parent");
390   gst_object_unref (parent);
391   /* try to set other object as parent again */
392   result = gst_object_set_parent (object1, object2);
393   fail_if (result == TRUE, "GstFakeObject could set parent twice");
395   /* ref before unparenting */
396   gst_object_ref (object1);
397   /* clear parent of object */
398   gst_object_unparent (object1);
400   /* check the parent */
401   parent = gst_object_get_parent (object1);
402   fail_if (parent != NULL, "GstFakeObject has parent");
404   /* object should not be floating */
405   fail_if (GST_OBJECT_IS_FLOATING (object1),
406       "GstFakeObject instance is floating again");
408   gst_object_unref (object1);
409   gst_object_unref (object2);
412 GST_END_TEST
413 /* parentage test dispose on GstFakeObject, since our testcase
414  * does not handle the parent relation completely, the parent does
415  * not hold a ref to the child, we cannot dispose the parent to
416  * dipose the child as well. This test needs to be run with DEBUG
417  * info to check if the finalize methods are called correctly. */
418 GST_START_TEST (test_fake_object_parentage_dispose)
420   GstObject *object1, *object2;
421   gboolean result;
423   object1 = g_object_new (gst_fake_object_get_type (), NULL);
424   fail_if (object1 == NULL, "Failed to create instance of GstFakeObject");
426   object2 = g_object_new (gst_fake_object_get_type (), NULL);
427   fail_if (object2 == NULL, "Failed to create instance of GstFakeObject");
429   /* try to set other object as parent */
430   result = gst_object_set_parent (object1, object2);
431   fail_if (result == FALSE,
432       "GstFakeObject could not accept other object as parent");
434   /* clear parent of object */
435   gst_object_unparent (object1);
437   /* now dispose parent */
438   gst_object_unref (object2);
441 GST_END_TEST
442 /* test: try renaming a parented object, make sure it fails */
443     Suite * gst_object_suite (void)
445   Suite *s = suite_create ("GstObject");
446   TCase *tc_chain = tcase_create ("general");
448   /* turn off timeout */
449   tcase_set_timeout (tc_chain, 60);
451   suite_add_tcase (s, tc_chain);
452   tcase_add_test (tc_chain, test_fake_object_new);
453   tcase_add_test (tc_chain, test_fake_object_name);
454 #if 0
455   tcase_add_test (tc_chain, test_fake_object_name_threaded_wrong);
456 #endif
457   tcase_add_test (tc_chain, test_fake_object_name_threaded_right);
458   tcase_add_test (tc_chain, test_fake_object_name_threaded_unique);
459   tcase_add_test (tc_chain, test_fake_object_parentage);
460   tcase_add_test (tc_chain, test_fake_object_parentage_dispose);
461   //tcase_add_checked_fixture (tc_chain, setup, teardown);
463   /* SEGV tests go last so we can debug the others */
464   tcase_add_test_raise_signal (tc_chain, test_fail_abstract_new, SIGSEGV);
465   return s;
468 GST_CHECK_MAIN (gst_object);