This repository has been archived by the owner on May 27, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 17
/
checklist_test.go
191 lines (183 loc) · 3.47 KB
/
checklist_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
184
185
186
187
188
189
190
191
package ice
import (
"bytes"
"encoding/json"
"testing"
"gortc.io/ice/candidate"
)
func TestChecklistJSON(t *testing.T) {
c := Checklist{
State: ChecklistCompleted,
Pairs: Pairs{
{
Local: Candidate{Priority: 102, Type: candidate.PeerReflexive},
Remote: Candidate{Priority: 91},
State: PairSucceeded,
},
{
Local: Candidate{Priority: 100, Type: candidate.Relayed},
Remote: Candidate{Priority: 50},
State: PairWaiting,
},
{
Local: Candidate{Priority: 103},
Remote: Candidate{Priority: 93},
State: PairFrozen,
},
{
Local: Candidate{Priority: 104},
Remote: Candidate{Priority: 94},
State: PairFailed,
},
{
Local: Candidate{Priority: 101},
Remote: Candidate{Priority: 90},
State: PairInProgress,
},
},
}
buf := new(bytes.Buffer)
e := json.NewEncoder(buf)
if err := e.Encode(c); err != nil {
t.Fatal(err)
}
d := json.NewDecoder(buf)
var cGot Checklist
if err := d.Decode(&cGot); err != nil {
t.Fatal(err)
}
if !cGot.Equal(c) {
t.Error("not equal")
}
}
func TestChecklist_Order(t *testing.T) {
c := Checklist{
Pairs: Pairs{
{Priority: 1},
{Priority: 10},
},
}
c.Sort()
if c.Pairs[0].Priority == 1 {
t.Error("pair with 1 priority should be second")
}
}
func TestChecklist_ComputePriorities(t *testing.T) {
c := Checklist{
Pairs: Pairs{
{
Local: Candidate{Priority: 102},
Remote: Candidate{Priority: 91},
},
{
Local: Candidate{Priority: 100},
Remote: Candidate{Priority: 50},
},
{
Local: Candidate{Priority: 103},
Remote: Candidate{Priority: 93},
},
{
Local: Candidate{Priority: 104},
Remote: Candidate{Priority: 94},
},
{
Local: Candidate{Priority: 101},
Remote: Candidate{Priority: 90},
},
},
}
expectedControlled := []int64{
390842024140, 214748365000, 399431958734, 403726926032, 386547056842,
}
expectedControlling := []int64{
390842024141, 214748365001, 399431958735, 403726926033, 386547056843,
}
c.ComputePriorities(Controlled)
for i, p := range c.Pairs {
e := expectedControlled[i]
if e != p.Priority {
t.Errorf("controlled: [%d] %d (got) != %d (expected)",
i, p.Priority, e,
)
}
}
c.ComputePriorities(Controlling)
for i, p := range c.Pairs {
e := expectedControlling[i]
if e != p.Priority {
t.Errorf("controlling: [%d] %d (got) != %d (expected)",
i, p.Priority, e,
)
}
}
}
func TestChecklist_Prune(t *testing.T) {
c := Checklist{
Pairs: Pairs{
// TODO: Improve this
{
Local: Candidate{},
Remote: Candidate{},
},
{
Local: Candidate{},
Remote: Candidate{},
},
{
Local: Candidate{},
Remote: Candidate{},
},
{
Local: Candidate{},
Remote: Candidate{},
},
{
Local: Candidate{},
Remote: Candidate{},
},
},
}
c.Prune()
if len(c.Pairs) != 1 {
t.Error("unexpected result length")
}
}
func TestChecklist_Limit(t *testing.T) {
c := Checklist{
Pairs: Pairs{
{
Priority: 100,
},
{
Priority: 99,
},
{
Priority: 98,
},
{
Priority: 97,
},
{
Priority: 96,
},
},
}
c.Limit(10)
if c.Len() != 5 {
t.Error("unexpected length")
}
c.Limit(3)
if c.Len() != 3 {
t.Error("unexpected length")
}
}
func TestChecklistState_String(t *testing.T) {
for _, s := range []ChecklistState{
ChecklistRunning, ChecklistCompleted, ChecklistFailed,
} {
if s.String() == "" {
t.Errorf("checklist iota %d should have String() value", int(s))
}
}
}