-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathmemqueue_test.go
98 lines (91 loc) · 1.84 KB
/
memqueue_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
package crawler
import (
"net/url"
"strconv"
"testing"
"time"
"github.com/fanyang01/crawler/queue"
"github.com/stretchr/testify/assert"
)
func mustParseURL(ur string) *url.URL {
u, err := url.Parse(ur)
if err != nil {
panic(err)
}
return u
}
func mustParseInt(s string) int {
i, err := strconv.ParseInt(s, 0, 32)
if err != nil {
panic(err)
}
return int(i)
}
func newTestCrawler() *Crawler {
opt := DefaultOption
store := NewMemStore()
ctrl := DefaultController
cw := &Crawler{
opt: opt,
store: store,
ctrl: ctrl,
}
return cw
}
func TestQueuePriority(t *testing.T) {
pq := NewMemQueue(100)
now := time.Now()
pq.Push(&queue.Item{
Score: 300,
URL: mustParseURL("/300"),
Next: now.Add(50 * time.Millisecond),
})
pq.Push(&queue.Item{
Score: 100,
URL: mustParseURL("/100"),
Next: now.Add(50 * time.Millisecond),
})
pq.Push(&queue.Item{
Score: 200,
URL: mustParseURL("/200"),
Next: now.Add(50 * time.Millisecond),
})
item, _ := pq.Pop()
assert.Equal(t, "/300", item.URL.Path)
item, _ = pq.Pop()
assert.Equal(t, "/200", item.URL.Path)
item, _ = pq.Pop()
assert.Equal(t, "/100", item.URL.Path)
}
func TestQueueTime(t *testing.T) {
wq := NewMemQueue(100)
now := time.Now()
items := []*queue.Item{
{
Next: now.Add(50 * time.Millisecond),
URL: mustParseURL("http://a.example.com/50"),
}, {
Next: now.Add(75 * time.Millisecond),
URL: mustParseURL("http://b.example.com/75"),
}, {
Next: now.Add(25 * time.Millisecond),
URL: mustParseURL("http://a.example.com/25"),
}, {
Next: now.Add(100 * time.Millisecond),
URL: mustParseURL("http://b.example.com/100"),
},
}
exp := []string{
"/25",
"/50",
"/75",
"/100",
}
for _, item := range items {
wq.Push(item)
}
for i := 0; i < len(items); i++ {
item, _ := wq.Pop()
assert.Equal(t, exp[i], item.URL.Path)
}
}