From d64aa06b64fd9e7766bd1c26c38f0a79feb947c1 Mon Sep 17 00:00:00 2001 From: Jeevanandam M Date: Sun, 19 Mar 2017 10:57:11 -0700 Subject: [PATCH] test case improvement --- client12.go | 1 + client_test.go | 4 ++-- context_test.go | 12 ++++-------- resty_test.go | 16 ++++++---------- 4 files changed, 13 insertions(+), 20 deletions(-) diff --git a/client12.go b/client12.go index 72a3b850..b7f6ee5b 100644 --- a/client12.go +++ b/client12.go @@ -20,5 +20,6 @@ func (c *Client) SetTimeout(timeout time.Duration) *Client { } c.transport.ResponseHeaderTimeout = timeout c.httpClient.Transport = c.transport + return c } diff --git a/client_test.go b/client_test.go index bb735cb2..50d57618 100644 --- a/client_test.go +++ b/client_test.go @@ -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) } diff --git a/context_test.go b/context_test.go index 43ec3550..7a5226a6 100644 --- a/context_test.go +++ b/context_test.go @@ -12,7 +12,7 @@ import ( "context" "net/http" "strings" - "sync" + "sync/atomic" "testing" "time" ) @@ -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")) diff --git a/resty_test.go b/resty_test.go index b5722df8..86b02947 100644 --- a/resty_test.go +++ b/resty_test.go @@ -21,7 +21,6 @@ import ( "reflect" "strconv" "strings" - "sync" "sync/atomic" "testing" "time" @@ -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)) @@ -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) { @@ -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" {