racer: add a first racer test
This commit is contained in:
parent
d83f1c6f4c
commit
491d2c1b22
23
racer/racer.go
Normal file
23
racer/racer.go
Normal file
@ -0,0 +1,23 @@
|
|||||||
|
package racer
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"net/http"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
func Racer(a, b string) string {
|
||||||
|
startA := time.Now()
|
||||||
|
http.Get(a)
|
||||||
|
aDuration := time.Since(startA)
|
||||||
|
fmt.Println(aDuration)
|
||||||
|
|
||||||
|
startB := time.Now()
|
||||||
|
http.Get(b)
|
||||||
|
bDuration := time.Since(startB)
|
||||||
|
fmt.Println(bDuration)
|
||||||
|
if aDuration < bDuration {
|
||||||
|
return a
|
||||||
|
}
|
||||||
|
return b
|
||||||
|
}
|
15
racer/racer_test.go
Normal file
15
racer/racer_test.go
Normal file
@ -0,0 +1,15 @@
|
|||||||
|
package racer
|
||||||
|
|
||||||
|
import "testing"
|
||||||
|
|
||||||
|
func TestRacer(t *testing.T) {
|
||||||
|
slowURL := "http://www.facebook.com"
|
||||||
|
fastURL := "http://www.quii.dev"
|
||||||
|
|
||||||
|
want := fastURL
|
||||||
|
got := Racer(slowURL, fastURL)
|
||||||
|
|
||||||
|
if got != want {
|
||||||
|
t.Errorf("got %q, want %q", got, want)
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user