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