-
Notifications
You must be signed in to change notification settings - Fork 43
/
sctp_test.go
183 lines (146 loc) · 4.55 KB
/
sctp_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
package mediasoup
import (
"fmt"
"net"
"strconv"
"sync/atomic"
"testing"
"time"
"github.com/pion/logging"
"github.com/pion/sctp"
"github.com/stretchr/testify/suite"
)
var sctpSendStreamId uint16
func TestSctpTestingSuite(t *testing.T) {
suite.Run(t, new(SctpTestingSuite))
}
type SctpTestingSuite struct {
TestingSuite
worker *Worker
router *Router
dataProducer *DataProducer
dataConsumer *DataConsumer
stcpStream *sctp.Stream
}
func (suite *SctpTestingSuite) SetupTest() {
var err error
suite.worker = CreateTestWorker()
suite.router = CreateRouter(suite.worker)
suite.NoError(err)
transport, err := suite.router.CreatePlainTransport(PlainTransportOptions{
ListenIp: TransportListenIp{
Ip: "0.0.0.0",
AnnouncedIp: "127.0.0.1",
},
Comedia: true,
EnableSctp: true,
NumSctpStreams: NumSctpStreams{OS: 256, MIS: 256},
})
suite.NoError(err)
remoteUdpIp := transport.Tuple().LocalIp
remoteUdpPort := transport.Tuple().LocalPort
conn, err := net.Dial("udp", fmt.Sprintf("%s:%d", remoteUdpIp, remoteUdpPort))
suite.NoError(err)
config := sctp.Config{
NetConn: conn,
LoggerFactory: logging.NewDefaultLoggerFactory(),
}
association, err := sctp.Client(config)
suite.NoError(err)
// Create an explicit SCTP outgoing stream with id 123 (id 0 is already used
// by the implicit SCTP outgoing stream built-in the SCTP socket).
sctpSendStreamId = uint16(123)
stream, err := association.OpenStream(sctpSendStreamId, sctp.PayloadTypeWebRTCBinary)
suite.NoError(err)
suite.stcpStream = stream
// Create a DataProducer with the corresponding SCTP stream id.
dataProducer, err := transport.ProduceData(DataProducerOptions{
SctpStreamParameters: &SctpStreamParameters{
StreamId: sctpSendStreamId,
Ordered: Bool(true),
},
Label: "go-sctp",
Protocol: "foo & bar 😀😀😀",
})
suite.NoError(err)
suite.dataProducer = dataProducer
transport2, err := suite.router.CreateDirectTransport()
suite.NoError(err)
// Create a DataConsumer to receive messages from the DataProducer over the
// direct transport.
dataConsumer, err := transport2.ConsumeData(DataConsumerOptions{
DataProducerId: dataProducer.Id(),
})
suite.NoError(err)
suite.dataConsumer = dataConsumer
}
func (suite *SctpTestingSuite) TearDownTest() {
suite.stcpStream.Close()
suite.worker.Close()
}
func (suite *SctpTestingSuite) TestOrderedDataProducerDeliversAllSCTPMessagesToTheDataConsumer() {
numMessages := uint32(200)
sentMessageBytes := uint32(0)
recvMessageBytes := uint32(0)
lastSentMessageId := uint32(0)
lastRecvMessageId := uint32(0)
done := make(chan struct{})
suite.dataConsumer.On("message", func(payload []byte, ppid int) {
recvMessageBytes += uint32(len(payload))
id64, err := strconv.ParseUint(string(payload), 10, 32)
suite.NoError(err)
id := uint32(id64)
if id == numMessages {
close(done)
}
if id < numMessages/2 {
suite.EqualValues(sctp.PayloadTypeWebRTCString, ppid)
} else {
suite.EqualValues(sctp.PayloadTypeWebRTCBinary, ppid)
}
suite.Equal(atomic.AddUint32(&lastRecvMessageId, 1), id)
})
for {
time.Sleep(time.Millisecond)
lastSentMessageId++
data := []byte(fmt.Sprintf("%d", lastSentMessageId))
payloadType := sctp.PayloadTypeWebRTCBinary
if lastSentMessageId < numMessages/2 {
payloadType = sctp.PayloadTypeWebRTCString
}
n, err := suite.stcpStream.WriteSCTP(data, payloadType)
suite.NoError(err)
sentMessageBytes += uint32(n)
if lastSentMessageId == numMessages {
break
}
}
select {
case <-done:
case <-time.NewTimer(2 * time.Duration(numMessages) * time.Millisecond).C:
suite.FailNow("timeout")
}
suite.EqualValues(lastSentMessageId, numMessages)
suite.EqualValues(lastRecvMessageId, numMessages)
suite.EqualValues(sentMessageBytes, recvMessageBytes)
dataProducerStats, err := suite.dataProducer.GetStats()
suite.NoError(err)
suite.Equal(&DataProducerStat{
Type: "data-producer",
Timestamp: dataProducerStats[0].Timestamp,
Label: suite.dataProducer.Label(),
Protocol: suite.dataProducer.Protocol(),
MessagesReceived: int64(numMessages),
BytesReceived: int64(sentMessageBytes),
}, dataProducerStats[0])
dataConumserStats, err := suite.dataConsumer.GetStats()
suite.NoError(err)
suite.Equal(&DataConsumerStat{
Type: "data-consumer",
Timestamp: dataConumserStats[0].Timestamp,
Label: suite.dataConsumer.Label(),
Protocol: suite.dataConsumer.Protocol(),
MessagesSent: int64(numMessages),
BytesSent: int64(sentMessageBytes),
}, dataConumserStats[0])
}