This repository has been archived by the owner on Apr 3, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
/
framer_test.go
87 lines (70 loc) · 1.65 KB
/
framer_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
package dcnet
import (
"io"
"io/ioutil"
"math/rand"
"strconv"
"testing"
"time"
)
// Interface assertions
var _ Framer = (*RTPFramer)(nil)
var _ FrameReader = (*RTPFrameReader)(nil)
var _ FrameWriter = (*RTPFrameWriter)(nil)
func TestRTPFramer(t *testing.T) {
testCases := []struct {
sequence string
}{
{""},
{"abc"},
{RandString(65535)},
}
for i, testCase := range testCases {
// TODO: make the pipe split the message
pr, pw := io.Pipe()
r, err := NewRTPFrameReader(pr)
if err != nil {
t.Fatalf("failed to create frame reader: %v", err)
}
defer r.Close()
t.Run("NewRTPFramer_"+strconv.Itoa(i), func(t *testing.T) {
w, err := NewRTPFrameWriter(len(testCase.sequence), pw)
if err != nil {
t.Fatalf("failed to create frame writer: %v", err)
}
defer w.Close()
done := make(chan struct{})
// Avoid extreme waiting time on blocking bugs
lim := time.AfterFunc(time.Second*2, func() {
panic("timeout")
})
defer lim.Stop()
go func() {
defer close(done)
i, err := w.Write([]byte(testCase.sequence))
if i != len(testCase.sequence) {
t.Fatalf("short write")
}
if err != nil {
t.Fatalf("failed to write: %v", err)
}
}()
result, err := ioutil.ReadAll(r)
if err != nil {
t.Fatalf("failed to read: %v", err)
}
<-done
if string(result) != testCase.sequence {
t.Errorf(string(result) + " != " + testCase.sequence)
}
})
}
}
func RandString(n int) string {
var letterRunes = []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ")
b := make([]rune, n)
for i := range b {
b[i] = letterRunes[rand.Intn(len(letterRunes))]
}
return string(b)
}