-
Notifications
You must be signed in to change notification settings - Fork 1
/
pool_test.go
45 lines (41 loc) · 1.09 KB
/
pool_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
package youcrawl
import (
"fmt"
"testing"
"time"
)
// only two task can be run
func TestRequestPool_Close(t *testing.T) {
e := NewEngine(&EngineOption{MaxRequest: 2})
urls := []string{"https://example.com", "https://example.com", "https://example.com"}
e.AddURLs(urls...)
e.AddHTMLParser(func(ctx *Context) error {
item := ctx.Item.(DefaultItem)
doc := ctx.Doc
title := doc.Find("title").Text()
item.SetValue("title", title)
<-time.After(3 * time.Second)
return nil
})
go func() {
<-time.After(1 * time.Second)
e.InterruptChan <- struct{}{}
}()
e.RunAndWait()
}
func TestRequestPool_GetTotal(t *testing.T) {
e := NewEngine(&EngineOption{MaxRequest: 2})
urls := []string{"http://example.com", "https://example.com", "https://example.com"}
e.AddURLs(urls...)
e.AddHTMLParser(func(ctx *Context) error {
item := ctx.Item.(DefaultItem)
doc := ctx.Doc
title := doc.Find("title").Text()
item.SetValue("title", title)
<-time.After(3 * time.Second)
return nil
})
fmt.Println(e.Pool.GetTotal())
fmt.Println(e.Pool.GetUnRequestCount())
fmt.Println(e.Pool.GetCompleteCount())
}