racer
							parent
							
								
									f308273124
								
							
						
					
					
						commit
						2150188501
					
				@ -0,0 +1,3 @@
 | 
				
			|||||||
 | 
					module my_select
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					go 1.22.5
 | 
				
			||||||
@ -0,0 +1,23 @@
 | 
				
			|||||||
 | 
					package my_select
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import (
 | 
				
			||||||
 | 
						"net/http"
 | 
				
			||||||
 | 
						"time"
 | 
				
			||||||
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func Racer(a string, b string) (winner string) {
 | 
				
			||||||
 | 
						aDuration := measureResponseTime(a)
 | 
				
			||||||
 | 
						bDuration := measureResponseTime(b)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						if aDuration < bDuration {
 | 
				
			||||||
 | 
							return a
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						return b
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func measureResponseTime(url string) time.Duration {
 | 
				
			||||||
 | 
						start := time.Now()
 | 
				
			||||||
 | 
						http.Get(url)
 | 
				
			||||||
 | 
						return time.Since(start)
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
@ -0,0 +1,34 @@
 | 
				
			|||||||
 | 
					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)
 | 
				
			||||||
 | 
						}))
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
					Loading…
					
					
				
		Reference in New Issue