-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathafec_test.go
157 lines (144 loc) · 2.57 KB
/
afec_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
package afec
import (
"testing"
"time"
"bou.ke/monkey"
"github.com/stretchr/testify/require"
)
func Test_Base(t *testing.T) {
monkey.Patch((*afec).fec, func(_ *afec, pl float64) (dataBlocks, parityBlocks uint8) {
return 1, 1
})
require.True(t, debug)
type suit struct {
send [][]byte
recv [][]byte
delay []time.Duration
msg string
}
var tail_zero_suits = []suit{
{
send: [][]byte{
{1, 2, 3},
{1},
{2, 3, 0, 0},
},
msg: "test-zero-tail-case1",
},
{
send: [][]byte{
{1, 2, 3, 0},
{1, 0},
{2, 3, 0, 0},
},
msg: "test-zero-tail-case2",
},
{
send: [][]byte{
{1, 2, 3},
{1},
{0, 0, 0, 0},
},
msg: "test-zero-pack-case3",
},
{
send: [][]byte{
{0, 0, 0},
{0},
{0, 0, 0, 0},
},
msg: "test-zero-pack-case4",
},
}
var reconstruct_case = []suit{
{
send: [][]byte{
{0, 0, 0},
{1}, {2}, {3},
{0, 0, 0, 0},
},
delay: []time.Duration{time.Minute},
recv: [][]byte{
{1}, {2}, {3},
{0, 0, 0},
{0, 0, 0, 0},
},
msg: "test-reconstruct-case1",
},
{
send: [][]byte{
{0, 0, 0},
{1}, {2}, {3},
{0, 0, 0, 0},
},
delay: []time.Duration{time.Minute, 0, time.Minute},
recv: [][]byte{
{2}, {3},
{0, 0, 0},
{0, 0, 0, 0},
},
msg: "test-reconstruct-case2",
},
{
send: [][]byte{
{0, 0, 0},
{1}, {2}, {3},
{0, 0, 0, 0},
{9},
},
delay: []time.Duration{time.Minute, 0, time.Minute},
recv: [][]byte{
{2}, {3},
{0, 0, 0},
{0, 0, 0, 0},
{1},
{9},
},
msg: "test-reconstruct-case3",
},
}
suits := []suit{}
suits = append(suits, tail_zero_suits...)
suits = append(suits, reconstruct_case...)
for _, suit := range suits {
if suit.recv == nil {
suit.recv = suit.send
}
if suit.msg == `test-reconstruct-case2` {
print()
}
c, s := NewMockUDPConn(func() func() time.Duration {
var i int
return func() time.Duration {
i++
if i > len(suit.delay) {
return 0
}
return suit.delay[i-1]
}
}())
go func() { // sender
conn := NewAfec(c)
for _, p := range suit.send {
_, err := conn.Write(gcap(p))
require.NoError(t, err, suit.msg)
}
}()
{ // recver
conn := NewAfec(s)
var b = make([]byte, 1532)
for _, r := range suit.recv {
b = b[:1500]
n, err := conn.Read(b)
require.NoError(t, err, suit.msg)
require.Equal(t, b[:n], r, suit.msg)
require.True(t, isEmpty(b[n:cap(b)]), suit.msg)
}
}
}
}
func gcap(b []byte) []byte {
tmp := make([]byte, len(b), len(b)+HdrSize)
copy(tmp, b)
return tmp
}