github.com/pion/webrtc/v3@v3.2.24/ortc_datachannel_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  	"io"
    11  	"testing"
    12  	"time"
    13  
    14  	"github.com/pion/transport/v2/test"
    15  	"github.com/stretchr/testify/assert"
    16  )
    17  
    18  func TestDataChannel_ORTC_SCTPTransport(t *testing.T) {
    19  	lim := test.TimeOut(time.Second * 20)
    20  	defer lim.Stop()
    21  
    22  	report := test.CheckRoutines(t)
    23  	defer report()
    24  
    25  	stackA, stackB, err := newORTCPair()
    26  	assert.NoError(t, err)
    27  
    28  	getSelectedCandidatePairErrChan := make(chan error)
    29  	stackB.sctp.OnDataChannel(func(d *DataChannel) {
    30  		_, getSelectedCandidatePairErr := d.Transport().Transport().ICETransport().GetSelectedCandidatePair()
    31  		getSelectedCandidatePairErrChan <- getSelectedCandidatePairErr
    32  	})
    33  
    34  	assert.NoError(t, signalORTCPair(stackA, stackB))
    35  
    36  	var id uint16 = 1
    37  	_, err = stackA.api.NewDataChannel(stackA.sctp, &DataChannelParameters{
    38  		Label: "Foo",
    39  		ID:    &id,
    40  	})
    41  	assert.NoError(t, err)
    42  
    43  	assert.NoError(t, <-getSelectedCandidatePairErrChan)
    44  	assert.NoError(t, stackA.close())
    45  	assert.NoError(t, stackB.close())
    46  }
    47  
    48  func TestDataChannel_ORTCE2E(t *testing.T) {
    49  	lim := test.TimeOut(time.Second * 20)
    50  	defer lim.Stop()
    51  
    52  	report := test.CheckRoutines(t)
    53  	defer report()
    54  
    55  	stackA, stackB, err := newORTCPair()
    56  	assert.NoError(t, err)
    57  
    58  	awaitSetup := make(chan struct{})
    59  	awaitString := make(chan struct{})
    60  	awaitBinary := make(chan struct{})
    61  	stackB.sctp.OnDataChannel(func(d *DataChannel) {
    62  		close(awaitSetup)
    63  
    64  		d.OnMessage(func(msg DataChannelMessage) {
    65  			if msg.IsString {
    66  				close(awaitString)
    67  			} else {
    68  				close(awaitBinary)
    69  			}
    70  		})
    71  	})
    72  
    73  	assert.NoError(t, signalORTCPair(stackA, stackB))
    74  
    75  	var id uint16 = 1
    76  	dcParams := &DataChannelParameters{
    77  		Label: "Foo",
    78  		ID:    &id,
    79  	}
    80  	channelA, err := stackA.api.NewDataChannel(stackA.sctp, dcParams)
    81  	assert.NoError(t, err)
    82  
    83  	<-awaitSetup
    84  
    85  	assert.NoError(t, channelA.SendText("ABC"))
    86  	assert.NoError(t, channelA.Send([]byte("ABC")))
    87  
    88  	<-awaitString
    89  	<-awaitBinary
    90  
    91  	assert.NoError(t, stackA.close())
    92  	assert.NoError(t, stackB.close())
    93  
    94  	// attempt to send when channel is closed
    95  	assert.ErrorIs(t, channelA.Send([]byte("ABC")), io.ErrClosedPipe)
    96  	assert.ErrorIs(t, channelA.SendText("test"), io.ErrClosedPipe)
    97  	assert.ErrorIs(t, channelA.ensureOpen(), io.ErrClosedPipe)
    98  }