Commit 1771bc79 authored by Olivier Crête's avatar Olivier Crête

tests: Fix horribly raciness in test-new-dribble

parent 91b93b44
...@@ -73,15 +73,17 @@ ...@@ -73,15 +73,17 @@
static GCond *stun_thread_signal_ptr = &stun_thread_signal; static GCond *stun_thread_signal_ptr = &stun_thread_signal;
#endif #endif
static GMainLoop *global_mainloop;
static NiceComponentState global_lagent_state = NICE_COMPONENT_STATE_LAST; static NiceComponentState global_lagent_state = NICE_COMPONENT_STATE_LAST;
static NiceComponentState global_ragent_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 exit_stun_thread = FALSE;
static gboolean lagent_candidate_gathering_done = FALSE; static gboolean lagent_candidate_gathering_done = FALSE;
static gboolean ragent_candidate_gathering_done = FALSE; static gboolean ragent_candidate_gathering_done = FALSE;
static guint global_ls_id, global_rs_id; static guint global_ls_id, global_rs_id;
static gboolean data_received = FALSE; static gboolean data_received = FALSE;
static gboolean drop_stun_packets = FALSE; static gboolean drop_stun_packets = FALSE;
static gboolean got_stun_packet = FALSE;
static gboolean send_stun = FALSE;
static const uint16_t known_attributes[] = { static const uint16_t known_attributes[] = {
0 0
...@@ -204,10 +206,11 @@ recv_packet: ...@@ -204,10 +206,11 @@ recv_packet:
buf_len = stun_agent_finish_message (agent, &response, NULL, 0); buf_len = stun_agent_finish_message (agent, &response, NULL, 0);
send_buf: send_buf:
g_main_loop_quit (global_mainloop); g_cancellable_cancel (global_cancellable);
g_debug ("Ready to send a STUN response"); g_debug ("Ready to send a STUN response");
g_assert (g_mutex_trylock (stun_mutex_ptr)); 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_debug ("Waiting for signal. State is %d", global_lagent_state);
g_cond_wait (stun_signal_ptr, stun_mutex_ptr); 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 ...@@ -282,7 +285,7 @@ static void cb_candidate_gathering_done(NiceAgent *agent, guint stream_id, gpoin
g_debug ("ragent finished gathering candidates"); g_debug ("ragent finished gathering candidates");
ragent_candidate_gathering_done = TRUE; 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) 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, ...@@ -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_debug ("test-dribblemode:%s: ragent recieved %d bytes : quit mainloop",
G_STRFUNC, len); G_STRFUNC, len);
data_received = TRUE; 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 ...@@ -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) { if (GPOINTER_TO_UINT(data) == 1 && state == NICE_COMPONENT_STATE_FAILED) {
g_debug ("Signalling STUN response since connchecks failed"); g_debug ("Signalling STUN response since connchecks failed");
g_mutex_lock (stun_mutex_ptr); g_mutex_lock (stun_mutex_ptr);
send_stun = TRUE;
g_cond_signal (stun_signal_ptr); g_cond_signal (stun_signal_ptr);
g_mutex_unlock (stun_mutex_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) { 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, ...@@ -346,6 +350,7 @@ static void swap_candidates(NiceAgent *local, guint local_id, NiceAgent *remote,
if (signal_stun_reply) { if (signal_stun_reply) {
g_mutex_lock (stun_mutex_ptr); g_mutex_lock (stun_mutex_ptr);
send_stun = TRUE;
g_cond_signal (stun_signal_ptr); g_cond_signal (stun_signal_ptr);
g_mutex_unlock (stun_mutex_ptr); g_mutex_unlock (stun_mutex_ptr);
} }
...@@ -444,12 +449,14 @@ static void init_test(NiceAgent *lagent, NiceAgent *ragent, gboolean connect_new ...@@ -444,12 +449,14 @@ static void init_test(NiceAgent *lagent, NiceAgent *ragent, gboolean connect_new
} }
data_received = FALSE; data_received = FALSE;
got_stun_packet = FALSE;
send_stun = FALSE;
nice_agent_attach_recv (lagent, global_ls_id, NICE_COMPONENT_TYPE_RTP, 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); cb_nice_recv, LEFT_AGENT);
nice_agent_attach_recv (ragent, global_rs_id, NICE_COMPONENT_TYPE_RTP, 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); cb_nice_recv, RIGHT_AGENT);
} }
...@@ -465,18 +472,20 @@ static void standard_test(NiceAgent *lagent, NiceAgent *ragent) ...@@ -465,18 +472,20 @@ static void standard_test(NiceAgent *lagent, NiceAgent *ragent)
{ {
g_debug ("test-dribblemode:%s", G_STRFUNC); g_debug ("test-dribblemode:%s", G_STRFUNC);
got_stun_packet = FALSE;
init_test (lagent, ragent, FALSE); init_test (lagent, ragent, FALSE);
nice_agent_gather_candidates (lagent, global_ls_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_assert (global_lagent_state == NICE_COMPONENT_STATE_GATHERING && g_assert (global_lagent_state == NICE_COMPONENT_STATE_GATHERING &&
!lagent_candidate_gathering_done); !lagent_candidate_gathering_done);
nice_agent_gather_candidates (ragent, global_rs_id); nice_agent_gather_candidates (ragent, global_rs_id);
if (!ragent_candidate_gathering_done) { while (!ragent_candidate_gathering_done)
g_main_loop_run (global_mainloop); g_main_context_iteration (NULL, TRUE);
g_cancellable_reset (global_cancellable);
g_assert (ragent_candidate_gathering_done); g_assert (ragent_candidate_gathering_done);
}
set_credentials (lagent, global_ls_id, ragent, global_rs_id); set_credentials (lagent, global_ls_id, ragent, global_rs_id);
...@@ -485,7 +494,9 @@ static void standard_test(NiceAgent *lagent, NiceAgent *ragent) ...@@ -485,7 +494,9 @@ static void standard_test(NiceAgent *lagent, NiceAgent *ragent)
lagent, global_ls_id, lagent, global_ls_id,
TRUE); 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 && g_assert (global_lagent_state >= NICE_COMPONENT_STATE_CONNECTED &&
data_received); data_received);
...@@ -493,7 +504,9 @@ static void standard_test(NiceAgent *lagent, NiceAgent *ragent) ...@@ -493,7 +504,9 @@ static void standard_test(NiceAgent *lagent, NiceAgent *ragent)
swap_candidates (lagent, global_ls_id, swap_candidates (lagent, global_ls_id,
ragent, global_rs_id, ragent, global_rs_id,
FALSE); 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); g_assert (lagent_candidate_gathering_done);
...@@ -515,21 +528,24 @@ static void bad_credentials_test(NiceAgent *lagent, NiceAgent *ragent) ...@@ -515,21 +528,24 @@ static void bad_credentials_test(NiceAgent *lagent, NiceAgent *ragent)
"wrong2", "wrong2"); "wrong2", "wrong2");
nice_agent_gather_candidates (lagent, global_ls_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 && g_assert (global_lagent_state == NICE_COMPONENT_STATE_GATHERING &&
!lagent_candidate_gathering_done); !lagent_candidate_gathering_done);
nice_agent_gather_candidates (ragent, global_rs_id); nice_agent_gather_candidates (ragent, global_rs_id);
if (!ragent_candidate_gathering_done) { while (!ragent_candidate_gathering_done)
g_main_loop_run (global_mainloop); g_main_context_iteration (NULL, TRUE);
g_cancellable_reset (global_cancellable);
g_assert (ragent_candidate_gathering_done); g_assert (ragent_candidate_gathering_done);
}
swap_candidates (ragent, global_rs_id, swap_candidates (ragent, global_rs_id,
lagent, global_ls_id, lagent, global_ls_id,
FALSE); FALSE);
g_main_loop_run (global_mainloop); while (global_lagent_state != NICE_COMPONENT_STATE_FAILED)
g_assert (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 the correct credentials and swap candidates
set_credentials (lagent, global_ls_id, ragent, global_rs_id); set_credentials (lagent, global_ls_id, ragent, global_rs_id);
...@@ -541,14 +557,19 @@ static void bad_credentials_test(NiceAgent *lagent, NiceAgent *ragent) ...@@ -541,14 +557,19 @@ static void bad_credentials_test(NiceAgent *lagent, NiceAgent *ragent)
ragent, global_rs_id, ragent, global_rs_id,
FALSE); 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 (data_received);
g_assert (global_lagent_state == NICE_COMPONENT_STATE_READY); g_assert (global_lagent_state == NICE_COMPONENT_STATE_READY);
g_assert (global_ragent_state >= NICE_COMPONENT_STATE_CONNECTED); g_assert (global_ragent_state >= NICE_COMPONENT_STATE_CONNECTED);
// Wait for lagent to finish gathering candidates // 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); g_assert (lagent_candidate_gathering_done);
cleanup (lagent, ragent); cleanup (lagent, ragent);
...@@ -563,23 +584,30 @@ static void bad_candidate_test(NiceAgent *lagent,NiceAgent *ragent) ...@@ -563,23 +584,30 @@ static void bad_candidate_test(NiceAgent *lagent,NiceAgent *ragent)
init_test (lagent, ragent, FALSE); init_test (lagent, ragent, FALSE);
nice_agent_gather_candidates (lagent, global_ls_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 && g_assert (global_lagent_state == NICE_COMPONENT_STATE_GATHERING &&
!lagent_candidate_gathering_done); !lagent_candidate_gathering_done);
nice_agent_gather_candidates (ragent, global_rs_id); nice_agent_gather_candidates (ragent, global_rs_id);
if (!ragent_candidate_gathering_done) { while (!ragent_candidate_gathering_done)
g_main_loop_run (global_mainloop); g_main_context_iteration (NULL, TRUE);
g_cancellable_reset (global_cancellable);
g_assert (ragent_candidate_gathering_done); g_assert (ragent_candidate_gathering_done);
}
add_bad_candidate (lagent, global_ls_id, cand); add_bad_candidate (lagent, global_ls_id, cand);
// lagent will finish candidate gathering causing this mainloop to quit // 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 // 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 && g_assert (global_lagent_state == NICE_COMPONENT_STATE_FAILED &&
!data_received); !data_received);
...@@ -593,11 +621,13 @@ static void bad_candidate_test(NiceAgent *lagent,NiceAgent *ragent) ...@@ -593,11 +621,13 @@ static void bad_candidate_test(NiceAgent *lagent,NiceAgent *ragent)
ragent, global_rs_id, ragent, global_rs_id,
FALSE); 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 (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); g_assert (global_ragent_state >= NICE_COMPONENT_STATE_CONNECTED);
cleanup (lagent, ragent); cleanup (lagent, ragent);
...@@ -611,26 +641,34 @@ static void new_candidate_test(NiceAgent *lagent, NiceAgent *ragent) ...@@ -611,26 +641,34 @@ static void new_candidate_test(NiceAgent *lagent, NiceAgent *ragent)
set_credentials (lagent, global_ls_id, ragent, global_rs_id); set_credentials (lagent, global_ls_id, ragent, global_rs_id);
nice_agent_gather_candidates (lagent, global_ls_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 && g_assert (global_lagent_state == NICE_COMPONENT_STATE_GATHERING &&
!lagent_candidate_gathering_done); !lagent_candidate_gathering_done);
nice_agent_gather_candidates (ragent, global_rs_id); nice_agent_gather_candidates (ragent, global_rs_id);
if (!ragent_candidate_gathering_done) { while (!ragent_candidate_gathering_done)
g_main_loop_run (global_mainloop); g_main_context_iteration (NULL, TRUE);
} g_cancellable_reset (global_cancellable);
// Wait for data // 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); g_assert (data_received);
// Data arrived, signal STUN thread to send STUN response // Data arrived, signal STUN thread to send STUN response
g_mutex_lock (stun_mutex_ptr); g_mutex_lock (stun_mutex_ptr);
send_stun = TRUE;
g_cond_signal (stun_signal_ptr); g_cond_signal (stun_signal_ptr);
g_mutex_unlock (stun_mutex_ptr); g_mutex_unlock (stun_mutex_ptr);
// Wait for lagent to finish gathering candidates // 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 (lagent_candidate_gathering_done);
g_assert (ragent_candidate_gathering_done); g_assert (ragent_candidate_gathering_done);
...@@ -664,16 +702,19 @@ int main(void) ...@@ -664,16 +702,19 @@ int main(void)
NiceAgent *lagent = NULL, *ragent = NULL; NiceAgent *lagent = NULL, *ragent = NULL;
GThread *stun_thread = NULL; GThread *stun_thread = NULL;
NiceAddress baseaddr; NiceAddress baseaddr;
GSource *src;
g_type_init(); 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) #if !GLIB_CHECK_VERSION(2,31,8)
g_thread_init (NULL); g_thread_init (NULL);
stun_thread = g_thread_create (stun_thread_func, stun_thread = g_thread_create (stun_thread_func, NULL, TRUE, NULL);
global_mainloop,
TRUE, NULL);
stun_mutex_ptr = g_mutex_new (); stun_mutex_ptr = g_mutex_new ();
stun_signal_ptr = g_cond_new (); stun_signal_ptr = g_cond_new ();
#else #else
...@@ -686,10 +727,8 @@ int main(void) ...@@ -686,10 +727,8 @@ int main(void)
g_mutex_lock (stun_thread_mutex_ptr); g_mutex_lock (stun_thread_mutex_ptr);
g_cond_wait (stun_thread_signal_ptr, 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), lagent = nice_agent_new (NULL, NICE_COMPATIBILITY_RFC5245);
NICE_COMPATIBILITY_RFC5245); ragent = nice_agent_new (NULL, NICE_COMPATIBILITY_RFC5245);
ragent = nice_agent_new (g_main_loop_get_context (global_mainloop),
NICE_COMPATIBILITY_RFC5245);
g_object_set (G_OBJECT (lagent), "controlling-mode", TRUE, NULL); g_object_set (G_OBJECT (lagent), "controlling-mode", TRUE, NULL);
g_object_set (G_OBJECT (ragent), "controlling-mode", FALSE, NULL); g_object_set (G_OBJECT (ragent), "controlling-mode", FALSE, NULL);
...@@ -734,7 +773,7 @@ int main(void) ...@@ -734,7 +773,7 @@ int main(void)
g_mutex_free (stun_mutex_ptr); g_mutex_free (stun_mutex_ptr);
g_cond_free (stun_signal_ptr); g_cond_free (stun_signal_ptr);
#endif #endif
g_main_loop_unref (global_mainloop); g_object_unref (global_cancellable);
return 0; return 0;
} }
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment