Решение на Concurrent Crawling от Йончо Йончев

Обратно към всички решения

Към профила на Йончо Йончев

Резултати

  • 0 точки от тестове
  • 0 бонус точки
  • 0 точки общо
  • 0 успешни тест(а)
  • 11 неуспешни тест(а)

Код

package main
import (
"io/ioutil"
"log"
"net/http"
)
func Worker(callback func(string) bool, str string) (string, error) {
//var wg sync.WaitGroup
resp, err := http.Get(str)
if err != nil {
log.Fatal(err)
return "", err
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
log.Fatal(err)
return "", err
}
if res := callback(string(body)); res {
return str, nil
}
return "", nil
}
func SeekAndDestroy(callback func(string) bool, chunkedUrlsToCheck <-chan []string, workersCount int) (string, error) {
var result_ = ""
for i := 0; i < 2*workersCount; i += 1 {
var y = ""
go func(value string) {
var urls, _ = <-chunkedUrlsToCheck
for _, value := range urls {
if outcome, _ := Worker(callback, value); outcome == value {
result_ = string(outcome)
}
}
}(y)
}
return result_, nil
}

Лог от изпълнението

[/tmp/go-build101840554/_/tmp/d20150111-16649-1rgtz1l/_test/d20150111-16649-1rgtz1l.test -test.run=TestWithNegativeWorkersCount -test.timeout=120s]
--- FAIL: TestWithNegativeWorkersCount-2 (0.00 seconds)
	solution_test.go:148: Function should return error when workersCount is negative
FAIL
exit status 1
FAIL	_/tmp/d20150111-16649-1rgtz1l	0.006s
[/tmp/go-build540041390/_/tmp/d20150111-16649-1rgtz1l/_test/d20150111-16649-1rgtz1l.test -test.run=TestWithZeroWorkersCount -test.timeout=120s]
--- FAIL: TestWithZeroWorkersCount-2 (0.00 seconds)
	solution_test.go:148: Function should return error when workersCount is zero
FAIL
exit status 1
FAIL	_/tmp/d20150111-16649-1rgtz1l	0.005s
[/tmp/go-build118639765/_/tmp/d20150111-16649-1rgtz1l/_test/d20150111-16649-1rgtz1l.test -test.run=TestWithInvalidCallback -test.timeout=120s]
--- FAIL: TestWithInvalidCallback-2 (0.00 seconds)
	solution_test.go:148: Function should return error when callback is nil
FAIL
exit status 1
FAIL	_/tmp/d20150111-16649-1rgtz1l	0.006s
[/tmp/go-build864199687/_/tmp/d20150111-16649-1rgtz1l/_test/d20150111-16649-1rgtz1l.test -test.run=TestWithNilChannel -test.timeout=120s]
--- FAIL: TestWithNilChannel-2 (0.00 seconds)
	solution_test.go:148: Function should return error when channel is uninitialized
FAIL
exit status 1
FAIL	_/tmp/d20150111-16649-1rgtz1l	0.006s
[/tmp/go-build284590730/_/tmp/d20150111-16649-1rgtz1l/_test/d20150111-16649-1rgtz1l.test -test.run=TestWithClosedChannelWhenStarting -test.timeout=120s]
--- FAIL: TestWithClosedChannelWhenStarting-2 (0.00 seconds)
	solution_test.go:148: Function should return error when the urls channel was closed
FAIL
exit status 1
FAIL	_/tmp/d20150111-16649-1rgtz1l	0.006s
[/tmp/go-build521963873/_/tmp/d20150111-16649-1rgtz1l/_test/d20150111-16649-1rgtz1l.test -test.run=TestWithClosedChannelMidway -test.timeout=120s]
--- FAIL: TestWithClosedChannelMidway-2 (0.00 seconds)
	solution_test.go:194: Function should have returned an error when urls channel was closed
FAIL
exit status 1
FAIL	_/tmp/d20150111-16649-1rgtz1l	0.007s
[/tmp/go-build898344635/_/tmp/d20150111-16649-1rgtz1l/_test/d20150111-16649-1rgtz1l.test -test.run=TestWhetherGlobalTimeoutIsHandled -test.timeout=120s]
--- FAIL: TestWhetherGlobalTimeoutIsHandled-2 (0.00 seconds)
	solution_test.go:207: Function should have returned an error when the global timeout was reached
FAIL
exit status 1
FAIL	_/tmp/d20150111-16649-1rgtz1l	0.006s
[/tmp/go-build010355682/_/tmp/d20150111-16649-1rgtz1l/_test/d20150111-16649-1rgtz1l.test -test.run=TestWithLoremIpsum -test.timeout=120s]
--- FAIL: TestWithLoremIpsum-2 (0.00 seconds)
	solution_test.go:237: Function returned '' when it should have returned 'http://127.0.0.2:43262/lorem_ipsum'
FAIL
exit status 1
FAIL	_/tmp/d20150111-16649-1rgtz1l	0.006s
[/tmp/go-build709340275/_/tmp/d20150111-16649-1rgtz1l/_test/d20150111-16649-1rgtz1l.test -test.run=TestIfTimeoutAndErrorCodesAreHonoured -test.timeout=120s]
--- FAIL: TestIfTimeoutAndErrorCodesAreHonoured-2 (0.00 seconds)
	solution_test.go:267: Function returned '' when it should have returned 'http://127.0.0.2:52307/correct_page'
FAIL
exit status 1
FAIL	_/tmp/d20150111-16649-1rgtz1l	0.005s
[/tmp/go-build729096440/_/tmp/d20150111-16649-1rgtz1l/_test/d20150111-16649-1rgtz1l.test -test.run=TestRaceCondition -test.timeout=120s]
--- FAIL: TestRaceCondition-2 (0.00 seconds)
	solution_test.go:293: Function returned '' when it should have returned 'http://127.0.0.2:47821/fast_success'
FAIL
exit status 1
FAIL	_/tmp/d20150111-16649-1rgtz1l	0.010s
[/tmp/go-build951593471/_/tmp/d20150111-16649-1rgtz1l/_test/d20150111-16649-1rgtz1l.test -test.run=TestCloseChannelBeforeFinish -test.timeout=120s]
--- FAIL: TestCloseChannelBeforeFinish-2 (0.00 seconds)
	solution_test.go:316: Function should have returned an error, channel was closed before it could finish
FAIL
exit status 1
FAIL	_/tmp/d20150111-16649-1rgtz1l	0.012s

История (1 версия и 0 коментара)

Йончо обнови решението на 11.12.2014 16:31 (преди над 3 години)

+package main
+
+import (
+ "io/ioutil"
+ "log"
+ "net/http"
+)
+
+func Worker(callback func(string) bool, str string) (string, error) {
+ //var wg sync.WaitGroup
+ resp, err := http.Get(str)
+ if err != nil {
+ log.Fatal(err)
+ return "", err
+ }
+ defer resp.Body.Close()
+ body, err := ioutil.ReadAll(resp.Body)
+ if err != nil {
+ log.Fatal(err)
+ return "", err
+ }
+
+ if res := callback(string(body)); res {
+ return str, nil
+ }
+ return "", nil
+}
+
+func SeekAndDestroy(callback func(string) bool, chunkedUrlsToCheck <-chan []string, workersCount int) (string, error) {
+ var result_ = ""
+ for i := 0; i < 2*workersCount; i += 1 {
+ var y = ""
+
+ go func(value string) {
+ var urls, _ = <-chunkedUrlsToCheck
+ for _, value := range urls {
+ if outcome, _ := Worker(callback, value); outcome == value {
+ result_ = string(outcome)
+ }
+ }
+ }(y)
+ }
+ return result_, nil
+}