racer: add timeout test
This commit is contained in:
		@ -1,29 +1,23 @@
 | 
			
		||||
package racer
 | 
			
		||||
 | 
			
		||||
import (
 | 
			
		||||
	"fmt"
 | 
			
		||||
	"errors"
 | 
			
		||||
	"net/http"
 | 
			
		||||
	"time"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
func Racer(a, b string) string {
 | 
			
		||||
func Racer(a, b string) (string, error) {
 | 
			
		||||
	select {
 | 
			
		||||
	// wait until channel closed
 | 
			
		||||
	case <-ping(a):
 | 
			
		||||
		return a
 | 
			
		||||
		return a, nil
 | 
			
		||||
	case <-ping(b):
 | 
			
		||||
		return b
 | 
			
		||||
		return b, nil
 | 
			
		||||
	case <-time.After(10 * time.Second):
 | 
			
		||||
		return "", errors.New("timeout")
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func measureResponseTime(url string) time.Duration {
 | 
			
		||||
	start := time.Now()
 | 
			
		||||
	http.Get(url)
 | 
			
		||||
	duration := time.Since(start)
 | 
			
		||||
	fmt.Println(duration)
 | 
			
		||||
	return duration
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func ping(url string) chan struct{} {
 | 
			
		||||
	ch := make(chan struct{})
 | 
			
		||||
	go func() {
 | 
			
		||||
@ -33,3 +27,11 @@ func ping(url string) chan struct{} {
 | 
			
		||||
	}()
 | 
			
		||||
	return ch
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// func measureResponseTime(url string) time.Duration {
 | 
			
		||||
// 	start := time.Now()
 | 
			
		||||
// 	http.Get(url)
 | 
			
		||||
// 	duration := time.Since(start)
 | 
			
		||||
// 	fmt.Println(duration)
 | 
			
		||||
// 	return duration
 | 
			
		||||
// }
 | 
			
		||||
 | 
			
		||||
@ -8,6 +8,7 @@ import (
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
func TestRacer(t *testing.T) {
 | 
			
		||||
	t.Run("compares speeds of servers, returning the url of the fasted one", func(t *testing.T) {
 | 
			
		||||
		slowServer := makeDelayedServer(20 * time.Millisecond)
 | 
			
		||||
		defer slowServer.Close()
 | 
			
		||||
 | 
			
		||||
@ -18,11 +19,25 @@ func TestRacer(t *testing.T) {
 | 
			
		||||
		fastURL := fastServer.URL
 | 
			
		||||
 | 
			
		||||
		want := fastURL
 | 
			
		||||
	got := Racer(slowURL, 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) {
 | 
			
		||||
		serverA := makeDelayedServer(11 * time.Second)
 | 
			
		||||
		defer serverA.Close()
 | 
			
		||||
 | 
			
		||||
		serverB := makeDelayedServer(11 * time.Second)
 | 
			
		||||
		defer serverB.Close()
 | 
			
		||||
 | 
			
		||||
		_, err := Racer(serverA.URL, serverB.URL)
 | 
			
		||||
		if err == nil {
 | 
			
		||||
			t.Error("expected an error but didn't got one")
 | 
			
		||||
		}
 | 
			
		||||
	})
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func makeDelayedServer(delay time.Duration) *httptest.Server {
 | 
			
		||||
 | 
			
		||||
		Reference in New Issue
	
	Block a user