Skip to content

Commit

Permalink
test case improvement
Browse files Browse the repository at this point in the history
  • Loading branch information
jeevatkm committed Mar 19, 2017
1 parent 506bd6a commit d64aa06
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 20 deletions.
1 change: 1 addition & 0 deletions client12.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,6 @@ func (c *Client) SetTimeout(timeout time.Duration) *Client {
}
c.transport.ResponseHeaderTimeout = timeout
c.httpClient.Transport = c.transport

return c
}
4 changes: 2 additions & 2 deletions client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,12 +109,12 @@ func TestClientTimeoutWithinThreshold(t *testing.T) {
resp, err := c.R().Get(ts.URL + "/set-timeout-test-with-sequence")
assertError(t, err)

seq1, _ := strconv.ParseInt(resp.String(), 10, 64)
seq1, _ := strconv.ParseInt(resp.String(), 10, 32)

resp, err = c.R().Get(ts.URL + "/set-timeout-test-with-sequence")
assertError(t, err)

seq2, _ := strconv.ParseInt(resp.String(), 10, 64)
seq2, _ := strconv.ParseInt(resp.String(), 10, 32)

assertEqual(t, seq1+1, seq2)
}
Expand Down
12 changes: 4 additions & 8 deletions context_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
"context"
"net/http"
"strings"
"sync"
"sync/atomic"
"testing"
"time"
)
Expand Down Expand Up @@ -173,17 +173,13 @@ func TestSetContextCancelWithError(t *testing.T) {
}
}

var mcr = &sync.Mutex{}

func TestClientRetryWithSetContext(t *testing.T) {
attempt := 0
var attemptctx int32
ts := createTestServer(func(w http.ResponseWriter, r *http.Request) {
t.Logf("Method: %v", r.Method)
t.Logf("Path: %v", r.URL.Path)
mcr.Lock()
defer mcr.Unlock()
attempt++
if attempt != 3 {
attp := atomic.AddInt32(&attemptctx, 1)
if attp <= 3 {
time.Sleep(time.Second * 2)
}
_, _ = w.Write([]byte("TestClientRetry page"))
Expand Down
16 changes: 6 additions & 10 deletions resty_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import (
"reflect"
"strconv"
"strings"
"sync"
"sync/atomic"
"testing"
"time"
Expand Down Expand Up @@ -871,6 +870,7 @@ func TestProxySetting(t *testing.T) {
assertEqual(t, true, (c.transport.Proxy == nil))

SetProxy("http://sampleproxy:8888")
assertEqual(t, true, IsProxySet())
RemoveProxy()
assertEqual(t, true, (DefaultClient.proxyURL == nil))
assertEqual(t, true, (DefaultClient.transport.Proxy == nil))
Expand Down Expand Up @@ -1163,10 +1163,8 @@ func getTestDataPath() string {
return pwd + "/test-data"
}

// Used for retry testing...
var mc = &sync.Mutex{}
var attempt int
var sequence int64
var attempt int32
var sequence int32

func createGetServer(t *testing.T) *httptest.Server {
ts := createTestServer(func(w http.ResponseWriter, r *http.Request) {
Expand All @@ -1181,15 +1179,13 @@ func createGetServer(t *testing.T) *httptest.Server {
} else if r.URL.Path == "/mypage2" {
_, _ = w.Write([]byte("TestGet: text response from mypage2"))
} else if r.URL.Path == "/set-retrycount-test" {
mc.Lock()
defer mc.Unlock()
attempt++
if attempt != 3 {
attp := atomic.AddInt32(&attempt, 1)
if attp <= 3 {
time.Sleep(time.Second * 6)
}
_, _ = w.Write([]byte("TestClientRetry page"))
} else if r.URL.Path == "/set-timeout-test-with-sequence" {
seq := atomic.AddInt64(&sequence, 1)
seq := atomic.AddInt32(&sequence, 1)
time.Sleep(time.Second * 2)
_, _ = fmt.Fprintf(w, "%d", seq)
} else if r.URL.Path == "/set-timeout-test" {
Expand Down

0 comments on commit d64aa06

Please sign in to comment.