-
Notifications
You must be signed in to change notification settings - Fork 66
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #69 from larien/traducao/select
Traducao/select
- Loading branch information
Showing
14 changed files
with
389 additions
and
394 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package corredor | ||
|
||
import ( | ||
"net/http" | ||
"time" | ||
) | ||
|
||
// Corredor compara os tempos de resposta de a e b, retornando o mais rápido | ||
func Corredor(a, b string) (vencedor string) { | ||
duracaoA := medirTempoDeResposta(a) | ||
duracaoB := medirTempoDeResposta(b) | ||
|
||
if duracaoA < duracaoB { | ||
return a | ||
} | ||
|
||
return b | ||
} | ||
|
||
func medirTempoDeResposta(URL string) time.Duration { | ||
inicio := time.Now() | ||
http.Get(URL) | ||
return time.Since(inicio) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package corredor | ||
|
||
import ( | ||
"net/http" | ||
"net/http/httptest" | ||
"testing" | ||
"time" | ||
) | ||
|
||
func TestCorredor(t *testing.T) { | ||
|
||
servidorLento := criarServidorComAtraso(20 * time.Millisecond) | ||
servidorRapido := criarServidorComAtraso(0 * time.Millisecond) | ||
|
||
defer servidorLento.Close() | ||
defer servidorRapido.Close() | ||
|
||
URLLenta := servidorLento.URL | ||
URLRapida := servidorRapido.URL | ||
|
||
esperado := URLRapida | ||
resultado := Corredor(URLLenta, URLRapida) | ||
|
||
if resultado != esperado { | ||
t.Errorf("resultado '%s', esperado '%s'", resultado, esperado) | ||
} | ||
} | ||
|
||
func criarServidorComAtraso(atraso time.Duration) *httptest.Server { | ||
return httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
time.Sleep(atraso) | ||
w.WriteHeader(http.StatusOK) | ||
})) | ||
} |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package corredor | ||
|
||
import ( | ||
"net/http" | ||
) | ||
|
||
// Corredor compara os tempos de resposta de a e b, retornando o mais rápido | ||
func Corredor(a, b string) (vencedor string) { | ||
select { | ||
case <-ping(a): | ||
return a | ||
case <-ping(b): | ||
return b | ||
} | ||
} | ||
|
||
func ping(URL string) chan bool { | ||
ch := make(chan bool) | ||
go func() { | ||
http.Get(URL) | ||
close(ch) | ||
}() | ||
return ch | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package corredor | ||
|
||
import ( | ||
"net/http" | ||
"net/http/httptest" | ||
"testing" | ||
"time" | ||
) | ||
|
||
func TestCorredor(t *testing.T) { | ||
|
||
servidorLento := criarServidorComAtraso(20 * time.Millisecond) | ||
servidorRapido := criarServidorComAtraso(0 * time.Millisecond) | ||
|
||
defer servidorLento.Close() | ||
defer servidorRapido.Close() | ||
|
||
URLLenta := servidorLento.URL | ||
URLRapida := servidorRapido.URL | ||
|
||
esperado := URLRapida | ||
resultado := Corredor(URLLenta, URLRapida) | ||
|
||
if resultado != esperado { | ||
t.Errorf("resultado '%s', esperado '%s'", resultado, esperado) | ||
} | ||
} | ||
|
||
func criarServidorComAtraso(atraso time.Duration) *httptest.Server { | ||
return httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
time.Sleep(atraso) | ||
w.WriteHeader(http.StatusOK) | ||
})) | ||
} |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package corredor | ||
|
||
import ( | ||
"fmt" | ||
"net/http" | ||
"time" | ||
) | ||
|
||
var limiteDeDezSegundos = 10 * time.Second | ||
|
||
// Corredor compara os tempos de resposta de a e b, retornando o mais rápido com tempo limite de 10s | ||
func Corredor(a, b string) (vencedor string, error error) { | ||
return Configuravel(a, b, limiteDeDezSegundos) | ||
} | ||
|
||
// Configuravel compara os tempos de resposta de a e b, retornando o mais rápido | ||
func Configuravel(a, b string, tempoLimite time.Duration) (vencedor string, erro error) { | ||
select { | ||
case <-ping(a): | ||
return a, nil | ||
case <-ping(b): | ||
return b, nil | ||
case <-time.After(tempoLimite): | ||
return "", fmt.Errorf("tempo limite de espera excedido para %s e %s", a, b) | ||
} | ||
} | ||
|
||
func ping(URL string) chan bool { | ||
ch := make(chan bool) | ||
go func() { | ||
http.Get(URL) | ||
close(ch) | ||
}() | ||
return ch | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package corredor | ||
|
||
import ( | ||
"net/http" | ||
"net/http/httptest" | ||
"testing" | ||
"time" | ||
) | ||
|
||
func TestCorredor(t *testing.T) { | ||
t.Run("compara a velocidade de servidores, retornando o endereço do mais rápido", func(t *testing.T) { | ||
servidorLento := criarServidorComAtraso(20 * time.Millisecond) | ||
servidorRapido := criarServidorComAtraso(0 * time.Millisecond) | ||
|
||
defer servidorLento.Close() | ||
defer servidorRapido.Close() | ||
|
||
URLLenta := servidorLento.URL | ||
URLRapida := servidorRapido.URL | ||
|
||
esperado := URLRapida | ||
resultado, err := Corredor(URLLenta, URLRapida) | ||
|
||
if err != nil { | ||
t.Fatalf("não esperava um erro, mas obteve um %v", err) | ||
} | ||
|
||
if resultado != esperado { | ||
t.Errorf("resultado '%s', esperado '%s'", resultado, esperado) | ||
} | ||
}) | ||
|
||
t.Run("retorna um erro se o servidor não responder dentro de 10s", func(t *testing.T) { | ||
servidor := criarServidorComAtraso(25 * time.Millisecond) | ||
|
||
defer servidor.Close() | ||
|
||
_, err := Configuravel(servidor.URL, servidor.URL, 20*time.Millisecond) | ||
|
||
if err == nil { | ||
t.Error("esperava um erro, mas não obtive um") | ||
} | ||
}) | ||
} | ||
|
||
func criarServidorComAtraso(atraso time.Duration) *httptest.Server { | ||
return httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
time.Sleep(atraso) | ||
w.WriteHeader(http.StatusOK) | ||
})) | ||
} |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.