github.com/anacrolix/torrent@v1.61.0/webtorrent/transport_test.go (about)

     1  //go:build !js
     2  // +build !js
     3  
     4  package webtorrent
     5  
     6  import (
     7  	"testing"
     8  
     9  	"github.com/anacrolix/log"
    10  	qt "github.com/go-quicktest/qt"
    11  	"github.com/pion/webrtc/v4"
    12  )
    13  
    14  func TestClosingPeerConnectionDoesNotCloseUnopenedDataChannel(t *testing.T) {
    15  	var tc TrackerClient
    16  	pc, dc, _, err := tc.newOffer(log.Default, "", [20]byte{})
    17  	qt.Assert(t, qt.IsNil(err))
    18  	defer pc.Close()
    19  	defer dc.Close()
    20  	peerConnClosed := make(chan struct{})
    21  	pc.OnConnectionStateChange(func(state webrtc.PeerConnectionState) {
    22  		if state == webrtc.PeerConnectionStateClosed {
    23  			close(peerConnClosed)
    24  		}
    25  	})
    26  	dc.OnClose(func() {
    27  		// This should not be called because the DataChannel is never opened.
    28  		t.Fatal("DataChannel.OnClose handler called")
    29  	})
    30  	t.Logf("data channel ready state before close: %v", dc.ReadyState())
    31  	dc.OnError(func(err error) {
    32  		t.Logf("data channel error: %v", err)
    33  	})
    34  	pc.Close()
    35  	qt.Check(t, qt.Equals(dc.ReadyState(), webrtc.DataChannelStateClosed))
    36  	<-peerConnClosed
    37  }