You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
49 lines
910 B
Go
49 lines
910 B
Go
package concurrency
|
|
|
|
import (
|
|
"reflect"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
func mockWebsiteChecker(url string) bool {
|
|
return url != "waat://furhurterwe.geds"
|
|
}
|
|
|
|
func slowStubWebsiteChecker(_ string) bool {
|
|
time.Sleep(20 * time.Millisecond)
|
|
return true
|
|
}
|
|
|
|
// What does testing.B do?
|
|
func BenchmarkCheckWebsites(b *testing.B) {
|
|
urls := make([]string, 100)
|
|
for i := 0; i < len(urls); i++ {
|
|
urls[i] = "a url"
|
|
}
|
|
b.ResetTimer()
|
|
for i := 0; i < b.N; i++ {
|
|
CheckWebsites(slowStubWebsiteChecker, urls)
|
|
}
|
|
}
|
|
|
|
func TestWebsites(t *testing.T) {
|
|
websites := []string{
|
|
"http://google.com",
|
|
"http://hackaday.com",
|
|
"waat://furhurterwe.geds",
|
|
}
|
|
|
|
want := map[string]bool{
|
|
"http://google.com": true,
|
|
"http://hackaday.com": true,
|
|
"waat://furhurterwe.geds": false,
|
|
}
|
|
|
|
got := CheckWebsites(mockWebsiteChecker, websites)
|
|
|
|
if !reflect.DeepEqual(want, got) {
|
|
t.Fatalf("Wanted %v, got %v", want, got)
|
|
}
|
|
}
|