From 491d2c1b2278c6a3c6b4ff3220b4a5d91ceb052f Mon Sep 17 00:00:00 2001 From: vinchent Date: Wed, 18 Sep 2024 10:15:53 +0200 Subject: [PATCH] racer: add a first racer test --- racer/racer.go | 23 +++++++++++++++++++++++ racer/racer_test.go | 15 +++++++++++++++ 2 files changed, 38 insertions(+) create mode 100644 racer/racer.go create mode 100644 racer/racer_test.go diff --git a/racer/racer.go b/racer/racer.go new file mode 100644 index 0000000..8927c2e --- /dev/null +++ b/racer/racer.go @@ -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 +} diff --git a/racer/racer_test.go b/racer/racer_test.go new file mode 100644 index 0000000..691bfb0 --- /dev/null +++ b/racer/racer_test.go @@ -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) + } +}