48 lines
1.0 KiB
Go
48 lines
1.0 KiB
Go
package main
|
|
|
|
import (
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
func TestRacer(t *testing.T) {
|
|
t.Run("Test that fast server returns", func(t *testing.T) {
|
|
slowServer := makeDelayedServer(time.Millisecond * 20)
|
|
fastServer := makeDelayedServer(time.Millisecond * 0)
|
|
|
|
defer slowServer.Close()
|
|
defer fastServer.Close()
|
|
|
|
slowURL := slowServer.URL
|
|
fastURL := fastServer.URL
|
|
|
|
want := fastURL
|
|
got, _ := Racer(slowURL, fastURL)
|
|
|
|
if got != want {
|
|
t.Errorf("got %q, want %q", got, want)
|
|
}
|
|
})
|
|
|
|
t.Run("returns an error if a server doesn't respond within 10s", func(t *testing.T) {
|
|
server := makeDelayedServer(25 * time.Millisecond)
|
|
|
|
defer server.Close()
|
|
|
|
_, err := ConfigurableRacer(server.URL, server.URL, 20*time.Millisecond)
|
|
|
|
if err == nil {
|
|
t.Error("expected an error, but didn't get one")
|
|
}
|
|
})
|
|
}
|
|
|
|
func makeDelayedServer(delay time.Duration) *httptest.Server {
|
|
return httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
time.Sleep(delay)
|
|
w.WriteHeader(http.StatusOK)
|
|
}))
|
|
}
|