Skip to content

Commit

Permalink
fix data race
Browse files Browse the repository at this point in the history
  • Loading branch information
Seth Ammons committed Apr 22, 2016
1 parent 855a4e3 commit 9c0640f
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 8 deletions.
11 changes: 10 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ func (c *Client) pester(p params) (*http.Response, error) {
}

if c.hc == nil {
c.hc = http.DefaultClient
c.hc = &http.Client{}
c.hc.Transport = c.Transport
c.hc.CheckRedirect = c.CheckRedirect
c.hc.Jar = c.Jar
Expand Down Expand Up @@ -298,6 +298,8 @@ func (c *Client) pester(p params) (*http.Response, error) {

// LogString provides a string representation of the errors the client has seen
func (c *Client) LogString() string {
c.Lock()
defer c.Unlock()
var res string
for _, e := range c.ErrLog {
res += fmt.Sprintf("%d %s [%s] %s request-%d retry-%d error: %s\n",
Expand All @@ -306,6 +308,13 @@ func (c *Client) LogString() string {
return res
}

// LogErrCount is a helper method used primarily for test validation
func (c *Client) LogErrCount() int {
c.Lock()
defer c.Unlock()
return len(c.ErrLog)
}

// EmbedHTTPClient allows you to extend an existing Pester client with an
// underlying http.Client, such as https://godoc.org/golang.org/x/oauth2/google#DefaultClient
func (c *Client) EmbedHTTPClient(hc *http.Client) {
Expand Down
14 changes: 7 additions & 7 deletions main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ func TestConcurrentRequests(t *testing.T) {
t.Parallel()

c := pester.New()
c.Concurrency = 4
c.Concurrency = 3
c.KeepLog = true

nonExistantURL := "http://localhost:9000/foo"
Expand All @@ -32,16 +32,16 @@ func TestConcurrentRequests(t *testing.T) {
// in the event of an error, let's see what the logs were
t.Log("\n", c.LogString())

if got, want := len(c.ErrLog), c.Concurrency*c.MaxRetries; got != want {
if got, want := c.LogErrCount(), c.Concurrency*c.MaxRetries; got != want {
t.Errorf("got %d attempts, want %d", got, want)
}
}

func TestConcurrent4Retry0(t *testing.T) {
func TestConcurrent2Retry0(t *testing.T) {
t.Parallel()

c := pester.New()
c.Concurrency = 4
c.Concurrency = 2
c.MaxRetries = 0
c.KeepLog = true

Expand All @@ -55,7 +55,7 @@ func TestConcurrent4Retry0(t *testing.T) {
// in the event of an error, let's see what the logs were
t.Log("\n", c.LogString())

if got, want := len(c.ErrLog), c.Concurrency; got != want {
if got, want := c.LogErrCount(), c.Concurrency; got != want {
t.Errorf("got %d attempts, want %d", got, want)
}
}
Expand All @@ -80,7 +80,7 @@ func TestDefaultBackoff(t *testing.T) {
t.Error("got %d, want %d for concurrency", got, want)
}

if got, want := len(c.ErrLog), c.MaxRetries; got != want {
if got, want := c.LogErrCount(), c.MaxRetries; got != want {
t.Fatalf("got %d errors, want %d", got, want)
}

Expand Down Expand Up @@ -151,7 +151,7 @@ func TestExponentialBackoff(t *testing.T) {
// in the event of an error, let's see what the logs were
t.Log("\n", c.LogString())

if got, want := len(c.ErrLog), c.MaxRetries; got != want {
if got, want := c.LogErrCount(), c.MaxRetries; got != want {
t.Fatalf("got %d errors, want %d", got, want)
}

Expand Down

0 comments on commit 9c0640f

Please sign in to comment.