forked from moby/spdystream
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpriority_test.go
107 lines (96 loc) · 2.49 KB
/
priority_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
package spdystream
import (
"code.google.com/p/go.net/spdy"
"sync"
"testing"
"time"
)
func TestPriorityQueueOrdering(t *testing.T) {
queue := NewPriorityFrameQueue(150)
data1 := &spdy.DataFrame{}
data2 := &spdy.DataFrame{}
data3 := &spdy.DataFrame{}
data4 := &spdy.DataFrame{}
queue.Push(data1, 2)
queue.Push(data2, 1)
queue.Push(data3, 1)
queue.Push(data4, 0)
if queue.Pop() != data4 {
t.Fatalf("Wrong order, expected data4 first")
}
if queue.Pop() != data2 {
t.Fatalf("Wrong order, expected data2 second")
}
if queue.Pop() != data3 {
t.Fatalf("Wrong order, expected data3 third")
}
if queue.Pop() != data1 {
t.Fatalf("Wrong order, expected data1 fourth")
}
// Insert 50 Medium priority frames
for i := spdy.StreamId(50); i < 100; i++ {
queue.Push(&spdy.DataFrame{StreamId: i}, 1)
}
// Insert 50 low priority frames
for i := spdy.StreamId(100); i < 150; i++ {
queue.Push(&spdy.DataFrame{StreamId: i}, 2)
}
// Insert 50 high priority frames
for i := spdy.StreamId(0); i < 50; i++ {
queue.Push(&spdy.DataFrame{StreamId: i}, 0)
}
for i := spdy.StreamId(0); i < 150; i++ {
frame := queue.Pop()
if frame.(*spdy.DataFrame).StreamId != i {
t.Fatalf("Wrong frame\nActual: %d\nExpecting: %d", frame.(*spdy.DataFrame).StreamId, i)
}
}
}
func TestPriorityQueueSync(t *testing.T) {
queue := NewPriorityFrameQueue(150)
var wg sync.WaitGroup
insertRange := func(start, stop spdy.StreamId, priority uint8) {
for i := start; i < stop; i++ {
queue.Push(&spdy.DataFrame{StreamId: i}, priority)
}
wg.Done()
}
wg.Add(3)
go insertRange(spdy.StreamId(100), spdy.StreamId(150), 2)
go insertRange(spdy.StreamId(0), spdy.StreamId(50), 0)
go insertRange(spdy.StreamId(50), spdy.StreamId(100), 1)
wg.Wait()
for i := spdy.StreamId(0); i < 150; i++ {
frame := queue.Pop()
if frame.(*spdy.DataFrame).StreamId != i {
t.Fatalf("Wrong frame\nActual: %d\nExpecting: %d", frame.(*spdy.DataFrame).StreamId, i)
}
}
}
func TestPriorityQueueBlocking(t *testing.T) {
queue := NewPriorityFrameQueue(15)
for i := 0; i < 15; i++ {
queue.Push(&spdy.DataFrame{}, 2)
}
doneChan := make(chan bool)
go func() {
queue.Push(&spdy.DataFrame{}, 2)
close(doneChan)
}()
select {
case <-doneChan:
t.Fatalf("Push succeeded, expected to block")
case <-time.After(time.Millisecond):
break
}
queue.Pop()
select {
case <-doneChan:
break
case <-time.After(time.Millisecond):
t.Fatalf("Push should have succeeded, but timeout reached")
}
for i := 0; i < 15; i++ {
queue.Pop()
}
}