From a0224e5842524624217f77acd75dcdbb72e03c71 Mon Sep 17 00:00:00 2001 From: huangsimin Date: Fri, 10 Apr 2020 16:50:49 +0800 Subject: [PATCH] improve: NewHunter --- hunter.go | 19 ++++++++++++++++--- pre_curl_com_test.go | 4 +--- 2 files changed, 17 insertions(+), 6 deletions(-) diff --git a/hunter.go b/hunter.go index 8b3f467..b831787 100644 --- a/hunter.go +++ b/hunter.go @@ -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{} diff --git a/pre_curl_com_test.go b/pre_curl_com_test.go index 533c7e9..44f2899 100644 --- a/pre_curl_com_test.go +++ b/pre_curl_com_test.go @@ -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("httpbin.org").MatchString(content) if !isMatchContent { t.Error(content)