master
 1package main
 2
 3import (
 4	"net/http"
 5	"net/http/httptest"
 6	"testing"
 7	"time"
 8)
 9
10func TestRacer(t *testing.T) {
11	slowServer := makeDelayedServer(20 * time.Millisecond)
12	fastServer := makeDelayedServer(0 * time.Millisecond)
13
14	defer slowServer.Close()
15	defer fastServer.Close()
16
17	slowUrl := slowServer.URL
18	fastUrl := fastServer.URL
19
20	want := fastUrl
21	got := Racer(slowUrl, fastUrl)
22
23	if got != want {
24		t.Errorf("got %q, want %q", got, want)
25	}
26}
27
28func makeDelayedServer(delay time.Duration) *httptest.Server {
29	return httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
30		time.Sleep(delay)
31		w.WriteHeader(http.StatusOK)
32	}))
33}