github.com/pion/webrtc/v3@v3.2.24/vnet_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  	"testing"
    11  	"time"
    12  
    13  	"github.com/pion/logging"
    14  	"github.com/pion/transport/v2/vnet"
    15  	"github.com/stretchr/testify/assert"
    16  )
    17  
    18  func createVNetPair(t *testing.T) (*PeerConnection, *PeerConnection, *vnet.Router) {
    19  	// Create a root router
    20  	wan, err := vnet.NewRouter(&vnet.RouterConfig{
    21  		CIDR:          "1.2.3.0/24",
    22  		LoggerFactory: logging.NewDefaultLoggerFactory(),
    23  	})
    24  	assert.NoError(t, err)
    25  
    26  	// Create a network interface for offerer
    27  	offerVNet, err := vnet.NewNet(&vnet.NetConfig{
    28  		StaticIPs: []string{"1.2.3.4"},
    29  	})
    30  	assert.NoError(t, err)
    31  
    32  	// Add the network interface to the router
    33  	assert.NoError(t, wan.AddNet(offerVNet))
    34  
    35  	offerSettingEngine := SettingEngine{}
    36  	offerSettingEngine.SetVNet(offerVNet)
    37  	offerSettingEngine.SetICETimeouts(time.Second, time.Second, time.Millisecond*200)
    38  
    39  	// Create a network interface for answerer
    40  	answerVNet, err := vnet.NewNet(&vnet.NetConfig{
    41  		StaticIPs: []string{"1.2.3.5"},
    42  	})
    43  	assert.NoError(t, err)
    44  
    45  	// Add the network interface to the router
    46  	assert.NoError(t, wan.AddNet(answerVNet))
    47  
    48  	answerSettingEngine := SettingEngine{}
    49  	answerSettingEngine.SetVNet(answerVNet)
    50  	answerSettingEngine.SetICETimeouts(time.Second, time.Second, time.Millisecond*200)
    51  
    52  	// Start the virtual network by calling Start() on the root router
    53  	assert.NoError(t, wan.Start())
    54  
    55  	offerMediaEngine := &MediaEngine{}
    56  	assert.NoError(t, offerMediaEngine.RegisterDefaultCodecs())
    57  	offerPeerConnection, err := NewAPI(WithSettingEngine(offerSettingEngine), WithMediaEngine(offerMediaEngine)).NewPeerConnection(Configuration{})
    58  	assert.NoError(t, err)
    59  
    60  	answerMediaEngine := &MediaEngine{}
    61  	assert.NoError(t, answerMediaEngine.RegisterDefaultCodecs())
    62  	answerPeerConnection, err := NewAPI(WithSettingEngine(answerSettingEngine), WithMediaEngine(answerMediaEngine)).NewPeerConnection(Configuration{})
    63  	assert.NoError(t, err)
    64  
    65  	return offerPeerConnection, answerPeerConnection, wan
    66  }