Skip to content

Commit

Permalink
better error handle way and add test case for failure
Browse files Browse the repository at this point in the history
  • Loading branch information
yujiahaol68 committed Nov 30, 2017
1 parent 0f63cc2 commit 1e4a16c
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 14 deletions.
24 changes: 14 additions & 10 deletions downmark.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"net/url"
"strings"
"sync"
"sync/atomic"
"time"

"golang.org/x/net/html"
Expand Down Expand Up @@ -110,35 +109,40 @@ func (d DLink) Convert(h HandlerFunc) (Conversion, error) {
Timeout: timeOut,
}

var successCounter int64
var failCounter int64
var totalCount int
var successCounter int

for i, u := range d {
go func(id int, url string) {
resp, err := client.Get(url)

if err != nil {
fmt.Println(url + "FAIL in request")
atomic.AddInt64(&failCounter, 1)
fmt.Println("FAIL in request --> " + url)

failure := newDBody(id, nil)
tokenizerChan <- &failure

return
}

defer resp.Body.Close()

dBody := newDBody(id, html.NewTokenizer(resp.Body))

atomic.AddInt64(&successCounter, 1)

tokenizerChan <- &dBody
}(i, u)
}

for dy := range tokenizerChan {
if atomic.LoadInt64(&successCounter)+atomic.LoadInt64(&failCounter) == int64(len(d)) {
totalCount++

if totalCount == len(d) {
close(tokenizerChan)
}

go h(newConverted(dy.Index), dy.ParsedBody, convertedChan)
if dy.ParsedBody != nil {
successCounter++
go h(newConverted(dy.Index), dy.ParsedBody, convertedChan)
}
}

for successCounter > 0 {
Expand Down
30 changes: 26 additions & 4 deletions downmark_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,19 +45,41 @@ func exampleFunc(cv *Converted, tr *html.Tokenizer, ch chan *Converted) {
func Test_handler(t *testing.T) {
d := NewDLink()

d.AddLink("http://www.google.com")
d.AddLink("://www.google.com")
d.AddLink("http://www.youtube.com")
d.AddLink("https://www.washingtonpost.com/")

cv, _ := d.Convert(exampleFunc)
var testStr string

for i := 0; i < 3; i++ {
s := *(cv[string(i)].data)
testStr += s[0]
for i := 0; i < len(d); i++ {
c, contains := cv[string(i)]

if contains {
s := *c.data
testStr += s[0]
}
}

if testStr != "abc" {
t.Errorf("Expected 'abc' but got '%v' ", testStr)
}
}

func Test_failure(t *testing.T) {
d := NewDLink()

d.AddLink("://www.nkvxxnvhh.com")
d.AddLink("http://youtouneaube.com")
d.AddLink("https://www.waffawshinnjbgtonpost.com/")

cv, _ := d.Convert(exampleFunc)

for i := 0; i < len(d); i++ {
_, contains := cv[string(i)]
t.Log("Expected ALL fail when request")
if contains {
t.Errorf("It should be no any index key in it when every request fail")
}
}
}

0 comments on commit 1e4a16c

Please sign in to comment.