tests: Fix horribly raciness in test-new-dribble
authorOlivier Crête <olivier.crete@collabora.com>
Tue, 25 Feb 2014 05:23:47 +0000 (00:23 -0500)
committerOlivier Crête <olivier.crete@collabora.com>
Tue, 25 Feb 2014 05:51:31 +0000 (00:51 -0500)
tests/test-new-dribble.c

index 6c83091..c6b2d8b 100644 (file)
   static GCond *stun_thread_signal_ptr = &stun_thread_signal;
 #endif
 
-static GMainLoop *global_mainloop;
 static NiceComponentState global_lagent_state = NICE_COMPONENT_STATE_LAST;
 static NiceComponentState global_ragent_state = NICE_COMPONENT_STATE_LAST;
+static GCancellable *global_cancellable;
 static gboolean exit_stun_thread = FALSE;
 static gboolean lagent_candidate_gathering_done = FALSE;
 static gboolean ragent_candidate_gathering_done = FALSE;
 static guint global_ls_id, global_rs_id;
 static gboolean data_received = FALSE;
 static gboolean drop_stun_packets = FALSE;
+static gboolean got_stun_packet = FALSE;
+static gboolean send_stun = FALSE;
 
 static const uint16_t known_attributes[] =  {
   0
@@ -204,10 +206,11 @@ recv_packet:
   buf_len = stun_agent_finish_message (agent, &response, NULL, 0);
 
 send_buf:
-  g_main_loop_quit (global_mainloop);
+  g_cancellable_cancel (global_cancellable);
   g_debug ("Ready to send a STUN response");
   g_assert (g_mutex_trylock (stun_mutex_ptr));
-  while (global_lagent_state < NICE_COMPONENT_STATE_CONNECTING) {
+  got_stun_packet = TRUE;
+  while (send_stun) {
     g_debug ("Waiting for signal. State is %d", global_lagent_state);
     g_cond_wait (stun_signal_ptr, stun_mutex_ptr);
   }
@@ -282,7 +285,7 @@ static void cb_candidate_gathering_done(NiceAgent *agent, guint stream_id, gpoin
     g_debug ("ragent finished gathering candidates");
     ragent_candidate_gathering_done = TRUE;
   }
-  g_main_loop_quit(global_mainloop);
+  g_cancellable_cancel (global_cancellable);
 }
 
 static void cb_nice_recv (NiceAgent *agent, guint stream_id, guint component_id, guint len, gchar *buf, gpointer user_data)
@@ -299,7 +302,7 @@ static void cb_nice_recv (NiceAgent *agent, guint stream_id, guint component_id,
     g_debug ("test-dribblemode:%s: ragent recieved %d bytes : quit mainloop",
              G_STRFUNC, len);
     data_received = TRUE;
-    g_main_loop_quit (global_mainloop);
+    g_cancellable_cancel (global_cancellable);
   }
 }
 
@@ -320,9 +323,10 @@ static void cb_component_state_changed (NiceAgent *agent, guint stream_id, guint
   if (GPOINTER_TO_UINT(data) == 1 && state == NICE_COMPONENT_STATE_FAILED) {
     g_debug ("Signalling STUN response since connchecks failed");
     g_mutex_lock (stun_mutex_ptr);
+    send_stun = TRUE;
     g_cond_signal (stun_signal_ptr);
     g_mutex_unlock (stun_mutex_ptr);
-    g_main_loop_quit (global_mainloop);
+    g_cancellable_cancel (global_cancellable);
   }
 
   if(GPOINTER_TO_UINT(data) == 1 && state == NICE_COMPONENT_STATE_READY) {
@@ -346,6 +350,7 @@ static void swap_candidates(NiceAgent *local, guint local_id, NiceAgent *remote,
 
   if (signal_stun_reply) {
     g_mutex_lock (stun_mutex_ptr);
+    send_stun = TRUE;
     g_cond_signal (stun_signal_ptr);
     g_mutex_unlock (stun_mutex_ptr);
   }
@@ -444,12 +449,14 @@ static void init_test(NiceAgent *lagent, NiceAgent *ragent, gboolean connect_new
   }
 
   data_received = FALSE;
+  got_stun_packet = FALSE;
+  send_stun = FALSE;
 
   nice_agent_attach_recv (lagent, global_ls_id, NICE_COMPONENT_TYPE_RTP,
-                   g_main_loop_get_context(global_mainloop),
+                   g_main_context_default (),
                    cb_nice_recv, LEFT_AGENT);
   nice_agent_attach_recv (ragent, global_rs_id, NICE_COMPONENT_TYPE_RTP,
-                   g_main_loop_get_context(global_mainloop),
+                   g_main_context_default (),
                    cb_nice_recv, RIGHT_AGENT);
 }
 
@@ -465,18 +472,20 @@ static void standard_test(NiceAgent *lagent, NiceAgent *ragent)
 {
   g_debug ("test-dribblemode:%s", G_STRFUNC);
 
+  got_stun_packet = FALSE;
   init_test (lagent, ragent, FALSE);
 
   nice_agent_gather_candidates (lagent, global_ls_id);
-  g_main_loop_run (global_mainloop);
+  while (!got_stun_packet)
+    g_main_context_iteration (NULL, TRUE);
   g_assert (global_lagent_state == NICE_COMPONENT_STATE_GATHERING &&
             !lagent_candidate_gathering_done);
 
   nice_agent_gather_candidates (ragent, global_rs_id);
-  if (!ragent_candidate_gathering_done) {
-    g_main_loop_run (global_mainloop);
-    g_assert (ragent_candidate_gathering_done);
-  }
+  while (!ragent_candidate_gathering_done)
+    g_main_context_iteration (NULL, TRUE);
+  g_cancellable_reset (global_cancellable);
+  g_assert (ragent_candidate_gathering_done);
 
   set_credentials (lagent, global_ls_id, ragent, global_rs_id);
 
@@ -485,7 +494,9 @@ static void standard_test(NiceAgent *lagent, NiceAgent *ragent)
                    lagent, global_ls_id,
                    TRUE);
 
-  g_main_loop_run (global_mainloop);
+  while (!data_received)
+    g_main_context_iteration (NULL, TRUE);
+  g_cancellable_reset (global_cancellable);
   g_assert (global_lagent_state >= NICE_COMPONENT_STATE_CONNECTED &&
             data_received);
 
@@ -493,7 +504,9 @@ static void standard_test(NiceAgent *lagent, NiceAgent *ragent)
   swap_candidates (lagent, global_ls_id,
                    ragent, global_rs_id,
                    FALSE);
-  g_main_loop_run (global_mainloop);
+  while (!lagent_candidate_gathering_done)
+    g_main_context_iteration (NULL, TRUE);
+  g_cancellable_reset (global_cancellable);
 
   g_assert (lagent_candidate_gathering_done);
 
@@ -515,21 +528,24 @@ static void bad_credentials_test(NiceAgent *lagent, NiceAgent *ragent)
                                      "wrong2", "wrong2");
 
   nice_agent_gather_candidates (lagent, global_ls_id);
-  g_main_loop_run (global_mainloop);
+  while (!got_stun_packet)
+    g_main_context_iteration (NULL, TRUE);
+  g_cancellable_reset (global_cancellable);
   g_assert (global_lagent_state == NICE_COMPONENT_STATE_GATHERING &&
             !lagent_candidate_gathering_done);
 
   nice_agent_gather_candidates (ragent, global_rs_id);
-  if (!ragent_candidate_gathering_done) {
-    g_main_loop_run (global_mainloop);
-    g_assert (ragent_candidate_gathering_done);
-  }
+  while (!ragent_candidate_gathering_done)
+    g_main_context_iteration (NULL, TRUE);
+  g_cancellable_reset (global_cancellable);
+  g_assert (ragent_candidate_gathering_done);
 
   swap_candidates (ragent, global_rs_id,
                    lagent, global_ls_id,
                    FALSE);
-  g_main_loop_run (global_mainloop);
-  g_assert (global_lagent_state == NICE_COMPONENT_STATE_FAILED);
+  while (global_lagent_state != NICE_COMPONENT_STATE_FAILED)
+    g_main_context_iteration (NULL, TRUE);
+  g_cancellable_reset (global_cancellable);
 
   // Set the correct credentials and swap candidates
   set_credentials (lagent, global_ls_id, ragent, global_rs_id);
@@ -541,14 +557,19 @@ static void bad_credentials_test(NiceAgent *lagent, NiceAgent *ragent)
                    ragent, global_rs_id,
                    FALSE);
 
-  g_main_loop_run (global_mainloop);
+  while (!data_received)
+    g_main_context_iteration (NULL, TRUE);
+  g_cancellable_reset (global_cancellable);
 
   g_assert (data_received);
   g_assert (global_lagent_state == NICE_COMPONENT_STATE_READY);
   g_assert (global_ragent_state >= NICE_COMPONENT_STATE_CONNECTED);
 
   // Wait for lagent to finish gathering candidates
-  g_main_loop_run (global_mainloop);
+  while (!lagent_candidate_gathering_done)
+    g_main_context_iteration (NULL, TRUE);
+  g_cancellable_reset (global_cancellable);
+
   g_assert (lagent_candidate_gathering_done);
 
   cleanup (lagent, ragent);
@@ -563,23 +584,30 @@ static void bad_candidate_test(NiceAgent *lagent,NiceAgent *ragent)
   init_test (lagent, ragent, FALSE);
 
   nice_agent_gather_candidates (lagent, global_ls_id);
-  g_main_loop_run (global_mainloop);
+  while (!got_stun_packet)
+    g_main_context_iteration (NULL, TRUE);
+  g_cancellable_reset (global_cancellable);
   g_assert (global_lagent_state == NICE_COMPONENT_STATE_GATHERING &&
             !lagent_candidate_gathering_done);
 
   nice_agent_gather_candidates (ragent, global_rs_id);
-  if (!ragent_candidate_gathering_done) {
-    g_main_loop_run (global_mainloop);
-    g_assert (ragent_candidate_gathering_done);
-  }
+  while (!ragent_candidate_gathering_done)
+      g_main_context_iteration (NULL, TRUE);
+  g_cancellable_reset (global_cancellable);
+
+  g_assert (ragent_candidate_gathering_done);
 
   add_bad_candidate (lagent, global_ls_id, cand);
 
   // lagent will finish candidate gathering causing this mainloop to quit
-  g_main_loop_run (global_mainloop);
+  while (!lagent_candidate_gathering_done)
+    g_main_context_iteration (NULL, TRUE);
+  g_cancellable_reset (global_cancellable);
 
   // connchecks will fail causing this mainloop to quit
-  g_main_loop_run (global_mainloop);
+  while (global_lagent_state != NICE_COMPONENT_STATE_FAILED)
+    g_main_context_iteration (NULL, TRUE);
+  g_cancellable_reset (global_cancellable);
 
   g_assert (global_lagent_state == NICE_COMPONENT_STATE_FAILED &&
             !data_received);
@@ -593,11 +621,13 @@ static void bad_candidate_test(NiceAgent *lagent,NiceAgent *ragent)
                    ragent, global_rs_id,
                    FALSE);
 
-  g_main_loop_run (global_mainloop);
+  while (!data_received)
+    g_main_context_iteration (NULL, TRUE);
+  g_cancellable_reset (global_cancellable);
 
   g_assert (lagent_candidate_gathering_done);
 
-  g_assert (global_lagent_state == NICE_COMPONENT_STATE_READY);
+  g_assert (global_lagent_state >= NICE_COMPONENT_STATE_CONNECTED);
   g_assert (global_ragent_state >= NICE_COMPONENT_STATE_CONNECTED);
 
   cleanup (lagent, ragent);
@@ -611,26 +641,34 @@ static void new_candidate_test(NiceAgent *lagent, NiceAgent *ragent)
   set_credentials (lagent, global_ls_id, ragent, global_rs_id);
 
   nice_agent_gather_candidates (lagent, global_ls_id);
-  g_main_loop_run (global_mainloop);
+  while (!got_stun_packet)
+    g_main_context_iteration (NULL, TRUE);
+  g_cancellable_reset (global_cancellable);
   g_assert (global_lagent_state == NICE_COMPONENT_STATE_GATHERING &&
             !lagent_candidate_gathering_done);
 
   nice_agent_gather_candidates (ragent, global_rs_id);
-  if (!ragent_candidate_gathering_done) {
-    g_main_loop_run (global_mainloop);
-  }
+  while (!ragent_candidate_gathering_done)
+    g_main_context_iteration (NULL, TRUE);
+  g_cancellable_reset (global_cancellable);
 
   // Wait for data
-  g_main_loop_run (global_mainloop);
+  while (!data_received)
+      g_main_context_iteration (NULL, TRUE);
+  g_cancellable_reset (global_cancellable);
   g_assert (data_received);
 
   // Data arrived, signal STUN thread to send STUN response
   g_mutex_lock (stun_mutex_ptr);
+  send_stun = TRUE;
   g_cond_signal (stun_signal_ptr);
   g_mutex_unlock (stun_mutex_ptr);
 
   // Wait for lagent to finish gathering candidates
-  g_main_loop_run (global_mainloop);
+  while (!lagent_candidate_gathering_done ||
+      !lagent_candidate_gathering_done)
+    g_main_context_iteration (NULL, TRUE);
+  g_cancellable_reset (global_cancellable);
 
   g_assert (lagent_candidate_gathering_done);
   g_assert (ragent_candidate_gathering_done);
@@ -664,16 +702,19 @@ int main(void)
   NiceAgent *lagent = NULL, *ragent = NULL;
   GThread *stun_thread = NULL;
   NiceAddress baseaddr;
+  GSource *src;
 
   g_type_init();
 
-  global_mainloop = g_main_loop_new (NULL, FALSE);
+  global_cancellable = g_cancellable_new ();
+  src = g_cancellable_source_new (global_cancellable);
+  g_source_set_dummy_callback (src);
+  g_source_attach (src, NULL);
+  g_source_unref (src);
 
 #if !GLIB_CHECK_VERSION(2,31,8)
   g_thread_init (NULL);
-  stun_thread = g_thread_create (stun_thread_func,
-                                 global_mainloop,
-                                 TRUE, NULL);
+  stun_thread = g_thread_create (stun_thread_func, NULL,  TRUE, NULL);
  stun_mutex_ptr = g_mutex_new ();
  stun_signal_ptr = g_cond_new ();
 #else
@@ -686,10 +727,8 @@ int main(void)
   g_mutex_lock (stun_thread_mutex_ptr);
   g_cond_wait (stun_thread_signal_ptr, stun_thread_mutex_ptr); 
 
-  lagent = nice_agent_new (g_main_loop_get_context (global_mainloop),
-      NICE_COMPATIBILITY_RFC5245);
-  ragent = nice_agent_new (g_main_loop_get_context (global_mainloop),
-      NICE_COMPATIBILITY_RFC5245);
+  lagent = nice_agent_new (NULL, NICE_COMPATIBILITY_RFC5245);
+  ragent = nice_agent_new (NULL, NICE_COMPATIBILITY_RFC5245);
 
   g_object_set (G_OBJECT (lagent), "controlling-mode", TRUE, NULL);
   g_object_set (G_OBJECT (ragent), "controlling-mode", FALSE, NULL);
@@ -734,7 +773,7 @@ int main(void)
   g_mutex_free (stun_mutex_ptr);
   g_cond_free (stun_signal_ptr);
 #endif
-  g_main_loop_unref (global_mainloop);
+  g_object_unref (global_cancellable);
 
   return 0;
 }