racer: add a first racer test
This commit is contained in:
		
							
								
								
									
										23
									
								
								racer/racer.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										23
									
								
								racer/racer.go
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,23 @@
 | 
				
			|||||||
 | 
					package racer
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import (
 | 
				
			||||||
 | 
						"fmt"
 | 
				
			||||||
 | 
						"net/http"
 | 
				
			||||||
 | 
						"time"
 | 
				
			||||||
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func Racer(a, b string) string {
 | 
				
			||||||
 | 
						startA := time.Now()
 | 
				
			||||||
 | 
						http.Get(a)
 | 
				
			||||||
 | 
						aDuration := time.Since(startA)
 | 
				
			||||||
 | 
						fmt.Println(aDuration)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						startB := time.Now()
 | 
				
			||||||
 | 
						http.Get(b)
 | 
				
			||||||
 | 
						bDuration := time.Since(startB)
 | 
				
			||||||
 | 
						fmt.Println(bDuration)
 | 
				
			||||||
 | 
						if aDuration < bDuration {
 | 
				
			||||||
 | 
							return a
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						return b
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
							
								
								
									
										15
									
								
								racer/racer_test.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										15
									
								
								racer/racer_test.go
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,15 @@
 | 
				
			|||||||
 | 
					package racer
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import "testing"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func TestRacer(t *testing.T) {
 | 
				
			||||||
 | 
						slowURL := "http://www.facebook.com"
 | 
				
			||||||
 | 
						fastURL := "http://www.quii.dev"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						want := fastURL
 | 
				
			||||||
 | 
						got := Racer(slowURL, fastURL)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						if got != want {
 | 
				
			||||||
 | 
							t.Errorf("got %q, want %q", got, want)
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
		Reference in New Issue
	
	Block a user