-
Notifications
You must be signed in to change notification settings - Fork 1
/
engine_test.go
365 lines (308 loc) · 10.1 KB
/
engine_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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
package tegenaria
import (
"context"
"errors"
"fmt"
"os"
"strings"
"testing"
"time"
"github.com/wxnacy/wgo/arrays"
)
func newTestEngine(spiderName string) *SpiderEngine {
engine := NewSpiderEngine()
server := newTestServer()
// register test spider
engine.RegisterSpider(&TestSpider{NewBaseSpider(spiderName, []string{server.URL + "/testGET"})})
// register test pipelines
engine.RegisterPipelines(&TestItemPipeline{0})
engine.RegisterPipelines(&TestItemPipeline3{2})
engine.RegisterPipelines(&TestItemPipeline2{1})
// register download middlerware
engine.RegisterDownloadMiddlewares(TestDownloadMiddler{0, "1"})
engine.RegisterDownloadMiddlewares(TestDownloadMiddler{2, "3"})
engine.RegisterDownloadMiddlewares(TestDownloadMiddler{1, "2"})
return engine
}
func newTestRequest() *Context {
server := newTestServer()
request := NewRequest(server.URL+"/testGET", GET, testParser)
var MainCtx context.Context = context.Background()
ctx := NewContext(request, WithContext(MainCtx))
return ctx
}
func TestEngineRegister(t *testing.T) {
engine := newTestEngine("testSpider1")
for index, pipeline := range engine.pipelines {
if pipeline.GetPriority() != index {
t.Errorf("pipeline priority %d error", pipeline.GetPriority())
}
}
_, ok := engine.spiders.SpidersModules["testSpider1"]
if !ok {
t.Errorf("spider register error not found spider %s", "testSpider1")
}
}
func TestEngineOptions(t *testing.T) {
engine := NewSpiderEngine(
EngineWithContext(context.Background()),
EngineWithTimeout(1*time.Second),
EngineWithAllowStatusCode([]uint64{404}),
EngineWithUniqueReq(false),
EngineWithSchedulerNum(2),
EngineWithRequestNum(32),
EngineWithReadCacheNum(4),
EngineWithDownloader(NewDownloader()),
)
if engine.DownloadTimeout != 1*time.Second {
t.Errorf("spider engine set download timeout value error")
}
if arrays.ContainsUint(engine.allowStatusCode, uint64(404)) != 0 {
t.Errorf("spider engine set allow status code arrays value error\n")
}
if engine.filterDuplicateReq != false {
t.Errorf("spider engine set disable request unique value error\n")
}
if engine.schedulerNum != 2 {
t.Errorf("spider engine set schedulers number value error\n")
}
if cap(engine.cacheChan) != 32 {
t.Errorf("spider engine set request number value error except %d, get %d\n", 32, cap(engine.cacheChan))
}
if engine.cacheReadNum != 4 {
t.Errorf("spider engine set cache reader number value error\n")
}
}
func TestCache(t *testing.T) {
server := newTestServer()
engine := NewSpiderEngine()
engine.waitGroup.Add(1)
// write new request
ctx := newTestRequest()
go engine.writeCache(ctx)
engine.waitGroup.Wait()
// read a request from cache
engine.mainWaitGroup.Add(1)
go engine.readCache()
engine.isDone = true
engine.mainWaitGroup.Wait()
if len(engine.cacheChan) == 0 {
t.Errorf("Read request from cache error get empty cache channel buffer")
}
new_req := <-engine.cacheChan
if new_req != nil && new_req.Request.Url != server.URL+"/testGET" {
t.Errorf("Read request from cache error except url %s, but get url %s\n", server.URL+"/testGET", new_req.Request.Url)
}
}
func TestDoFilter(t *testing.T) {
engine := NewSpiderEngine(EngineWithUniqueReq(true))
ctx := newTestRequest()
for i := 0; i < 3; i++ {
engine.waitGroup.Add(1)
go engine.writeCache(ctx)
}
engine.waitGroup.Wait()
if engine.cache.getSize() != 1 {
t.Errorf("Cache filter duplicate request fail except %d requests, but get %d requests\n", 1, engine.cache.getSize())
}
}
func TestDoDownload(t *testing.T) {
engine := newTestEngine("testSpider2")
server := newTestServer()
ctx := newTestRequest()
engine.waitGroup.Add(1)
go engine.doDownload(ctx)
engine.waitGroup.Wait()
value, ok := ctx.Request.Header["priority-0"]
if !ok {
t.Errorf("Download middlerwares not work except priority-0 header, but get nil\n")
}
if value != "0" {
t.Errorf("Download middlerwares not work except priority-0 header value 0, but get %s\n", value)
}
result := <-engine.requestResultChan
if result.DownloadResult.Error != nil {
t.Errorf("Download result error get %s\n", result.DownloadResult.Error)
}
// test parser
spider := &TestSpider{NewBaseSpider("testSpider2", []string{server.URL + "/testGET"})}
engine.waitGroup.Add(1)
go engine.doParse(spider, result)
engine.waitGroup.Wait()
itemCtx := <-engine.itemsChan
i := itemCtx.Item.(*testItem)
if i.test != "test" {
t.Errorf("Download result parser fail except %s get %s\n", "test", i.test)
}
// test pipelines
engine.waitGroup.Add(1)
go engine.doPipelinesHandlers(spider, itemCtx)
engine.waitGroup.Wait()
}
func TestDoParse(t *testing.T) {
}
func TestDoRequestResult(t *testing.T) {
engine := newTestEngine("testSpider4")
ctx := newTestRequest()
ctx.Request.Url = newTestServer().URL + "/testTimeout"
engine.SetDownloadTimeout(1 * time.Second)
// test timeout
engine.waitGroup.Add(1)
go engine.doDownload(ctx)
engine.waitGroup.Wait()
result := <-engine.requestResultChan
engine.waitGroup.Add(1)
go engine.doRequestResult(result)
engine.waitGroup.Wait()
err := <-engine.errorChan
if err == nil {
t.Errorf("Download should be timeout but not \n")
}
}
func TestAllowedStatusCode(t *testing.T) {
engine := newTestEngine("testSpider5")
ctx := newTestRequest()
ctx.Request.Url = newTestServer().URL + "/test404"
engine.SetAllowedStatus([]uint64{404, 403})
// test 404 status codes
engine.waitGroup.Add(1)
go engine.doDownload(ctx)
engine.waitGroup.Wait()
result := <-engine.requestResultChan
engine.waitGroup.Add(1)
go engine.doRequestResult(result)
engine.waitGroup.Wait()
resp := <-engine.respChan
if resp.DownloadResult.Response == nil {
t.Errorf("Download response not should be empty \n")
}
if resp.DownloadResult.Response.Status != 404 {
t.Errorf("Download response status should be 404 but get %d \n", resp.DownloadResult.Response.Status)
}
}
func TestNotAllowedStatus(t *testing.T) {
engine := newTestEngine("testSpider11")
ctx := newTestRequest()
ctx.Request.Url = newTestServer().URL + "/test403"
engine.SetAllowedStatus([]uint64{404})
// test 403 status codes
engine.waitGroup.Add(1)
go engine.doDownload(ctx)
engine.waitGroup.Wait()
result := <-engine.requestResultChan
engine.waitGroup.Add(1)
go engine.doRequestResult(result)
engine.waitGroup.Wait()
err := <-engine.errorChan
if err.Error() != fmt.Sprintf("%s %d with context id %s", ErrNotAllowStatusCode.Error(), 403, err.CtxId) {
t.Errorf("Download response error get %s\n", err.Error())
}
}
func TestSpiderNotFound(t *testing.T) {
defer func() {
if p := recover(); p != nil {
t.Errorf("Spider spider not should be found \n")
}
}()
engine := newTestEngine("testSpider6")
engine.Start("spider")
}
func TestSpiderDuplicate(t *testing.T) {
defer func() {
if p := recover(); p == nil {
t.Errorf("Spider register should be get duplicate error \n")
}
}()
engine := newTestEngine("testSpider7")
engine.RegisterSpider(&TestSpider{NewBaseSpider("testSpider7", []string{})})
}
func TestSystemSignalHandle(t *testing.T) {
engine := newTestEngine("testSpider8")
engine.quitSignal <- os.Interrupt
engine.Start("testSpider8")
}
func TestEngineStart(t *testing.T) {
engine := newTestEngine("testSpider9")
engine.Start("testSpider9")
if engine.Stats.ErrorCount != 0 {
t.Errorf("Spider crawl get %d errors \n", engine.Stats.ErrorCount)
}
if engine.Stats.RequestDownloaded != 1 {
t.Errorf("Spider crawl except 1 download, but get %d \n", engine.Stats.RequestDownloaded)
}
if engine.Stats.ItemScraped != 1 {
t.Errorf("Spider crawl except 1 item, but get %d \n", engine.Stats.ItemScraped)
}
}
func TestEngineErrorHandler(t *testing.T) {
engine := newTestEngine("testSpider10")
engine.spiders.SpidersModules["testSpider10"].(*TestSpider).FeedUrls = []string{"http://127.0.0.1:12345"}
engine.Start("testSpider10")
if engine.Stats.ErrorCount != 1 {
t.Errorf("Spider crawl except 1 error,but get %d \n", engine.Stats.ErrorCount)
}
}
func TestProcessRequestError(t *testing.T) {
engine := newTestEngine("testSpider19")
m := TestDownloadMiddler2{9, "test"}
engine.RegisterDownloadMiddlewares(m)
ctx := newTestRequest()
engine.waitGroup.Add(1)
go engine.doDownload(ctx)
engine.waitGroup.Wait()
err := <-engine.errorChan
if !strings.Contains(err.Error(), "process request fail") {
t.Errorf("request should have error process request fail, but get %s\n", err.Error())
}
}
func TestProcessResponseError(t *testing.T) {
engine := newTestEngine("testSpider79")
m := TestDownloadMiddler2{9, "test"}
engine.RegisterDownloadMiddlewares(m)
ctx := newTestRequest()
engine.processResponse(ctx)
err := <-engine.errorChan
if !strings.Contains(err.Error(), "process response fail") {
t.Errorf("request should have error process response fail, but get %s\n", err.Error())
}
}
func TestProcessItemError(t *testing.T) {
engine := newTestEngine("testSpider99")
engine.RegisterPipelines(&TestItemPipeline4{4})
engine.waitGroup.Add(1)
m := make(map[string]string)
m["a"] = "b"
ctx := newTestRequest()
item := NewItem(ctx, &testItem{"TEST", make([]int, 0)})
go engine.doPipelinesHandlers(&TestSpider{NewBaseSpider("testSpider7", []string{})}, item)
engine.waitGroup.Wait()
err := <-engine.errorChan
if !strings.Contains(err.Error(), "process item fail") {
t.Errorf("request should have error process item fail, but get %s\n", err.Error())
}
}
func TestParseError(t *testing.T) {
engine := newTestEngine("testSpiderParseError")
server := newTestServer()
request := NewRequest(server.URL+"/testGET", GET, func(resp *Context, item chan<- *ItemMeta, req chan<- *Context) error {
return errors.New("parse response error")
})
var MainCtx context.Context = context.Background()
ctx := NewContext(request, WithContext(MainCtx))
// test 403 status codes
engine.waitGroup.Add(1)
go engine.doDownload(ctx)
engine.waitGroup.Wait()
result := <-engine.requestResultChan
engine.waitGroup.Add(1)
spider, err := engine.spiders.GetSpider("testSpiderParseError")
if err != nil {
t.Errorf("Get spider testSpiderParseError")
}
go engine.doParse(spider, result)
engine.waitGroup.Wait()
err = <-engine.errorChan
if !strings.Contains(err.Error(), "parse response error") {
t.Errorf("Except parse response error but get %s\n", err.Error())
}
}