Skip to content

Commit

Permalink
Use pester for callback requests (#483)
Browse files Browse the repository at this point in the history
Signed-off-by: liz <[email protected]>
  • Loading branch information
liztio authored and timothysc committed Jul 12, 2018
1 parent 43b287a commit 786ff49
Show file tree
Hide file tree
Showing 2 changed files with 100 additions and 3 deletions.
10 changes: 7 additions & 3 deletions pkg/worker/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package worker
import (
"bytes"
"encoding/json"
"fmt"
"io"
"net/http"

Expand Down Expand Up @@ -55,8 +56,11 @@ func DoRequest(url string, client *http.Client, callback func() (io.Reader, stri

// And if we can't even do that, log it.
resp, err := pesterClient.Do(req)
if err != nil || resp.StatusCode != http.StatusOK {
errlog.LogError(errors.Wrapf(err, "could not send error message to master URL (%v)", url))
if err == nil && resp.StatusCode != http.StatusOK {
err = fmt.Errorf("unexpected status code %d", resp.StatusCode)
}
if err != nil {
errlog.LogError(errors.Wrapf(err, "could not send error message to master URL (%s)", url))
}

return errors.WithStack(err)
Expand All @@ -68,7 +72,7 @@ func DoRequest(url string, client *http.Client, callback func() (io.Reader, stri
}
req.Header.Add("content-type", mimeType)

resp, err := client.Do(req)
resp, err := pesterClient.Do(req)
if err != nil {
return errors.Wrapf(err, "error encountered dialing master at %v", url)
}
Expand Down
93 changes: 93 additions & 0 deletions pkg/worker/request_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
/*
Copyright 2018 Heptio Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package worker

import (
"bytes"
"io"
"net/http"
"net/http/httptest"
"sync"
"testing"

"github.com/pkg/errors"
)

func TestErrorRequestRetry(t *testing.T) {

tests := []struct {
name string
f func() (io.Reader, string, error)
}{
{
name: "error request retry",
f: func() (io.Reader, string, error) {
return nil, "", errors.New("didn't succeed")
},
},
{
name: "success request retry",
f: func() (io.Reader, string, error) {
return bytes.NewBuffer([]byte("success!")), "success!", nil
},
},
}

for _, test := range tests {

t.Run(test.name, func(t *testing.T) {
testServer := &testServer{
responseCodes: []int{500, 200},
}

server := httptest.NewTLSServer(testServer)
defer server.Close()

err := DoRequest(server.URL, server.Client(), test.f)

if err != nil {
t.Errorf("unexpected error: %v", err)
}

if testServer.responseCount != 2 {
t.Errorf("expected 2 requests, got %d", testServer.responseCount)
}
})
}
}

type testServer struct {
sync.Mutex
responseCodes []int
responseCount int
}

func (t *testServer) ServeHTTP(w http.ResponseWriter, r *http.Request) {
t.Lock()
defer t.Unlock()

responseCode := 500

if len(t.responseCodes) > 0 {
responseCode, t.responseCodes = t.responseCodes[0], t.responseCodes[1:]
}

w.WriteHeader(responseCode)
w.Write([]byte("ok!"))

t.responseCount++
}

0 comments on commit 786ff49

Please sign in to comment.