tests: Fix horribly raciness in test-new-dribble
Olivier CrĂȘte
10 years ago
72 | 72 | static GCond *stun_thread_signal_ptr = &stun_thread_signal; |
73 | 73 | #endif |
74 | 74 | |
75 | static GMainLoop *global_mainloop; | |
76 | 75 | static NiceComponentState global_lagent_state = NICE_COMPONENT_STATE_LAST; |
77 | 76 | static NiceComponentState global_ragent_state = NICE_COMPONENT_STATE_LAST; |
77 | static GCancellable *global_cancellable; | |
78 | 78 | static gboolean exit_stun_thread = FALSE; |
79 | 79 | static gboolean lagent_candidate_gathering_done = FALSE; |
80 | 80 | static gboolean ragent_candidate_gathering_done = FALSE; |
81 | 81 | static guint global_ls_id, global_rs_id; |
82 | 82 | static gboolean data_received = FALSE; |
83 | 83 | static gboolean drop_stun_packets = FALSE; |
84 | static gboolean got_stun_packet = FALSE; | |
85 | static gboolean send_stun = FALSE; | |
84 | 86 | |
85 | 87 | static const uint16_t known_attributes[] = { |
86 | 88 | 0 |
203 | 205 | buf_len = stun_agent_finish_message (agent, &response, NULL, 0); |
204 | 206 | |
205 | 207 | send_buf: |
206 | g_main_loop_quit (global_mainloop); | |
208 | g_cancellable_cancel (global_cancellable); | |
207 | 209 | g_debug ("Ready to send a STUN response"); |
208 | 210 | 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) { | |
210 | 213 | g_debug ("Waiting for signal. State is %d", global_lagent_state); |
211 | 214 | g_cond_wait (stun_signal_ptr, stun_mutex_ptr); |
212 | 215 | } |
281 | 284 | g_debug ("ragent finished gathering candidates"); |
282 | 285 | ragent_candidate_gathering_done = TRUE; |
283 | 286 | } |
284 | g_main_loop_quit(global_mainloop); | |
287 | g_cancellable_cancel (global_cancellable); | |
285 | 288 | } |
286 | 289 | |
287 | 290 | static void cb_nice_recv (NiceAgent *agent, guint stream_id, guint component_id, guint len, gchar *buf, gpointer user_data) |
298 | 301 | g_debug ("test-dribblemode:%s: ragent recieved %d bytes : quit mainloop", |
299 | 302 | G_STRFUNC, len); |
300 | 303 | data_received = TRUE; |
301 | g_main_loop_quit (global_mainloop); | |
304 | g_cancellable_cancel (global_cancellable); | |
302 | 305 | } |
303 | 306 | } |
304 | 307 | |
319 | 322 | if (GPOINTER_TO_UINT(data) == 1 && state == NICE_COMPONENT_STATE_FAILED) { |
320 | 323 | g_debug ("Signalling STUN response since connchecks failed"); |
321 | 324 | g_mutex_lock (stun_mutex_ptr); |
325 | send_stun = TRUE; | |
322 | 326 | g_cond_signal (stun_signal_ptr); |
323 | 327 | g_mutex_unlock (stun_mutex_ptr); |
324 | g_main_loop_quit (global_mainloop); | |
328 | g_cancellable_cancel (global_cancellable); | |
325 | 329 | } |
326 | 330 | |
327 | 331 | if(GPOINTER_TO_UINT(data) == 1 && state == NICE_COMPONENT_STATE_READY) { |
345 | 349 | |
346 | 350 | if (signal_stun_reply) { |
347 | 351 | g_mutex_lock (stun_mutex_ptr); |
352 | send_stun = TRUE; | |
348 | 353 | g_cond_signal (stun_signal_ptr); |
349 | 354 | g_mutex_unlock (stun_mutex_ptr); |
350 | 355 | } |
443 | 448 | } |
444 | 449 | |
445 | 450 | data_received = FALSE; |
451 | got_stun_packet = FALSE; | |
452 | send_stun = FALSE; | |
446 | 453 | |
447 | 454 | 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 (), | |
449 | 456 | cb_nice_recv, LEFT_AGENT); |
450 | 457 | 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 (), | |
452 | 459 | cb_nice_recv, RIGHT_AGENT); |
453 | 460 | } |
454 | 461 | |
464 | 471 | { |
465 | 472 | g_debug ("test-dribblemode:%s", G_STRFUNC); |
466 | 473 | |
474 | got_stun_packet = FALSE; | |
467 | 475 | init_test (lagent, ragent, FALSE); |
468 | 476 | |
469 | 477 | 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); | |
471 | 480 | g_assert (global_lagent_state == NICE_COMPONENT_STATE_GATHERING && |
472 | 481 | !lagent_candidate_gathering_done); |
473 | 482 | |
474 | 483 | 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); | |
479 | 488 | |
480 | 489 | set_credentials (lagent, global_ls_id, ragent, global_rs_id); |
481 | 490 | |
484 | 493 | lagent, global_ls_id, |
485 | 494 | TRUE); |
486 | 495 | |
487 | g_main_loop_run (global_mainloop); | |
496 | while (!data_received) | |
497 | g_main_context_iteration (NULL, TRUE); | |
498 | g_cancellable_reset (global_cancellable); | |
488 | 499 | g_assert (global_lagent_state >= NICE_COMPONENT_STATE_CONNECTED && |
489 | 500 | data_received); |
490 | 501 | |
492 | 503 | swap_candidates (lagent, global_ls_id, |
493 | 504 | ragent, global_rs_id, |
494 | 505 | 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); | |
496 | 509 | |
497 | 510 | g_assert (lagent_candidate_gathering_done); |
498 | 511 | |
514 | 527 | "wrong2", "wrong2"); |
515 | 528 | |
516 | 529 | 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); | |
518 | 533 | g_assert (global_lagent_state == NICE_COMPONENT_STATE_GATHERING && |
519 | 534 | !lagent_candidate_gathering_done); |
520 | 535 | |
521 | 536 | 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); | |
526 | 541 | |
527 | 542 | swap_candidates (ragent, global_rs_id, |
528 | 543 | lagent, global_ls_id, |
529 | 544 | 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); | |
532 | 548 | |
533 | 549 | // Set the correct credentials and swap candidates |
534 | 550 | set_credentials (lagent, global_ls_id, ragent, global_rs_id); |
540 | 556 | ragent, global_rs_id, |
541 | 557 | FALSE); |
542 | 558 | |
543 | g_main_loop_run (global_mainloop); | |
559 | while (!data_received) | |
560 | g_main_context_iteration (NULL, TRUE); | |
561 | g_cancellable_reset (global_cancellable); | |
544 | 562 | |
545 | 563 | g_assert (data_received); |
546 | 564 | g_assert (global_lagent_state == NICE_COMPONENT_STATE_READY); |
547 | 565 | g_assert (global_ragent_state >= NICE_COMPONENT_STATE_CONNECTED); |
548 | 566 | |
549 | 567 | // 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 | ||
551 | 572 | g_assert (lagent_candidate_gathering_done); |
552 | 573 | |
553 | 574 | cleanup (lagent, ragent); |
562 | 583 | init_test (lagent, ragent, FALSE); |
563 | 584 | |
564 | 585 | 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); | |
566 | 589 | g_assert (global_lagent_state == NICE_COMPONENT_STATE_GATHERING && |
567 | 590 | !lagent_candidate_gathering_done); |
568 | 591 | |
569 | 592 | 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); | |
574 | 598 | |
575 | 599 | add_bad_candidate (lagent, global_ls_id, cand); |
576 | 600 | |
577 | 601 | // 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); | |
579 | 605 | |
580 | 606 | // 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); | |
582 | 610 | |
583 | 611 | g_assert (global_lagent_state == NICE_COMPONENT_STATE_FAILED && |
584 | 612 | !data_received); |
592 | 620 | ragent, global_rs_id, |
593 | 621 | FALSE); |
594 | 622 | |
595 | g_main_loop_run (global_mainloop); | |
623 | while (!data_received) | |
624 | g_main_context_iteration (NULL, TRUE); | |
625 | g_cancellable_reset (global_cancellable); | |
596 | 626 | |
597 | 627 | g_assert (lagent_candidate_gathering_done); |
598 | 628 | |
599 | g_assert (global_lagent_state == NICE_COMPONENT_STATE_READY); | |
629 | g_assert (global_lagent_state >= NICE_COMPONENT_STATE_CONNECTED); | |
600 | 630 | g_assert (global_ragent_state >= NICE_COMPONENT_STATE_CONNECTED); |
601 | 631 | |
602 | 632 | cleanup (lagent, ragent); |
610 | 640 | set_credentials (lagent, global_ls_id, ragent, global_rs_id); |
611 | 641 | |
612 | 642 | 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); | |
614 | 646 | g_assert (global_lagent_state == NICE_COMPONENT_STATE_GATHERING && |
615 | 647 | !lagent_candidate_gathering_done); |
616 | 648 | |
617 | 649 | 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); | |
621 | 653 | |
622 | 654 | // 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); | |
624 | 658 | g_assert (data_received); |
625 | 659 | |
626 | 660 | // Data arrived, signal STUN thread to send STUN response |
627 | 661 | g_mutex_lock (stun_mutex_ptr); |
662 | send_stun = TRUE; | |
628 | 663 | g_cond_signal (stun_signal_ptr); |
629 | 664 | g_mutex_unlock (stun_mutex_ptr); |
630 | 665 | |
631 | 666 | // 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); | |
633 | 671 | |
634 | 672 | g_assert (lagent_candidate_gathering_done); |
635 | 673 | g_assert (ragent_candidate_gathering_done); |
663 | 701 | NiceAgent *lagent = NULL, *ragent = NULL; |
664 | 702 | GThread *stun_thread = NULL; |
665 | 703 | NiceAddress baseaddr; |
704 | GSource *src; | |
666 | 705 | |
667 | 706 | g_type_init(); |
668 | 707 | |
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); | |
670 | 713 | |
671 | 714 | #if !GLIB_CHECK_VERSION(2,31,8) |
672 | 715 | 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); | |
676 | 717 | stun_mutex_ptr = g_mutex_new (); |
677 | 718 | stun_signal_ptr = g_cond_new (); |
678 | 719 | #else |
685 | 726 | g_mutex_lock (stun_thread_mutex_ptr); |
686 | 727 | g_cond_wait (stun_thread_signal_ptr, stun_thread_mutex_ptr); |
687 | 728 | |
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); | |
692 | 731 | |
693 | 732 | g_object_set (G_OBJECT (lagent), "controlling-mode", TRUE, NULL); |
694 | 733 | g_object_set (G_OBJECT (ragent), "controlling-mode", FALSE, NULL); |
733 | 772 | g_mutex_free (stun_mutex_ptr); |
734 | 773 | g_cond_free (stun_signal_ptr); |
735 | 774 | #endif |
736 | g_main_loop_unref (global_mainloop); | |
775 | g_object_unref (global_cancellable); | |
737 | 776 | |
738 | 777 | return 0; |
739 | 778 | } |