Codebase list libnice / 1771bc7
tests: Fix horribly raciness in test-new-dribble Olivier CrĂȘte 10 years ago
1 changed file(s) with 87 addition(s) and 48 deletion(s). Raw diff Collapse all Expand all
7272 static GCond *stun_thread_signal_ptr = &stun_thread_signal;
7373 #endif
7474
75 static GMainLoop *global_mainloop;
7675 static NiceComponentState global_lagent_state = NICE_COMPONENT_STATE_LAST;
7776 static NiceComponentState global_ragent_state = NICE_COMPONENT_STATE_LAST;
77 static GCancellable *global_cancellable;
7878 static gboolean exit_stun_thread = FALSE;
7979 static gboolean lagent_candidate_gathering_done = FALSE;
8080 static gboolean ragent_candidate_gathering_done = FALSE;
8181 static guint global_ls_id, global_rs_id;
8282 static gboolean data_received = FALSE;
8383 static gboolean drop_stun_packets = FALSE;
84 static gboolean got_stun_packet = FALSE;
85 static gboolean send_stun = FALSE;
8486
8587 static const uint16_t known_attributes[] = {
8688 0
203205 buf_len = stun_agent_finish_message (agent, &response, NULL, 0);
204206
205207 send_buf:
206 g_main_loop_quit (global_mainloop);
208 g_cancellable_cancel (global_cancellable);
207209 g_debug ("Ready to send a STUN response");
208210 g_assert (g_mutex_trylock (stun_mutex_ptr));
209 while (global_lagent_state < NICE_COMPONENT_STATE_CONNECTING) {
211 got_stun_packet = TRUE;
212 while (send_stun) {
210213 g_debug ("Waiting for signal. State is %d", global_lagent_state);
211214 g_cond_wait (stun_signal_ptr, stun_mutex_ptr);
212215 }
281284 g_debug ("ragent finished gathering candidates");
282285 ragent_candidate_gathering_done = TRUE;
283286 }
284 g_main_loop_quit(global_mainloop);
287 g_cancellable_cancel (global_cancellable);
285288 }
286289
287290 static void cb_nice_recv (NiceAgent *agent, guint stream_id, guint component_id, guint len, gchar *buf, gpointer user_data)
298301 g_debug ("test-dribblemode:%s: ragent recieved %d bytes : quit mainloop",
299302 G_STRFUNC, len);
300303 data_received = TRUE;
301 g_main_loop_quit (global_mainloop);
304 g_cancellable_cancel (global_cancellable);
302305 }
303306 }
304307
319322 if (GPOINTER_TO_UINT(data) == 1 && state == NICE_COMPONENT_STATE_FAILED) {
320323 g_debug ("Signalling STUN response since connchecks failed");
321324 g_mutex_lock (stun_mutex_ptr);
325 send_stun = TRUE;
322326 g_cond_signal (stun_signal_ptr);
323327 g_mutex_unlock (stun_mutex_ptr);
324 g_main_loop_quit (global_mainloop);
328 g_cancellable_cancel (global_cancellable);
325329 }
326330
327331 if(GPOINTER_TO_UINT(data) == 1 && state == NICE_COMPONENT_STATE_READY) {
345349
346350 if (signal_stun_reply) {
347351 g_mutex_lock (stun_mutex_ptr);
352 send_stun = TRUE;
348353 g_cond_signal (stun_signal_ptr);
349354 g_mutex_unlock (stun_mutex_ptr);
350355 }
443448 }
444449
445450 data_received = FALSE;
451 got_stun_packet = FALSE;
452 send_stun = FALSE;
446453
447454 nice_agent_attach_recv (lagent, global_ls_id, NICE_COMPONENT_TYPE_RTP,
448 g_main_loop_get_context(global_mainloop),
455 g_main_context_default (),
449456 cb_nice_recv, LEFT_AGENT);
450457 nice_agent_attach_recv (ragent, global_rs_id, NICE_COMPONENT_TYPE_RTP,
451 g_main_loop_get_context(global_mainloop),
458 g_main_context_default (),
452459 cb_nice_recv, RIGHT_AGENT);
453460 }
454461
464471 {
465472 g_debug ("test-dribblemode:%s", G_STRFUNC);
466473
474 got_stun_packet = FALSE;
467475 init_test (lagent, ragent, FALSE);
468476
469477 nice_agent_gather_candidates (lagent, global_ls_id);
470 g_main_loop_run (global_mainloop);
478 while (!got_stun_packet)
479 g_main_context_iteration (NULL, TRUE);
471480 g_assert (global_lagent_state == NICE_COMPONENT_STATE_GATHERING &&
472481 !lagent_candidate_gathering_done);
473482
474483 nice_agent_gather_candidates (ragent, global_rs_id);
475 if (!ragent_candidate_gathering_done) {
476 g_main_loop_run (global_mainloop);
477 g_assert (ragent_candidate_gathering_done);
478 }
484 while (!ragent_candidate_gathering_done)
485 g_main_context_iteration (NULL, TRUE);
486 g_cancellable_reset (global_cancellable);
487 g_assert (ragent_candidate_gathering_done);
479488
480489 set_credentials (lagent, global_ls_id, ragent, global_rs_id);
481490
484493 lagent, global_ls_id,
485494 TRUE);
486495
487 g_main_loop_run (global_mainloop);
496 while (!data_received)
497 g_main_context_iteration (NULL, TRUE);
498 g_cancellable_reset (global_cancellable);
488499 g_assert (global_lagent_state >= NICE_COMPONENT_STATE_CONNECTED &&
489500 data_received);
490501
492503 swap_candidates (lagent, global_ls_id,
493504 ragent, global_rs_id,
494505 FALSE);
495 g_main_loop_run (global_mainloop);
506 while (!lagent_candidate_gathering_done)
507 g_main_context_iteration (NULL, TRUE);
508 g_cancellable_reset (global_cancellable);
496509
497510 g_assert (lagent_candidate_gathering_done);
498511
514527 "wrong2", "wrong2");
515528
516529 nice_agent_gather_candidates (lagent, global_ls_id);
517 g_main_loop_run (global_mainloop);
530 while (!got_stun_packet)
531 g_main_context_iteration (NULL, TRUE);
532 g_cancellable_reset (global_cancellable);
518533 g_assert (global_lagent_state == NICE_COMPONENT_STATE_GATHERING &&
519534 !lagent_candidate_gathering_done);
520535
521536 nice_agent_gather_candidates (ragent, global_rs_id);
522 if (!ragent_candidate_gathering_done) {
523 g_main_loop_run (global_mainloop);
524 g_assert (ragent_candidate_gathering_done);
525 }
537 while (!ragent_candidate_gathering_done)
538 g_main_context_iteration (NULL, TRUE);
539 g_cancellable_reset (global_cancellable);
540 g_assert (ragent_candidate_gathering_done);
526541
527542 swap_candidates (ragent, global_rs_id,
528543 lagent, global_ls_id,
529544 FALSE);
530 g_main_loop_run (global_mainloop);
531 g_assert (global_lagent_state == NICE_COMPONENT_STATE_FAILED);
545 while (global_lagent_state != NICE_COMPONENT_STATE_FAILED)
546 g_main_context_iteration (NULL, TRUE);
547 g_cancellable_reset (global_cancellable);
532548
533549 // Set the correct credentials and swap candidates
534550 set_credentials (lagent, global_ls_id, ragent, global_rs_id);
540556 ragent, global_rs_id,
541557 FALSE);
542558
543 g_main_loop_run (global_mainloop);
559 while (!data_received)
560 g_main_context_iteration (NULL, TRUE);
561 g_cancellable_reset (global_cancellable);
544562
545563 g_assert (data_received);
546564 g_assert (global_lagent_state == NICE_COMPONENT_STATE_READY);
547565 g_assert (global_ragent_state >= NICE_COMPONENT_STATE_CONNECTED);
548566
549567 // Wait for lagent to finish gathering candidates
550 g_main_loop_run (global_mainloop);
568 while (!lagent_candidate_gathering_done)
569 g_main_context_iteration (NULL, TRUE);
570 g_cancellable_reset (global_cancellable);
571
551572 g_assert (lagent_candidate_gathering_done);
552573
553574 cleanup (lagent, ragent);
562583 init_test (lagent, ragent, FALSE);
563584
564585 nice_agent_gather_candidates (lagent, global_ls_id);
565 g_main_loop_run (global_mainloop);
586 while (!got_stun_packet)
587 g_main_context_iteration (NULL, TRUE);
588 g_cancellable_reset (global_cancellable);
566589 g_assert (global_lagent_state == NICE_COMPONENT_STATE_GATHERING &&
567590 !lagent_candidate_gathering_done);
568591
569592 nice_agent_gather_candidates (ragent, global_rs_id);
570 if (!ragent_candidate_gathering_done) {
571 g_main_loop_run (global_mainloop);
572 g_assert (ragent_candidate_gathering_done);
573 }
593 while (!ragent_candidate_gathering_done)
594 g_main_context_iteration (NULL, TRUE);
595 g_cancellable_reset (global_cancellable);
596
597 g_assert (ragent_candidate_gathering_done);
574598
575599 add_bad_candidate (lagent, global_ls_id, cand);
576600
577601 // lagent will finish candidate gathering causing this mainloop to quit
578 g_main_loop_run (global_mainloop);
602 while (!lagent_candidate_gathering_done)
603 g_main_context_iteration (NULL, TRUE);
604 g_cancellable_reset (global_cancellable);
579605
580606 // connchecks will fail causing this mainloop to quit
581 g_main_loop_run (global_mainloop);
607 while (global_lagent_state != NICE_COMPONENT_STATE_FAILED)
608 g_main_context_iteration (NULL, TRUE);
609 g_cancellable_reset (global_cancellable);
582610
583611 g_assert (global_lagent_state == NICE_COMPONENT_STATE_FAILED &&
584612 !data_received);
592620 ragent, global_rs_id,
593621 FALSE);
594622
595 g_main_loop_run (global_mainloop);
623 while (!data_received)
624 g_main_context_iteration (NULL, TRUE);
625 g_cancellable_reset (global_cancellable);
596626
597627 g_assert (lagent_candidate_gathering_done);
598628
599 g_assert (global_lagent_state == NICE_COMPONENT_STATE_READY);
629 g_assert (global_lagent_state >= NICE_COMPONENT_STATE_CONNECTED);
600630 g_assert (global_ragent_state >= NICE_COMPONENT_STATE_CONNECTED);
601631
602632 cleanup (lagent, ragent);
610640 set_credentials (lagent, global_ls_id, ragent, global_rs_id);
611641
612642 nice_agent_gather_candidates (lagent, global_ls_id);
613 g_main_loop_run (global_mainloop);
643 while (!got_stun_packet)
644 g_main_context_iteration (NULL, TRUE);
645 g_cancellable_reset (global_cancellable);
614646 g_assert (global_lagent_state == NICE_COMPONENT_STATE_GATHERING &&
615647 !lagent_candidate_gathering_done);
616648
617649 nice_agent_gather_candidates (ragent, global_rs_id);
618 if (!ragent_candidate_gathering_done) {
619 g_main_loop_run (global_mainloop);
620 }
650 while (!ragent_candidate_gathering_done)
651 g_main_context_iteration (NULL, TRUE);
652 g_cancellable_reset (global_cancellable);
621653
622654 // Wait for data
623 g_main_loop_run (global_mainloop);
655 while (!data_received)
656 g_main_context_iteration (NULL, TRUE);
657 g_cancellable_reset (global_cancellable);
624658 g_assert (data_received);
625659
626660 // Data arrived, signal STUN thread to send STUN response
627661 g_mutex_lock (stun_mutex_ptr);
662 send_stun = TRUE;
628663 g_cond_signal (stun_signal_ptr);
629664 g_mutex_unlock (stun_mutex_ptr);
630665
631666 // Wait for lagent to finish gathering candidates
632 g_main_loop_run (global_mainloop);
667 while (!lagent_candidate_gathering_done ||
668 !lagent_candidate_gathering_done)
669 g_main_context_iteration (NULL, TRUE);
670 g_cancellable_reset (global_cancellable);
633671
634672 g_assert (lagent_candidate_gathering_done);
635673 g_assert (ragent_candidate_gathering_done);
663701 NiceAgent *lagent = NULL, *ragent = NULL;
664702 GThread *stun_thread = NULL;
665703 NiceAddress baseaddr;
704 GSource *src;
666705
667706 g_type_init();
668707
669 global_mainloop = g_main_loop_new (NULL, FALSE);
708 global_cancellable = g_cancellable_new ();
709 src = g_cancellable_source_new (global_cancellable);
710 g_source_set_dummy_callback (src);
711 g_source_attach (src, NULL);
712 g_source_unref (src);
670713
671714 #if !GLIB_CHECK_VERSION(2,31,8)
672715 g_thread_init (NULL);
673 stun_thread = g_thread_create (stun_thread_func,
674 global_mainloop,
675 TRUE, NULL);
716 stun_thread = g_thread_create (stun_thread_func, NULL, TRUE, NULL);
676717 stun_mutex_ptr = g_mutex_new ();
677718 stun_signal_ptr = g_cond_new ();
678719 #else
685726 g_mutex_lock (stun_thread_mutex_ptr);
686727 g_cond_wait (stun_thread_signal_ptr, stun_thread_mutex_ptr);
687728
688 lagent = nice_agent_new (g_main_loop_get_context (global_mainloop),
689 NICE_COMPATIBILITY_RFC5245);
690 ragent = nice_agent_new (g_main_loop_get_context (global_mainloop),
691 NICE_COMPATIBILITY_RFC5245);
729 lagent = nice_agent_new (NULL, NICE_COMPATIBILITY_RFC5245);
730 ragent = nice_agent_new (NULL, NICE_COMPATIBILITY_RFC5245);
692731
693732 g_object_set (G_OBJECT (lagent), "controlling-mode", TRUE, NULL);
694733 g_object_set (G_OBJECT (ragent), "controlling-mode", FALSE, NULL);
733772 g_mutex_free (stun_mutex_ptr);
734773 g_cond_free (stun_signal_ptr);
735774 #endif
736 g_main_loop_unref (global_mainloop);
775 g_object_unref (global_cancellable);
737776
738777 return 0;
739778 }