Skip to content

Commit

Permalink
Merge pull request #67 from mbrosowicz/master
Browse files Browse the repository at this point in the history
Tradução do capítulo referente ao context
  • Loading branch information
larien authored Dec 27, 2019
2 parents 759bc90 + 0b922f2 commit a61fd4f
Show file tree
Hide file tree
Showing 9 changed files with 142 additions and 142 deletions.
4 changes: 2 additions & 2 deletions context/v1/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ import (
"net/http"
)

// Store fetches data
// Store busca dados
type Store interface {
Fetch() string
}

// Server returns a handler for calling Store
// Server retorna um handler para chamar a Store
func Server(store Store) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
fmt.Fprint(w, store.Fetch())
Expand Down
4 changes: 2 additions & 2 deletions context/v1/context_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ func (s *StubStore) Fetch() string {
}

func TestServer(t *testing.T) {
data := "hello, world"
data := "olá, mundo"
svr := Server(&StubStore{data})

request := httptest.NewRequest(http.MethodGet, "/", nil)
Expand All @@ -24,6 +24,6 @@ func TestServer(t *testing.T) {
svr.ServeHTTP(response, request)

if response.Body.String() != data {
t.Errorf(`got "%s", want "%s"`, response.Body.String(), data)
t.Errorf(`recebi "%s", quero "%s"`, response.Body.String(), data)
}
}
4 changes: 2 additions & 2 deletions context/v2/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@ import (
"net/http"
)

// Store fetches data
// Store busca dados
type Store interface {
Fetch() string
Cancel()
}

// Server returns a handler for calling Store
// Server retorna um handler para chamar a Store
func Server(store Store) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
Expand Down
8 changes: 4 additions & 4 deletions context/v2/context_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ import (
)

func TestServer(t *testing.T) {
data := "hello, world"
data := "olá, mundo"

t.Run("returns data from store", func(t *testing.T) {
t.Run("retorna dados da store", func(t *testing.T) {
store := &SpyStore{response: data, t: t}
svr := Server(store)

Expand All @@ -21,13 +21,13 @@ func TestServer(t *testing.T) {
svr.ServeHTTP(response, request)

if response.Body.String() != data {
t.Errorf(`got "%s", want "%s"`, response.Body.String(), data)
t.Errorf(`recebi "%s", quero "%s"`, response.Body.String(), data)
}

store.assertWasNotCancelled()
})

t.Run("tells store to cancel work if request is cancelled", func(t *testing.T) {
t.Run("avisa a store para cancelar o trabalho se a requisicao for cancelada", func(t *testing.T) {
store := &SpyStore{response: data, t: t}
svr := Server(store)

Expand Down
10 changes: 5 additions & 5 deletions context/v2/testdoubles.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,34 +5,34 @@ import (
"time"
)

// SpyStore allows you to simulate a store and see how its used
// SpyStore permite que você simule uma store e veja como ela é usada
type SpyStore struct {
response string
cancelled bool
t *testing.T
}

// Fetch returns response after a short delay
// Fetch retorna a resposta após um curto atraso
func (s *SpyStore) Fetch() string {
time.Sleep(100 * time.Millisecond)
return s.response
}

// Cancel will record the call
// Cancel irá gravar a chamada
func (s *SpyStore) Cancel() {
s.cancelled = true
}

func (s *SpyStore) assertWasCancelled() {
s.t.Helper()
if !s.cancelled {
s.t.Errorf("store was not told to cancel")
s.t.Errorf("store nao foi avisada para cancelar")
}
}

func (s *SpyStore) assertWasNotCancelled() {
s.t.Helper()
if s.cancelled {
s.t.Errorf("store was told to cancel")
s.t.Errorf("store foi avisada para cancelar")
}
}
6 changes: 3 additions & 3 deletions context/v3/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,18 @@ import (
"net/http"
)

// Store fetches data
// Store busca dados
type Store interface {
Fetch(ctx context.Context) (string, error)
}

// Server returns a handler for calling Store
// Server retorna um handler para chamar a Store
func Server(store Store) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
data, err := store.Fetch(r.Context())

if err != nil {
return // todo: log error however you like
return // todo: registre o erro como você quiser
}

fmt.Fprint(w, data)
Expand Down
10 changes: 5 additions & 5 deletions context/v3/context_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ import (
)

func TestServer(t *testing.T) {
data := "hello, world"
data := "olá, mundo"

t.Run("returns data from store", func(t *testing.T) {
t.Run("retorna dados da store", func(t *testing.T) {
store := &SpyStore{response: data, t: t}
svr := Server(store)

Expand All @@ -21,11 +21,11 @@ func TestServer(t *testing.T) {
svr.ServeHTTP(response, request)

if response.Body.String() != data {
t.Errorf(`got "%s", want "%s"`, response.Body.String(), data)
t.Errorf(`recebi "%s", quero "%s"`, response.Body.String(), data)
}
})

t.Run("tells store to cancel work if request is cancelled", func(t *testing.T) {
t.Run("avisa a store para cancelar o trabalho se a requisicao for cancelada", func(t *testing.T) {
store := &SpyStore{response: data, t: t}
svr := Server(store)

Expand All @@ -40,7 +40,7 @@ func TestServer(t *testing.T) {
svr.ServeHTTP(response, request)

if response.written {
t.Error("a response should not have been written")
t.Error("uma resposta nao deveria ter sido escrita")
}
})
}
16 changes: 8 additions & 8 deletions context/v3/testdoubles.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@ import (
"time"
)

// SpyStore allows you to simulate a store and see how its used
// SpyStore permite que você simule uma store e veja como ela é usada
type SpyStore struct {
response string
t *testing.T
}

// Fetch returns response after a short delay
// Fetch retorna a resposta após um curto atraso
func (s *SpyStore) Fetch(ctx context.Context) (string, error) {
data := make(chan string, 1)

Expand All @@ -23,7 +23,7 @@ func (s *SpyStore) Fetch(ctx context.Context) (string, error) {
for _, c := range s.response {
select {
case <-ctx.Done():
s.t.Log("spy store got cancelled")
s.t.Log("spy store foi cancelado")
return
default:
time.Sleep(10 * time.Millisecond)
Expand All @@ -41,24 +41,24 @@ func (s *SpyStore) Fetch(ctx context.Context) (string, error) {
}
}

// SpyResponseWriter checks whether a response has been written
// SpyResponseWriter verifica se uma resposta foi escrita
type SpyResponseWriter struct {
written bool
}

// Header will mark written to true
// Header marcará escrito (written) para verdadeiro
func (s *SpyResponseWriter) Header() http.Header {
s.written = true
return nil
}

// Write will mark written to true
// Write marcará escrito (written) para verdadeiro
func (s *SpyResponseWriter) Write([]byte) (int, error) {
s.written = true
return 0, errors.New("not implemented")
return 0, errors.New("não implementado")
}

// WriteHeader will mark written to true
// WriteHeader marcará escrito (written) para verdadeiro
func (s *SpyResponseWriter) WriteHeader(statusCode int) {
s.written = true
}
Loading

0 comments on commit a61fd4f

Please sign in to comment.