Skip to content

Commit

Permalink
Add solution checking for limit crawler exercise
Browse files Browse the repository at this point in the history
  • Loading branch information
loong committed Apr 11, 2016
1 parent c37141d commit e0f965d
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 11 deletions.
22 changes: 21 additions & 1 deletion 0-limit-crawler/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,24 @@
Given is a crawler (modified from the Go tour), which would pull pages excessively. The task is to modify the `main.go` file, so that

- the crawler cannot crawl more than 2 pages per second
- without losing it's concurrency
- without losing it's concurrency

# Test your solution

Use `go test` to verify if your solution is correct.

Correct solution:
```
PASS
ok github.com/mindworker/go-concurrency-exercises/0-limit-crawler 13.009s
```

Incorrect solution:
```
--- FAIL: TestMain (7.80s)
main_test.go:18: There exists a two crawls who were executed less than 1 sec apart.
main_test.go:19: Solution is incorrect.
FAIL
exit status 1
FAIL github.com/mindworker/go-concurrency-exercises/0-limit-crawler 7.808s
```
18 changes: 8 additions & 10 deletions 0-limit-crawler/main_test.go → 0-limit-crawler/check_test.go
Original file line number Diff line number Diff line change
@@ -1,36 +1,34 @@
//////////////////////////////////////////////////////////////////////
//
// DO NOT EDIT THIS PART
// Your task is to edit `main.go`
//

package main

import (
"sync"
"testing"
"time"
)

func TestMain(t *testing.T) {
fetchSig := fetchSignalInstance()
done := make(chan bool)

start := time.Now()
go func() {
for {
switch {
case <-fetchSig:
// Check if signal arrived earlier than a second (with error margin)
if time.Since(start).Nanoseconds() < 990000000 {
if time.Now().Sub(start).Nanoseconds() < 990000000 {
t.Log("There exists a two crawls who were executed less than 1 sec apart.")
t.Log("Solution is incorrect.")
t.FailNow()
}
start = time.Now()
case <-done:
break
}
}
}()

var wg sync.WaitGroup
wg.Add(1)
Crawl("http://golang.org/", 4, &wg)
wg.Wait()
done <- true
main()
}

0 comments on commit e0f965d

Please sign in to comment.