diff options
author | Valery Piashchynski <[email protected]> | 2020-03-26 19:59:18 +0300 |
---|---|---|
committer | Valery Piashchynski <[email protected]> | 2020-03-26 19:59:18 +0300 |
commit | f9c5ecbb31c9c8f63c5e793abb4afbf7311f33af (patch) | |
tree | 72124ae891db5591a5bdf8f8cfc883603bbf266b | |
parent | cbde9a2001657ae6a8e73e1a01dad6e660c2fc0a (diff) |
Update tests, add backoff mechanism
-rw-r--r-- | go.mod | 1 | ||||
-rw-r--r-- | go.sum | 4 | ||||
-rw-r--r-- | service/headers/service_test.go | 175 |
3 files changed, 124 insertions, 56 deletions
@@ -6,6 +6,7 @@ require ( github.com/NYTimes/gziphandler v1.1.1 github.com/StackExchange/wmi v0.0.0-20190523213315-cbe66965904d // indirect github.com/buger/goterm v0.0.0-20181115115552-c206103e1f37 + github.com/cenkalti/backoff/v4 v4.0.0 // indirect github.com/dustin/go-humanize v1.0.0 github.com/go-ole/go-ole v1.2.4 // indirect github.com/json-iterator/go v1.1.9 @@ -27,6 +27,10 @@ github.com/beorn7/perks v1.0.1 h1:VlbKKnNfV8bJzeqoa4cOKqO6bYr3WgKZxO8Z16+hsOM= github.com/beorn7/perks v1.0.1/go.mod h1:G2ZrVWU2WbWT9wwq4/hrbKbnv/1ERSJQ0ibhJ6rlkpw= github.com/buger/goterm v0.0.0-20181115115552-c206103e1f37 h1:uxxtrnACqI9zK4ENDMf0WpXfUsHP5V8liuq5QdgDISU= github.com/buger/goterm v0.0.0-20181115115552-c206103e1f37/go.mod h1:u9UyCz2eTrSGy6fbupqJ54eY5c4IC8gREQ1053dK12U= +github.com/cenkalti/backoff v1.1.0 h1:QnvVp8ikKCDWOsFheytRCoYWYPO/ObCTBGxT19Hc+yE= +github.com/cenkalti/backoff v2.2.1+incompatible h1:tNowT99t7UNflLxfYYSlKYsBpXdEet03Pg2g16Swow4= +github.com/cenkalti/backoff/v4 v4.0.0 h1:6VeaLF9aI+MAUQ95106HwWzYZgJJpZ4stumjj6RFYAU= +github.com/cenkalti/backoff/v4 v4.0.0/go.mod h1:eEew/i+1Q6OrCDZh3WiXYv3+nJwBASZ8Bog/87DQnVg= github.com/cespare/xxhash v1.1.0 h1:a6HrQnmkObjyL+Gs60czilIUGqrzKutQD6XZog3p+ko= github.com/cespare/xxhash v1.1.0/go.mod h1:XrSqR1VqqWfGrhpAt58auRo0WTKS1nRRg3ghfAqPWnc= github.com/cespare/xxhash/v2 v2.1.1 h1:6MnRN8NT7+YBpUIWxHtefFZOKTAPgGjpQSxqLNn0+qY= diff --git a/service/headers/service_test.go b/service/headers/service_test.go index e4bbfb84..7a28289c 100644 --- a/service/headers/service_test.go +++ b/service/headers/service_test.go @@ -1,6 +1,7 @@ package headers import ( + "github.com/cenkalti/backoff/v4" json "github.com/json-iterator/go" "github.com/sirupsen/logrus" "github.com/sirupsen/logrus/hooks/test" @@ -68,24 +69,39 @@ func Test_RequestHeaders(t *testing.T) { time.Sleep(time.Millisecond * 100) defer c.Stop() - req, err := http.NewRequest("GET", "http://localhost:6078?hello=value", nil) - assert.NoError(t, err) + bkoff := backoff.NewExponentialBackOff() + bkoff.MaxElapsedTime = time.Second * 15 - r, err := http.DefaultClient.Do(req) - assert.NoError(t, err) - defer func() { - err := r.Body.Close() + err := backoff.Retry(func() error { + req, err := http.NewRequest("GET", "http://localhost:6078?hello=value", nil) if err != nil { - t.Errorf("error during the body closing: error %v", err) + return err + } + + r, err := http.DefaultClient.Do(req) + if err != nil { + return err + } + + b, err := ioutil.ReadAll(r.Body) + if err != nil { + return err + } + + assert.Equal(t, 200, r.StatusCode) + assert.Equal(t, "CUSTOM-HEADER", string(b)) + + err = r.Body.Close() + if err != nil { + return err } - }() - b, err := ioutil.ReadAll(r.Body) - assert.NoError(t, err) + return nil + }, bkoff) - assert.NoError(t, err) - assert.Equal(t, 200, r.StatusCode) - assert.Equal(t, "CUSTOM-HEADER", string(b)) + if err != nil { + t.Fatal(err) + } } func Test_ResponseHeaders(t *testing.T) { @@ -122,25 +138,40 @@ func Test_ResponseHeaders(t *testing.T) { time.Sleep(time.Millisecond * 100) defer c.Stop() - req, err := http.NewRequest("GET", "http://localhost:6079?hello=value", nil) - assert.NoError(t, err) + bkoff := backoff.NewExponentialBackOff() + bkoff.MaxElapsedTime = time.Second * 15 - r, err := http.DefaultClient.Do(req) - assert.NoError(t, err) - defer func() { - err := r.Body.Close() + err := backoff.Retry(func() error { + req, err := http.NewRequest("GET", "http://localhost:6079?hello=value", nil) if err != nil { - t.Errorf("error during the body closing: error %v", err) + return err } - }() - assert.Equal(t, "output-header", r.Header.Get("output")) + r, err := http.DefaultClient.Do(req) + if err != nil { + return err + } + + assert.Equal(t, "output-header", r.Header.Get("output")) + + b, err := ioutil.ReadAll(r.Body) + if err != nil { + return err + } + assert.Equal(t, 200, r.StatusCode) + assert.Equal(t, "CUSTOM-HEADER", string(b)) + + err = r.Body.Close() + if err != nil { + return err + } - b, err := ioutil.ReadAll(r.Body) - assert.NoError(t, err) + return nil + }, bkoff) - assert.Equal(t, 200, r.StatusCode) - assert.Equal(t, "CUSTOM-HEADER", string(b)) + if err != nil { + t.Fatal(err) + } } func TestCORS_OPTIONS(t *testing.T) { @@ -186,29 +217,44 @@ func TestCORS_OPTIONS(t *testing.T) { time.Sleep(time.Millisecond * 100) defer c.Stop() - req, err := http.NewRequest("OPTIONS", "http://localhost:6379", nil) - assert.NoError(t, err) + bkoff := backoff.NewExponentialBackOff() + bkoff.MaxElapsedTime = time.Second * 15 - r, err := http.DefaultClient.Do(req) - assert.NoError(t, err) - defer func() { - err := r.Body.Close() + err := backoff.Retry(func() error { + req, err := http.NewRequest("OPTIONS", "http://localhost:6379", nil) if err != nil { - t.Errorf("error during the body closing: error %v", err) + return err } - }() - assert.Equal(t, "true", r.Header.Get("Access-Control-Allow-Credentials")) - assert.Equal(t, "*", r.Header.Get("Access-Control-Allow-Headers")) - assert.Equal(t, "GET,POST,PUT,DELETE", r.Header.Get("Access-Control-Allow-Methods")) - assert.Equal(t, "*", r.Header.Get("Access-Control-Allow-Origin")) - assert.Equal(t, "600", r.Header.Get("Access-Control-Max-Age")) - assert.Equal(t, "true", r.Header.Get("Access-Control-Allow-Credentials")) + r, err := http.DefaultClient.Do(req) + if err != nil { + return err + } + + assert.Equal(t, "true", r.Header.Get("Access-Control-Allow-Credentials")) + assert.Equal(t, "*", r.Header.Get("Access-Control-Allow-Headers")) + assert.Equal(t, "GET,POST,PUT,DELETE", r.Header.Get("Access-Control-Allow-Methods")) + assert.Equal(t, "*", r.Header.Get("Access-Control-Allow-Origin")) + assert.Equal(t, "600", r.Header.Get("Access-Control-Max-Age")) + assert.Equal(t, "true", r.Header.Get("Access-Control-Allow-Credentials")) + + _, err = ioutil.ReadAll(r.Body) + if err != nil { + return err + } + assert.Equal(t, 200, r.StatusCode) + + err = r.Body.Close() + if err != nil { + return err + } - _, err = ioutil.ReadAll(r.Body) - assert.NoError(t, err) + return nil + }, bkoff) - assert.Equal(t, 200, r.StatusCode) + if err != nil { + t.Fatal(err) + } } func TestCORS_Pass(t *testing.T) { @@ -254,23 +300,40 @@ func TestCORS_Pass(t *testing.T) { time.Sleep(time.Millisecond * 100) defer c.Stop() - req, err := http.NewRequest("GET", "http://localhost:6672", nil) - assert.NoError(t, err) + bkoff := backoff.NewExponentialBackOff() + bkoff.MaxElapsedTime = time.Second * 15 + + err := backoff.Retry(func() error { + req, err := http.NewRequest("GET", "http://localhost:6672", nil) + if err != nil { + return err + } + + r, err := http.DefaultClient.Do(req) + if err != nil { + return err + } + + assert.Equal(t, "true", r.Header.Get("Access-Control-Allow-Credentials")) + assert.Equal(t, "*", r.Header.Get("Access-Control-Allow-Headers")) + assert.Equal(t, "*", r.Header.Get("Access-Control-Allow-Origin")) + assert.Equal(t, "true", r.Header.Get("Access-Control-Allow-Credentials")) - r, err := http.DefaultClient.Do(req) - assert.NoError(t, err) - assert.Equal(t, "true", r.Header.Get("Access-Control-Allow-Credentials")) - assert.Equal(t, "*", r.Header.Get("Access-Control-Allow-Headers")) - assert.Equal(t, "*", r.Header.Get("Access-Control-Allow-Origin")) - assert.Equal(t, "true", r.Header.Get("Access-Control-Allow-Credentials")) + _, err = ioutil.ReadAll(r.Body) + if err != nil { + return err + } + assert.Equal(t, 200, r.StatusCode) - _, err = ioutil.ReadAll(r.Body) - assert.NoError(t, err) + err = r.Body.Close() + if err != nil { + return err + } - assert.Equal(t, 200, r.StatusCode) + return nil + }, bkoff) - err = r.Body.Close() if err != nil { - t.Errorf("error during the body closing: error %v", err) + t.Fatal(err) } } |