forked from PuerkitoBio/gocrawl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
complex_test.go
238 lines (202 loc) · 6.57 KB
/
complex_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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
package gocrawl
import (
"net/http"
"net/http/httptest"
"net/url"
"strings"
"testing"
"time"
)
func testNoCrawlDelay(t *testing.T, tc *testCase, buf bool) {
const MaxTime = 15 * time.Millisecond // Give some buffer when running with -race
ff := newFileFetcher()
spy := newSpy(ff, buf)
opts := NewOptions(spy)
opts.SameHostOnly = true
opts.CrawlDelay = 0
c := NewCrawlerWithOptions(opts)
start := time.Now()
c.Run([]string{
"http://hosta/page1.html",
"http://hosta/page4.html",
})
elps := time.Now().Sub(start)
assertTrue(elps <= MaxTime, "expected elapsed time to be at most %v, got %v", MaxTime, elps)
assertCallCount(spy, tc.name, eMKVisit, 5, t)
assertCallCount(spy, tc.name, eMKFilter, 13, t)
}
func testNoExtender(t *testing.T, tc *testCase, buf bool) {
defer assertPanic(tc.name, t)
c := NewCrawler(nil)
c.Options.CrawlDelay = DefaultTestCrawlDelay
c.Options.LogFlags = LogError | LogTrace
c.Run(nil)
}
func testCrawlDelay(t *testing.T, tc *testCase, buf bool) {
var last time.Time
var since []time.Duration
cnt := 0
ff := newFileFetcher()
spy := newSpy(ff, buf)
spy.setExtensionMethod(eMKFetch, func(ctx *URLContext, agent string, head bool) (*http.Response, error) {
since = append(since, time.Now().Sub(last))
last = time.Now()
return ff.Fetch(ctx, agent, head)
})
spy.setExtensionMethod(eMKComputeDelay, func(host string, di *DelayInfo, lastFetch *FetchInfo) time.Duration {
// Crawl delay always grows
cnt++
return time.Duration(int(di.OptsDelay) * cnt)
})
opts := NewOptions(spy)
opts.SameHostOnly = true
opts.CrawlDelay = DefaultTestCrawlDelay
opts.HeadBeforeGet = true
opts.LogFlags = LogAll
c := NewCrawlerWithOptions(opts)
last = time.Now()
c.Run("http://hosta/page1.html")
assertCallCount(spy, tc.name, eMKFetch, 7, t)
assertCallCount(spy, tc.name, eMKComputeDelay, 7, t)
for i, d := range since {
min := (DefaultTestCrawlDelay * time.Duration(i))
assertTrue(d >= min, "expected a delay of at least %v for fetch #%d, got %v.", min, i, d)
}
}
func testUserAgent(t *testing.T, tc *testCase, buf bool) {
// Create crawler, with all defaults
c := NewCrawler(new(DefaultExtender))
c.Options.CrawlDelay = 10 * time.Millisecond
// Create server
mux := http.NewServeMux()
mux.HandleFunc("/robots.txt", func(w http.ResponseWriter, r *http.Request) {
// Expect robots.txt user agent
assertTrue(r.UserAgent() == c.Options.UserAgent, "expected user-agent %s, got %s", c.Options.UserAgent, r.UserAgent())
})
mux.HandleFunc("/bidon", func(w http.ResponseWriter, r *http.Request) {
// Expect crawl user agent
assertTrue(r.UserAgent() == c.Options.UserAgent, "expected user-agent %s, got %s", c.Options.UserAgent, r.UserAgent())
})
srv := httptest.NewServer(mux)
defer srv.Close()
c.Run(srv.URL + "/bidon")
}
func testRunTwiceSameInstance(t *testing.T, tc *testCase, buf bool) {
ff := newFileFetcher()
spy := newSpy(ff, buf)
opts := NewOptions(spy)
opts.SameHostOnly = true
opts.CrawlDelay = DefaultTestCrawlDelay
opts.LogFlags = LogAll
c := NewCrawlerWithOptions(opts)
c.Run([]string{
"http://hosta/page1.html",
"http://hosta/page4.html",
})
assertCallCount(spy, tc.name, eMKVisit, 5, t)
assertCallCount(spy, tc.name, eMKFilter, 13, t)
spy = newSpy(ff, buf)
spy.setExtensionMethod(eMKFilter, func(ctx *URLContext, isVisited bool) bool {
return !isVisited && strings.ToLower(ctx.url.Path) == "/page1.html"
})
opts.SameHostOnly = false
opts.Extender = spy
c.Run([]string{
"http://hosta/page1.html",
"http://hosta/page4.html",
"http://hostb/pageunlinked.html",
})
assertCallCount(spy, tc.name, eMKVisit, 3, t)
assertCallCount(spy, tc.name, eMKFilter, 11, t)
}
func testEnqueueChanEmbedded(t *testing.T, tc *testCase, buf bool) {
type MyExt struct {
SomeFieldBefore bool
*DefaultExtender
SomeFieldAfter int
}
me := &MyExt{false, new(DefaultExtender), 0}
c := NewCrawler(me)
assertTrue(me.EnqueueChan == nil, "expected EnqueueChan to be nil")
c.Run(nil)
assertTrue(me.EnqueueChan != nil, "expected EnqueueChan to be non-nil")
me.EnqueueChan <- "test"
l := len(me.EnqueueChan)
assertTrue(l == 1, "expected EnqueueChan to have 1 element, got %d", l)
}
func testEnqueueChanShadowed(t *testing.T, tc *testCase, buf bool) {
type ShadowExt struct {
*spyExtender
EnqueueChan int
}
me := &ShadowExt{
newSpy(new(DefaultExtender), buf),
0,
}
opts := NewOptions(me)
opts.LogFlags = LogInfo
c := NewCrawlerWithOptions(opts)
c.Run("")
assertIsInLog(tc.name, me.b, "extender.EnqueueChan is not of type chan<-interface{}, cannot set the enqueue channel\n", t)
}
func testEnqueueNewURL(t *testing.T, tc *testCase, buf bool) {
ff := newFileFetcher()
spy := newSpy(ff, buf)
spy.setExtensionMethod(eMKFilter, func(ctx *URLContext, isVisited bool) bool {
// Accept only non-visited Page1s
return !isVisited && strings.HasSuffix(strings.ToLower(ctx.url.Path), "page1.html")
})
enqueued := false
spy.setExtensionMethod(eMKEnqueued, func(ctx *URLContext) {
// Add hostc's Page1 to crawl
if !enqueued {
newU, err := url.Parse("http://hostc/page1.html")
if err != nil {
panic(err)
}
spy.EnqueueChan <- newU
enqueued = true
}
})
opts := NewOptions(spy)
opts.CrawlDelay = DefaultTestCrawlDelay
opts.LogFlags = LogAll
opts.SameHostOnly = false
c := NewCrawlerWithOptions(opts)
c.Run("http://hostb/page1.html")
assertCallCount(spy, tc.name, eMKFilter, 7, t)
assertCallCount(spy, tc.name, eMKEnqueued, 4, t) // robots.txt * 2, both Page1s
}
func testEnqueueNewURLOnError(t *testing.T, tc *testCase, buf bool) {
ff := newFileFetcher()
spy := newSpy(ff, buf)
spy.setExtensionMethod(eMKFilter, func(ctx *URLContext, isVisited bool) bool {
// If is visited, but has a state of "Error", allow
st, ok := ctx.State.(string)
if isVisited && ok && st == "Error" {
return true
}
// Accept only non-visited by default
return !isVisited
})
once := false
spy.setExtensionMethod(eMKError, func(err *CrawlError) {
if err.Kind == CekFetch && !once {
// On error, reenqueue once only
once = true
// copy the URL first, otherwise creates a race
u := *err.Ctx.URL()
spy.EnqueueChan <- map[*url.URL]interface{}{
&u: "Error",
}
}
})
opts := NewOptions(spy)
opts.LogFlags = LogAll
opts.CrawlDelay = DefaultTestCrawlDelay
c := NewCrawlerWithOptions(opts)
c.Run("http://hosta/page6.html") // Page6 does not exist, that's the goal, generate an error
assertCallCount(spy, tc.name, eMKFilter, 2, t) // First pass and re-enqueued from error
assertCallCount(spy, tc.name, eMKEnqueued, 3, t) // Twice and robots.txt
}
// TODO : Test to assert low CPU usage during long crawl delay waits? (issue #12)