tests: fix spurious netclientclock test failures
[platform/upstream/gstreamer.git] / tests / check / libs / gstnetclientclock.c
1 /* GStreamer
2  * Copyright (C) 2005 Andy Wingo <wingo@pobox.com>
3  *
4  * gstnetclientclock.c: Unit test for the network client clock
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., 59 Temple Place - Suite 330,
19  * Boston, MA 02111-1307, USA.
20  */
21
22 #include <gst/check/gstcheck.h>
23 #include <gst/net/gstnet.h>
24
25 #include <unistd.h>
26
27 GST_START_TEST (test_instantiation)
28 {
29   GstClock *client, *local;
30
31   local = gst_system_clock_obtain ();
32   client = gst_net_client_clock_new (NULL, "127.0.0.1", 1234, GST_SECOND);
33   fail_unless (local != NULL, "failed to get system clock");
34   fail_unless (client != NULL, "failed to get network client clock");
35
36   /* one for gstreamer, one for us */
37   ASSERT_OBJECT_REFCOUNT (local, "system clock", 2);
38   ASSERT_OBJECT_REFCOUNT (client, "network client clock", 1);
39
40   gst_object_unref (client);
41
42   ASSERT_OBJECT_REFCOUNT (local, "system clock", 2);
43
44   gst_object_unref (local);
45 }
46
47 GST_END_TEST;
48
49 GST_START_TEST (test_functioning)
50 {
51   GstNetTimeProvider *ntp;
52   GstClock *client, *server;
53   GstClockTime basex, basey, rate_num, rate_denom;
54   GstClockTime servtime, clienttime;
55   gint port;
56
57   server = gst_system_clock_obtain ();
58   fail_unless (server != NULL, "failed to get system clock");
59
60   /* move the clock ahead 100 seconds */
61   gst_clock_get_calibration (server, &basex, &basey, &rate_num, &rate_denom);
62   basey += 100 * GST_SECOND;
63   gst_clock_set_calibration (server, basex, basey, rate_num, rate_denom);
64
65   ntp = gst_net_time_provider_new (server, "127.0.0.1", 0);
66   fail_unless (ntp != NULL, "failed to create network time provider");
67
68   g_object_get (ntp, "port", &port, NULL);
69
70   client = gst_net_client_clock_new (NULL, "127.0.0.1", port, GST_SECOND);
71   fail_unless (client != NULL, "failed to get network client clock");
72
73   g_object_get (client, "port", &port, NULL);
74
75   /* let the clocks synchronize */
76   g_usleep (G_USEC_PER_SEC);
77
78   servtime = gst_clock_get_time (server);
79   clienttime = gst_clock_get_time (client);
80
81   /* can't in general make a precise assertion here, because this depends on
82    * system load and a lot of things. however within half a second they should
83    * at least be within 1/10 of a second of each other... */
84   if (servtime > clienttime)
85     fail_unless (servtime - clienttime < 100 * GST_MSECOND,
86         "clocks not in sync (%" GST_TIME_FORMAT ")",
87         GST_TIME_ARGS (servtime - clienttime));
88   else
89     fail_unless (clienttime - servtime < 100 * GST_MSECOND,
90         "clocks not in sync (%" GST_TIME_FORMAT ")",
91         GST_TIME_ARGS (clienttime - servtime));
92
93   /*
94      g_print ("diff: %" GST_TIME_FORMAT,
95      GST_TIME_ARGS (servtime > clienttime ? servtime - clienttime
96      : clienttime - servtime));
97    */
98
99   /* one for gstreamer, one for ntp, one for us */
100   ASSERT_OBJECT_REFCOUNT (server, "system clock", 3);
101   ASSERT_OBJECT_REFCOUNT (client, "network client clock", 1);
102
103   gst_object_unref (ntp);
104
105   ASSERT_OBJECT_REFCOUNT (server, "system clock", 2);
106
107   gst_object_unref (client);
108   gst_object_unref (server);
109 }
110
111 GST_END_TEST;
112
113 static Suite *
114 gst_net_client_clock_suite (void)
115 {
116   Suite *s = suite_create ("GstNetClientClock");
117   TCase *tc_chain = tcase_create ("generic tests");
118
119   tcase_set_timeout (tc_chain, 0);
120
121   suite_add_tcase (s, tc_chain);
122   tcase_add_test (tc_chain, test_instantiation);
123   tcase_add_test (tc_chain, test_functioning);
124
125   return s;
126 }
127
128 GST_CHECK_MAIN (gst_net_client_clock);