improve: NewHunter

This commit is contained in:
huangsimin 2020-04-10 16:50:49 +08:00
parent ea6cfc1efa
commit a0224e5842
2 changed files with 17 additions and 6 deletions

View File

@ -27,11 +27,24 @@ type Hunter struct {
createQueue func() *pqueue.PriorityQueue
}
// NewHunter 默认最大优先
func NewHunter() *Hunter {
return NewPriorityMaxHunter()
// NewHunter 默认最大优先, tasks为预先需要addtask
func NewHunter(tasks ...ITask) *Hunter {
hunter := NewPriorityMaxHunter()
for _, task := range tasks {
hunter.AddTask(task)
}
return hunter
}
// NewHunterFromTasks 默认最大优先
// func NewHunterFromTasks(tasks ...ITask) *Hunter {
// hunter := NewPriorityMaxHunter()
// for _, task := range tasks {
// hunter.AddTask(task)
// }
// return hunter
// }
// NewPriorityHunter 自定义优先处理队列
func NewPriorityHunter(queueCreator func() *pqueue.PriorityQueue) *Hunter {
hunter := &Hunter{}

View File

@ -19,12 +19,10 @@ func (web *WebGurl) Execute(cxt *TaskContext) {
func TestCurlCom(t *testing.T) {
curlBash := "curl 'http://httpbin.org/' -H 'Connection: keep-alive' -H 'Cache-Control: max-age=0' -H 'Upgrade-Insecure-Requests: 1' -H 'User-Agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.130 Safari/537.36' -H 'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9' -H 'Accept-Encoding: gzip, deflate' -H 'Accept-Language: zh-CN,zh;q=0.9' --compressed --insecure"
hunter := NewHunter()
hunter.AddTask(&WebGurl{PreCurlUrl: PreCurlUrl(curlBash)})
hunter := NewHunter(&WebGurl{PreCurlUrl(curlBash)}) // first params PreCurlUrl
hunter.Execute()
content := hunter.GetShare("test").(string)
isMatchContent := regexp.MustCompile("<title>httpbin.org</title>").MatchString(content)
if !isMatchContent {
t.Error(content)