github.com/pion/webrtc/v3@v3.2.24/icetransport_test.go (about) 1 // SPDX-FileCopyrightText: 2023 The Pion community <https://pion.ly> 2 // SPDX-License-Identifier: MIT 3 4 //go:build !js 5 // +build !js 6 7 package webrtc 8 9 import ( 10 "sync" 11 "sync/atomic" 12 "testing" 13 "time" 14 15 "github.com/pion/transport/v2/test" 16 "github.com/stretchr/testify/assert" 17 ) 18 19 func TestICETransport_OnConnectionStateChange(t *testing.T) { 20 report := test.CheckRoutines(t) 21 defer report() 22 23 lim := test.TimeOut(time.Second * 30) 24 defer lim.Stop() 25 26 pcOffer, pcAnswer, err := newPair() 27 assert.NoError(t, err) 28 29 var ( 30 iceComplete sync.WaitGroup 31 peerConnectionConnected sync.WaitGroup 32 ) 33 iceComplete.Add(2) 34 peerConnectionConnected.Add(2) 35 36 onIceComplete := func(s ICETransportState) { 37 if s == ICETransportStateConnected { 38 iceComplete.Done() 39 } 40 } 41 pcOffer.SCTP().Transport().ICETransport().OnConnectionStateChange(onIceComplete) 42 pcAnswer.SCTP().Transport().ICETransport().OnConnectionStateChange(onIceComplete) 43 44 onConnected := func(s PeerConnectionState) { 45 if s == PeerConnectionStateConnected { 46 peerConnectionConnected.Done() 47 } 48 } 49 pcOffer.OnConnectionStateChange(onConnected) 50 pcAnswer.OnConnectionStateChange(onConnected) 51 52 assert.NoError(t, signalPair(pcOffer, pcAnswer)) 53 iceComplete.Wait() 54 peerConnectionConnected.Wait() 55 56 closePairNow(t, pcOffer, pcAnswer) 57 } 58 59 func TestICETransport_OnSelectedCandidatePairChange(t *testing.T) { 60 report := test.CheckRoutines(t) 61 defer report() 62 63 lim := test.TimeOut(time.Second * 30) 64 defer lim.Stop() 65 66 pcOffer, pcAnswer, err := newPair() 67 assert.NoError(t, err) 68 69 iceComplete := make(chan bool) 70 pcAnswer.OnICEConnectionStateChange(func(iceState ICEConnectionState) { 71 if iceState == ICEConnectionStateConnected { 72 time.Sleep(3 * time.Second) 73 close(iceComplete) 74 } 75 }) 76 77 senderCalledCandidateChange := int32(0) 78 pcOffer.SCTP().Transport().ICETransport().OnSelectedCandidatePairChange(func(pair *ICECandidatePair) { 79 atomic.StoreInt32(&senderCalledCandidateChange, 1) 80 }) 81 82 assert.NoError(t, signalPair(pcOffer, pcAnswer)) 83 <-iceComplete 84 85 if atomic.LoadInt32(&senderCalledCandidateChange) == 0 { 86 t.Fatalf("Sender ICETransport OnSelectedCandidateChange was never called") 87 } 88 89 closePairNow(t, pcOffer, pcAnswer) 90 } 91 92 func TestICETransport_GetSelectedCandidatePair(t *testing.T) { 93 offerer, answerer, err := newPair() 94 assert.NoError(t, err) 95 96 peerConnectionConnected := untilConnectionState(PeerConnectionStateConnected, offerer, answerer) 97 98 offererSelectedPair, err := offerer.SCTP().Transport().ICETransport().GetSelectedCandidatePair() 99 assert.NoError(t, err) 100 assert.Nil(t, offererSelectedPair) 101 102 answererSelectedPair, err := answerer.SCTP().Transport().ICETransport().GetSelectedCandidatePair() 103 assert.NoError(t, err) 104 assert.Nil(t, answererSelectedPair) 105 106 assert.NoError(t, signalPair(offerer, answerer)) 107 peerConnectionConnected.Wait() 108 109 offererSelectedPair, err = offerer.SCTP().Transport().ICETransport().GetSelectedCandidatePair() 110 assert.NoError(t, err) 111 assert.NotNil(t, offererSelectedPair) 112 113 answererSelectedPair, err = answerer.SCTP().Transport().ICETransport().GetSelectedCandidatePair() 114 assert.NoError(t, err) 115 assert.NotNil(t, answererSelectedPair) 116 117 closePairNow(t, offerer, answerer) 118 } 119 120 func TestICETransport_GetLocalParameters(t *testing.T) { 121 offerer, answerer, err := newPair() 122 assert.NoError(t, err) 123 124 peerConnectionConnected := untilConnectionState(PeerConnectionStateConnected, offerer, answerer) 125 126 assert.NoError(t, signalPair(offerer, answerer)) 127 peerConnectionConnected.Wait() 128 129 localParameters, err := offerer.SCTP().Transport().ICETransport().GetLocalParameters() 130 assert.NoError(t, err) 131 assert.NotEqual(t, localParameters.UsernameFragment, "") 132 assert.NotEqual(t, localParameters.Password, "") 133 134 closePairNow(t, offerer, answerer) 135 }