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.
35 lines
662 B
Go
35 lines
662 B
Go
package my_select
|
|
|
|
import (
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
func TestRacer(t *testing.T) {
|
|
|
|
slowServer := makeDelayedServer(20 * time.Microsecond)
|
|
defer slowServer.Close()
|
|
|
|
fastServer := makeDelayedServer(0 * time.Microsecond)
|
|
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)
|
|
}
|
|
}
|
|
|
|
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)
|
|
}))
|
|
}
|