Skip to content

Commit

Permalink
Merge pull request #69 from larien/traducao/select
Browse files Browse the repository at this point in the history
Traducao/select
  • Loading branch information
larien authored Dec 25, 2019
2 parents 308ce05 + 7a13680 commit e0752c5
Show file tree
Hide file tree
Showing 14 changed files with 389 additions and 394 deletions.
2 changes: 1 addition & 1 deletion primeiros-passos-com-go/mocks.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Mocking
# Mocks

[**Você pode encontrar todos os códigos para esse capítulo aqui**](https://github.com/larien/learn-go-with-tests/tree/master/mocks)

Expand Down
376 changes: 186 additions & 190 deletions primeiros-passos-com-go/select.md

Large diffs are not rendered by default.

24 changes: 24 additions & 0 deletions select/v1/corredor.go
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)
}
34 changes: 34 additions & 0 deletions select/v1/corredor_test.go
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)
}))
}
24 changes: 0 additions & 24 deletions select/v1/racer.go

This file was deleted.

34 changes: 0 additions & 34 deletions select/v1/racer_test.go

This file was deleted.

24 changes: 24 additions & 0 deletions select/v2/corredor.go
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
}
34 changes: 34 additions & 0 deletions select/v2/corredor_test.go
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)
}))
}
24 changes: 0 additions & 24 deletions select/v2/racer.go

This file was deleted.

34 changes: 0 additions & 34 deletions select/v2/racer_test.go

This file was deleted.

35 changes: 35 additions & 0 deletions select/v3/corredor.go
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
}
51 changes: 51 additions & 0 deletions select/v3/corredor_test.go
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)
}))
}
35 changes: 0 additions & 35 deletions select/v3/racer.go

This file was deleted.

Loading

0 comments on commit e0752c5

Please sign in to comment.