diff options
author | Valery Piashchynski <[email protected]> | 2019-11-17 18:13:15 +0300 |
---|---|---|
committer | Valery Piashchynski <[email protected]> | 2019-11-17 18:13:15 +0300 |
commit | a7982f6e692e9dc40f035d5f1486cb2bbae71f88 (patch) | |
tree | ab4b32c190bc8bffd9b1b99b806e03192729c59a /service | |
parent | c463235f55cdae5fa7c63a87e7cc73c6e535d27a (diff) |
finish the check
Diffstat (limited to 'service')
-rw-r--r-- | service/health/service.go | 9 | ||||
-rw-r--r-- | service/http/h2c_test.go | 14 | ||||
-rw-r--r-- | service/http/handler.go | 10 | ||||
-rw-r--r-- | service/http/handler_test.go | 6 | ||||
-rw-r--r-- | service/http/response.go | 10 | ||||
-rw-r--r-- | service/http/rpc_test.go | 21 | ||||
-rw-r--r-- | service/http/service.go | 27 | ||||
-rw-r--r-- | service/http/service_test.go | 79 | ||||
-rw-r--r-- | service/http/ssl_test.go | 56 | ||||
-rw-r--r-- | service/http/uploads.go | 41 | ||||
-rw-r--r-- | service/http/uploads_test.go | 196 | ||||
-rw-r--r-- | service/metrics/rpc.go | 30 | ||||
-rw-r--r-- | service/metrics/rpc_test.go | 7 | ||||
-rw-r--r-- | service/metrics/service.go | 10 | ||||
-rw-r--r-- | service/metrics/service_test.go | 37 | ||||
-rw-r--r-- | service/rpc/config_test.go | 47 |
16 files changed, 491 insertions, 109 deletions
diff --git a/service/health/service.go b/service/health/service.go index c0be68e0..a730de7e 100644 --- a/service/health/service.go +++ b/service/health/service.go @@ -2,6 +2,7 @@ package health import ( "context" + "fmt" "net/http" "sync" @@ -47,7 +48,13 @@ func (s *Service) Stop() { if s.http != nil { // gracefully stop the server - go s.http.Shutdown(context.Background()) + go func() { + err := s.http.Shutdown(context.Background()) + if err != nil { + // TODO how to log error here? + fmt.Println(fmt.Errorf("error shutting down the server: error %v", err)) + } + }() } } diff --git a/service/http/h2c_test.go b/service/http/h2c_test.go index d806e5ff..7bbc30ac 100644 --- a/service/http/h2c_test.go +++ b/service/http/h2c_test.go @@ -36,7 +36,12 @@ func Test_Service_H2C(t *testing.T) { // should do nothing s.(*Service).Stop() - go func() { c.Serve() }() + go func() { + err := c.Serve() + if err != nil { + t.Errorf("error serving: %v", err) + } + }() time.Sleep(time.Millisecond * 100) defer c.Stop() @@ -49,7 +54,12 @@ func Test_Service_H2C(t *testing.T) { r, err := http.DefaultClient.Do(req) assert.NoError(t, err) - defer r.Body.Close() + defer func() { + err := r.Body.Close() + if err != nil { + t.Errorf("fail to close the Body: error %v", err) + } + }() assert.Equal(t, "101 Switching Protocols", r.Status) diff --git a/service/http/handler.go b/service/http/handler.go index a4da224d..4de33844 100644 --- a/service/http/handler.go +++ b/service/http/handler.go @@ -120,7 +120,10 @@ func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) { } h.handleResponse(req, resp, start) - resp.Write(w) + err = resp.Write(w) + if err != nil { + h.handleError(w, r, err, start) + } } // handleError sends error. @@ -128,7 +131,10 @@ func (h *Handler) handleError(w http.ResponseWriter, r *http.Request, err error, h.throw(EventError, &ErrorEvent{Request: r, Error: err, start: start, elapsed: time.Since(start)}) w.WriteHeader(500) - w.Write([]byte(err.Error())) + _, err = w.Write([]byte(err.Error())) + if err != nil { + h.throw(EventError, &ErrorEvent{Request: r, Error: err, start: start, elapsed: time.Since(start)}) + } } // handleResponse triggers response event. diff --git a/service/http/handler_test.go b/service/http/handler_test.go index 91f0ca2b..0db999c9 100644 --- a/service/http/handler_test.go +++ b/service/http/handler_test.go @@ -24,6 +24,9 @@ func get(url string) (string, *http.Response, error) { return "", nil, err } b, err := ioutil.ReadAll(r.Body) + if err != nil { + return "", nil, err + } err = r.Body.Close() if err != nil { @@ -49,6 +52,9 @@ func getHeader(url string, h map[string]string) (string, *http.Response, error) } b, err := ioutil.ReadAll(r.Body) + if err != nil { + return "", nil, err + } err = r.Body.Close() if err != nil { diff --git a/service/http/response.go b/service/http/response.go index 8bd770ec..7102f078 100644 --- a/service/http/response.go +++ b/service/http/response.go @@ -35,7 +35,10 @@ func (r *Response) Write(w http.ResponseWriter) error { for _, v := range h { if n == "http2-push" { if pusher, ok := w.(http.Pusher); ok { - pusher.Push(v, nil) + err := pusher.Push(v, nil) + if err != nil { + return err + } } continue @@ -48,7 +51,10 @@ func (r *Response) Write(w http.ResponseWriter) error { w.WriteHeader(r.Status) if data, ok := r.body.([]byte); ok { - w.Write(data) + _, err := w.Write(data) + if err != nil { + return err + } } if rc, ok := r.body.(io.Reader); ok { diff --git a/service/http/rpc_test.go b/service/http/rpc_test.go index 669b201c..0e4b2c0a 100644 --- a/service/http/rpc_test.go +++ b/service/http/rpc_test.go @@ -49,7 +49,12 @@ func Test_RPC(t *testing.T) { s2, _ := c.Get(rpc.ID) rs := s2.(*rpc.Service) - go func() { c.Serve() }() + go func() { + err := c.Serve() + if err != nil { + t.Errorf("error during the Serve: error %v", err) + } + }() time.Sleep(time.Millisecond * 100) defer c.Stop() @@ -110,7 +115,12 @@ func Test_RPC_Unix(t *testing.T) { s2, _ := c.Get(rpc.ID) rs := s2.(*rpc.Service) - go func() { c.Serve() }() + go func() { + err := c.Serve() + if err != nil { + t.Errorf("error during the Serve: error %v", err) + } + }() time.Sleep(time.Millisecond * 100) defer c.Stop() @@ -164,7 +174,12 @@ func Test_Workers(t *testing.T) { s2, _ := c.Get(rpc.ID) rs := s2.(*rpc.Service) - go func() { c.Serve() }() + go func() { + err := c.Serve() + if err != nil { + t.Errorf("error during the Serve: error %v", err) + } + }() time.Sleep(time.Millisecond * 100) defer c.Stop() diff --git a/service/http/service.go b/service/http/service.go index 58038acb..1547538b 100644 --- a/service/http/service.go +++ b/service/http/service.go @@ -157,15 +157,36 @@ func (s *Service) Stop() { defer s.mu.Unlock() if s.fcgi != nil { - go s.fcgi.Shutdown(context.Background()) + go func() { + err := s.fcgi.Shutdown(context.Background()) + if err != nil { + // TODO think about returning error from this Stop function + // Stop() error + // push error from goroutines to the channel and block unil error or success shutdown or timeout + fmt.Println(fmt.Errorf("error shutting down the server, error: %v", err)) + return + } + }() } if s.https != nil { - go s.https.Shutdown(context.Background()) + go func() { + err := s.fcgi.Shutdown(context.Background()) + if err != nil { + fmt.Println(fmt.Errorf("error shutting down the server, error: %v", err)) + return + } + }() } if s.http != nil { - go s.http.Shutdown(context.Background()) + go func() { + err := s.fcgi.Shutdown(context.Background()) + if err != nil { + fmt.Println(fmt.Errorf("error shutting down the server, error: %v", err)) + return + } + }() } } diff --git a/service/http/service_test.go b/service/http/service_test.go index 69cb7003..bfc10971 100644 --- a/service/http/service_test.go +++ b/service/http/service_test.go @@ -53,7 +53,10 @@ func Test_Service_NoConfig(t *testing.T) { c := service.NewContainer(logger) c.Register(ID, &Service{}) - c.Init(&testCfg{httpCfg: `{"Enable":true}`}) + err := c.Init(&testCfg{httpCfg: `{"Enable":true}`}) + if err != nil { + t.Errorf("error during the Init: error %v", err) + } s, st := c.Get(ID) assert.NotNil(t, s) @@ -138,7 +141,12 @@ func Test_Service_Echo(t *testing.T) { // should do nothing s.(*Service).Stop() - go func() { c.Serve() }() + go func() { + err := c.Serve() + if err != nil { + t.Errorf("serve error: %v", err) + } + }() time.Sleep(time.Millisecond * 100) defer c.Stop() @@ -147,7 +155,12 @@ func Test_Service_Echo(t *testing.T) { r, err := http.DefaultClient.Do(req) assert.NoError(t, err) - defer r.Body.Close() + defer func() { + err := r.Body.Close() + if err != nil { + t.Errorf("error closing the Body: error %v", err) + } + }() b, err := ioutil.ReadAll(r.Body) assert.NoError(t, err) @@ -191,7 +204,12 @@ func Test_Service_Env(t *testing.T) { // should do nothing s.(*Service).Stop() - go func() { c.Serve() }() + go func() { + err := c.Serve() + if err != nil { + t.Errorf("serve error: %v", err) + } + }() time.Sleep(time.Millisecond * 100) defer c.Stop() @@ -200,7 +218,12 @@ func Test_Service_Env(t *testing.T) { r, err := http.DefaultClient.Do(req) assert.NoError(t, err) - defer r.Body.Close() + defer func() { + err := r.Body.Close() + if err != nil { + t.Errorf("error closing the Body: error %v", err) + } + }() b, err := ioutil.ReadAll(r.Body) assert.NoError(t, err) @@ -249,7 +272,12 @@ func Test_Service_ErrorEcho(t *testing.T) { } }) - go func() { c.Serve() }() + go func() { + err := c.Serve() + if err != nil { + t.Errorf("serve error: %v", err) + } + }() time.Sleep(time.Millisecond * 100) defer c.Stop() @@ -258,7 +286,12 @@ func Test_Service_ErrorEcho(t *testing.T) { r, err := http.DefaultClient.Do(req) assert.NoError(t, err) - defer r.Body.Close() + defer func() { + err := r.Body.Close() + if err != nil { + t.Errorf("error closing the Body: error %v", err) + } + }() b, err := ioutil.ReadAll(r.Body) assert.NoError(t, err) @@ -304,14 +337,22 @@ func Test_Service_Middleware(t *testing.T) { return func(w http.ResponseWriter, r *http.Request) { if r.URL.Path == "/halt" { w.WriteHeader(500) - w.Write([]byte("halted")) + _, err := w.Write([]byte("halted")) + if err != nil { + t.Errorf("error writing the data to the http reply: error %v", err) + } } else { f(w, r) } } }) - go func() { c.Serve() }() + go func() { + err := c.Serve() + if err != nil { + t.Errorf("serve error: %v", err) + } + }() time.Sleep(time.Millisecond * 100) defer c.Stop() @@ -320,7 +361,6 @@ func Test_Service_Middleware(t *testing.T) { r, err := http.DefaultClient.Do(req) assert.NoError(t, err) - defer r.Body.Close() b, err := ioutil.ReadAll(r.Body) assert.NoError(t, err) @@ -329,19 +369,27 @@ func Test_Service_Middleware(t *testing.T) { assert.Equal(t, 201, r.StatusCode) assert.Equal(t, "WORLD", string(b)) + err = r.Body.Close() + if err != nil { + t.Errorf("error closing the Body: error %v", err) + } + req, err = http.NewRequest("GET", "http://localhost:6029/halt", nil) assert.NoError(t, err) r, err = http.DefaultClient.Do(req) assert.NoError(t, err) - defer r.Body.Close() - b, err = ioutil.ReadAll(r.Body) assert.NoError(t, err) assert.NoError(t, err) assert.Equal(t, 500, r.StatusCode) assert.Equal(t, "halted", string(b)) + + err = r.Body.Close() + if err != nil { + t.Errorf("error closing the Body: error %v", err) + } } func Test_Service_Listener(t *testing.T) { @@ -381,7 +429,12 @@ func Test_Service_Listener(t *testing.T) { } }) - go func() { c.Serve() }() + go func() { + err := c.Serve() + if err != nil { + t.Errorf("serve error: %v", err) + } + }() time.Sleep(time.Millisecond * 100) c.Stop() diff --git a/service/http/ssl_test.go b/service/http/ssl_test.go index 63eb90b1..c9b4d090 100644 --- a/service/http/ssl_test.go +++ b/service/http/ssl_test.go @@ -47,7 +47,12 @@ func Test_SSL_Service_Echo(t *testing.T) { // should do nothing s.(*Service).Stop() - go func() { c.Serve() }() + go func() { + err := c.Serve() + if err != nil { + t.Errorf("error during the Serve: error %v", err) + } + }() time.Sleep(time.Millisecond * 100) defer c.Stop() @@ -56,7 +61,12 @@ func Test_SSL_Service_Echo(t *testing.T) { r, err := sslClient.Do(req) assert.NoError(t, err) - defer r.Body.Close() + defer func() { + err := r.Body.Close() + if err != nil { + t.Errorf("fail to close the Body: error %v", err) + } + }() b, err := ioutil.ReadAll(r.Body) assert.NoError(t, err) @@ -93,7 +103,12 @@ func Test_SSL_Service_NoRedirect(t *testing.T) { // should do nothing s.(*Service).Stop() - go func() { c.Serve() }() + go func() { + err := c.Serve() + if err != nil { + t.Errorf("error during the Serve: error %v", err) + } + }() time.Sleep(time.Millisecond * 100) defer c.Stop() @@ -102,7 +117,12 @@ func Test_SSL_Service_NoRedirect(t *testing.T) { r, err := sslClient.Do(req) assert.NoError(t, err) - defer r.Body.Close() + defer func() { + err := r.Body.Close() + if err != nil { + t.Errorf("fail to close the Body: error %v", err) + } + }() assert.Nil(t, r.TLS) @@ -142,7 +162,12 @@ func Test_SSL_Service_Redirect(t *testing.T) { // should do nothing s.(*Service).Stop() - go func() { c.Serve() }() + go func() { + err := c.Serve() + if err != nil { + t.Errorf("error during the Serve: error %v", err) + } + }() time.Sleep(time.Millisecond * 100) defer c.Stop() @@ -151,7 +176,12 @@ func Test_SSL_Service_Redirect(t *testing.T) { r, err := sslClient.Do(req) assert.NoError(t, err) - defer r.Body.Close() + defer func() { + err := r.Body.Close() + if err != nil { + t.Errorf("fail to close the Body: error %v", err) + } + }() assert.NotNil(t, r.TLS) @@ -191,7 +221,12 @@ func Test_SSL_Service_Push(t *testing.T) { // should do nothing s.(*Service).Stop() - go func() { c.Serve() }() + go func() { + err := c.Serve() + if err != nil { + t.Errorf("error during the Serve: error %v", err) + } + }() time.Sleep(time.Millisecond * 100) defer c.Stop() @@ -200,7 +235,12 @@ func Test_SSL_Service_Push(t *testing.T) { r, err := sslClient.Do(req) assert.NoError(t, err) - defer r.Body.Close() + defer func() { + err := r.Body.Close() + if err != nil { + t.Errorf("fail to close the Body: error %v", err) + } + }() assert.NotNil(t, r.TLS) diff --git a/service/http/uploads.go b/service/http/uploads.go index 7610ab28..7efa7e4a 100644 --- a/service/http/uploads.go +++ b/service/http/uploads.go @@ -2,6 +2,7 @@ package http import ( "encoding/json" + "fmt" "io" "io/ioutil" "mime/multipart" @@ -51,7 +52,11 @@ func (u *Uploads) Open() { wg.Add(1) go func(f *FileUpload) { defer wg.Done() - f.Open(u.cfg) + err := f.Open(u.cfg) + if err != nil { + // TODO handle error mechanism in goroutines + fmt.Println(fmt.Errorf("error opening the file: error %v", err)) + } }(f) } @@ -62,7 +67,11 @@ func (u *Uploads) Open() { func (u *Uploads) Clear() { for _, f := range u.list { if f.TempFilename != "" && exists(f.TempFilename) { - os.Remove(f.TempFilename) + err := os.Remove(f.TempFilename) + if err != nil { + // TODO error handling mechanism + fmt.Println(fmt.Errorf("error removing the file: error %v", err)) + } } } } @@ -99,7 +108,13 @@ func NewUpload(f *multipart.FileHeader) *FileUpload { } // Open moves file content into temporary file available for PHP. -func (f *FileUpload) Open(cfg *UploadsConfig) error { +// NOTE: +// There is 2 deferred functions, and in case of getting 2 errors from both functions +// error from close of temp file would be overwritten by error from the main file +// STACK +// DEFER FILE CLOSE (2) +// DEFER TMP CLOSE (1) +func (f *FileUpload) Open(cfg *UploadsConfig) (err error) { if cfg.Forbids(f.Name) { f.Error = UploadErrorExtension return nil @@ -110,7 +125,12 @@ func (f *FileUpload) Open(cfg *UploadsConfig) error { f.Error = UploadErrorNoFile return err } - defer file.Close() + + defer func() { + // close the main file + err = file.Close() + }() + tmp, err := ioutil.TempFile(cfg.TmpDir(), "upload") if err != nil { @@ -120,7 +140,10 @@ func (f *FileUpload) Open(cfg *UploadsConfig) error { } f.TempFilename = tmp.Name() - defer tmp.Close() + defer func() { + // close the temp file + err = tmp.Close() + }() if f.Size, err = io.Copy(tmp, file); err != nil { f.Error = UploadErrorCantWrite @@ -131,10 +154,8 @@ func (f *FileUpload) Open(cfg *UploadsConfig) error { // exists if file exists. func exists(path string) bool { - _, err := os.Stat(path) - if err == nil { - return true + if _, err := os.Stat(path); os.IsNotExist(err) { + return false } - - return false + return true } diff --git a/service/http/uploads_test.go b/service/http/uploads_test.go index 0fbf0e14..c5de224b 100644 --- a/service/http/uploads_test.go +++ b/service/http/uploads_test.go @@ -6,6 +6,7 @@ import ( "crypto/md5" "encoding/hex" "encoding/json" + "fmt" "github.com/spiral/roadrunner" "github.com/stretchr/testify/assert" "io" @@ -41,22 +42,43 @@ func TestHandler_Upload_File(t *testing.T) { defer h.rr.Stop() hs := &http.Server{Addr: ":8021", Handler: h} - defer hs.Shutdown(context.Background()) - - go func() { hs.ListenAndServe() }() + defer func() { + err := hs.Shutdown(context.Background()) + if err != nil { + t.Errorf("error during the shutdown: error %v", err) + } + }() + + go func() { + err := hs.ListenAndServe() + if err != nil { + t.Errorf("error listening the interface: error %v", err) + } + }() time.Sleep(time.Millisecond * 10) var mb bytes.Buffer w := multipart.NewWriter(&mb) f := mustOpen("uploads_test.go") - defer f.Close() + defer func() { + err := f.Close() + if err != nil { + t.Errorf("failed to close a file: error %v", err) + } + }() fw, err := w.CreateFormFile("upload", f.Name()) assert.NotNil(t, fw) assert.NoError(t, err) - io.Copy(fw, f) + _, err = io.Copy(fw, f) + if err != nil { + t.Errorf("error copying the file: error %v", err) + } - w.Close() + err = w.Close() + if err != nil { + t.Errorf("error closing the file: error %v", err) + } req, err := http.NewRequest("POST", "http://localhost"+hs.Addr, &mb) assert.NoError(t, err) @@ -65,7 +87,12 @@ func TestHandler_Upload_File(t *testing.T) { r, err := http.DefaultClient.Do(req) assert.NoError(t, err) - defer r.Body.Close() + defer func() { + err := r.Body.Close() + if err != nil { + t.Errorf("error closing the Body: error %v", err) + } + }() b, err := ioutil.ReadAll(r.Body) assert.NoError(t, err) @@ -102,22 +129,43 @@ func TestHandler_Upload_NestedFile(t *testing.T) { defer h.rr.Stop() hs := &http.Server{Addr: ":8021", Handler: h} - defer hs.Shutdown(context.Background()) - - go func() { hs.ListenAndServe() }() + defer func() { + err := hs.Shutdown(context.Background()) + if err != nil { + t.Errorf("error during the shutdown: error %v", err) + } + }() + + go func() { + err := hs.ListenAndServe() + if err != nil { + t.Errorf("error listening the interface: error %v", err) + } + }() time.Sleep(time.Millisecond * 10) var mb bytes.Buffer w := multipart.NewWriter(&mb) f := mustOpen("uploads_test.go") - defer f.Close() + defer func() { + err := f.Close() + if err != nil { + t.Errorf("failed to close a file: error %v", err) + } + }() fw, err := w.CreateFormFile("upload[x][y][z][]", f.Name()) assert.NotNil(t, fw) assert.NoError(t, err) - io.Copy(fw, f) + _, err = io.Copy(fw, f) + if err != nil { + t.Errorf("error copying the file: error %v", err) + } - w.Close() + err = w.Close() + if err != nil { + t.Errorf("error closing the file: error %v", err) + } req, err := http.NewRequest("POST", "http://localhost"+hs.Addr, &mb) assert.NoError(t, err) @@ -126,7 +174,12 @@ func TestHandler_Upload_NestedFile(t *testing.T) { r, err := http.DefaultClient.Do(req) assert.NoError(t, err) - defer r.Body.Close() + defer func() { + err := r.Body.Close() + if err != nil { + t.Errorf("error closing the Body: error %v", err) + } + }() b, err := ioutil.ReadAll(r.Body) assert.NoError(t, err) @@ -163,22 +216,43 @@ func TestHandler_Upload_File_NoTmpDir(t *testing.T) { defer h.rr.Stop() hs := &http.Server{Addr: ":8021", Handler: h} - defer hs.Shutdown(context.Background()) - - go func() { hs.ListenAndServe() }() + defer func() { + err := hs.Shutdown(context.Background()) + if err != nil { + t.Errorf("error during the shutdown: error %v", err) + } + }() + + go func() { + err := hs.ListenAndServe() + if err != nil { + t.Errorf("error listening the interface: error %v", err) + } + }() time.Sleep(time.Millisecond * 10) var mb bytes.Buffer w := multipart.NewWriter(&mb) f := mustOpen("uploads_test.go") - defer f.Close() + defer func() { + err := f.Close() + if err != nil { + t.Errorf("failed to close a file: error %v", err) + } + }() fw, err := w.CreateFormFile("upload", f.Name()) assert.NotNil(t, fw) assert.NoError(t, err) - io.Copy(fw, f) + _, err = io.Copy(fw, f) + if err != nil { + t.Errorf("error copying the file: error %v", err) + } - w.Close() + err = w.Close() + if err != nil { + t.Errorf("error closing the file: error %v", err) + } req, err := http.NewRequest("POST", "http://localhost"+hs.Addr, &mb) assert.NoError(t, err) @@ -187,7 +261,12 @@ func TestHandler_Upload_File_NoTmpDir(t *testing.T) { r, err := http.DefaultClient.Do(req) assert.NoError(t, err) - defer r.Body.Close() + defer func() { + err := r.Body.Close() + if err != nil { + t.Errorf("error closing the Body: error %v", err) + } + }() b, err := ioutil.ReadAll(r.Body) assert.NoError(t, err) @@ -224,22 +303,43 @@ func TestHandler_Upload_File_Forbids(t *testing.T) { defer h.rr.Stop() hs := &http.Server{Addr: ":8021", Handler: h} - defer hs.Shutdown(context.Background()) - - go func() { hs.ListenAndServe() }() + defer func() { + err := hs.Shutdown(context.Background()) + if err != nil { + t.Errorf("error during the shutdown: error %v", err) + } + }() + + go func() { + err := hs.ListenAndServe() + if err != nil { + t.Errorf("error listening the interface: error %v", err) + } + }() time.Sleep(time.Millisecond * 10) var mb bytes.Buffer w := multipart.NewWriter(&mb) f := mustOpen("uploads_test.go") - defer f.Close() + defer func() { + err := f.Close() + if err != nil { + t.Errorf("failed to close a file: error %v", err) + } + }() fw, err := w.CreateFormFile("upload", f.Name()) assert.NotNil(t, fw) assert.NoError(t, err) - io.Copy(fw, f) + _, err = io.Copy(fw, f) + if err != nil { + t.Errorf("error copying the file: error %v", err) + } - w.Close() + err = w.Close() + if err != nil { + t.Errorf("error closing the file: error %v", err) + } req, err := http.NewRequest("POST", "http://localhost"+hs.Addr, &mb) assert.NoError(t, err) @@ -248,7 +348,12 @@ func TestHandler_Upload_File_Forbids(t *testing.T) { r, err := http.DefaultClient.Do(req) assert.NoError(t, err) - defer r.Body.Close() + defer func() { + err := r.Body.Close() + if err != nil { + t.Errorf("error closing the Body: error %v", err) + } + }() b, err := ioutil.ReadAll(r.Body) assert.NoError(t, err) @@ -282,28 +387,47 @@ type fInfo struct { MD5 string `json:"md5,omitempty"` } -func fileString(f string, err int, mime string) string { - s, _ := os.Stat(f) +func fileString(f string, errNo int, mime string) string { + s, err := os.Stat(f) + if err != nil { + fmt.Println(fmt.Errorf("error stat the file, error: %v", err)) + } + + ff, err := os.Open(f) + if err != nil { + fmt.Println(fmt.Errorf("error opening the file, error: %v", err)) + } + + defer func() { + er := ff.Close() + if er != nil { + fmt.Println(fmt.Errorf("error closing the file, error: %v", er)) + } + }() - ff, _ := os.Open(f) - defer ff.Close() h := md5.New() - io.Copy(h, ff) + _, err = io.Copy(h, ff) + if err != nil { + fmt.Println(fmt.Errorf("error copying the file, error: %v", err)) + } v := &fInfo{ Name: s.Name(), Size: s.Size(), - Error: err, + Error: errNo, Mime: mime, MD5: hex.EncodeToString(h.Sum(nil)), } - if err != 0 { + if errNo != 0 { v.MD5 = "" v.Size = 0 } - r, _ := json.Marshal(v) + r, err := json.Marshal(v) + if err != nil { + fmt.Println(fmt.Errorf("error marshalling fInfo, error: %v", err)) + } return string(r) } diff --git a/service/metrics/rpc.go b/service/metrics/rpc.go index ee8ef984..9e7de640 100644 --- a/service/metrics/rpc.go +++ b/service/metrics/rpc.go @@ -32,26 +32,26 @@ func (rpc *rpcServer) Add(m *Metric, ok *bool) (err error) { return fmt.Errorf("undefined collector `%s`", m.Name) } - switch c.(type) { + switch c := c.(type) { case prometheus.Gauge: - c.(prometheus.Gauge).Add(m.Value) + c.Add(m.Value) case *prometheus.GaugeVec: if len(m.Labels) == 0 { return fmt.Errorf("required labels for collector `%s`", m.Name) } - c.(*prometheus.GaugeVec).WithLabelValues(m.Labels...).Add(m.Value) + c.WithLabelValues(m.Labels...).Add(m.Value) case prometheus.Counter: - c.(prometheus.Counter).Add(m.Value) + c.Add(m.Value) case *prometheus.CounterVec: if len(m.Labels) == 0 { return fmt.Errorf("required labels for collector `%s`", m.Name) } - c.(*prometheus.CounterVec).WithLabelValues(m.Labels...).Add(m.Value) + c.WithLabelValues(m.Labels...).Add(m.Value) default: return fmt.Errorf("collector `%s` does not support method `Add`", m.Name) @@ -74,16 +74,16 @@ func (rpc *rpcServer) Sub(m *Metric, ok *bool) (err error) { return fmt.Errorf("undefined collector `%s`", m.Name) } - switch c.(type) { + switch c := c.(type) { case prometheus.Gauge: - c.(prometheus.Gauge).Sub(m.Value) + c.Sub(m.Value) case *prometheus.GaugeVec: if len(m.Labels) == 0 { return fmt.Errorf("required labels for collector `%s`", m.Name) } - c.(*prometheus.GaugeVec).WithLabelValues(m.Labels...).Sub(m.Value) + c.WithLabelValues(m.Labels...).Sub(m.Value) default: return fmt.Errorf("collector `%s` does not support method `Sub`", m.Name) } @@ -105,23 +105,23 @@ func (rpc *rpcServer) Observe(m *Metric, ok *bool) (err error) { return fmt.Errorf("undefined collector `%s`", m.Name) } - switch c.(type) { + switch c := c.(type) { case *prometheus.SummaryVec: if len(m.Labels) == 0 { return fmt.Errorf("required labels for collector `%s`", m.Name) } - c.(*prometheus.SummaryVec).WithLabelValues(m.Labels...).Observe(m.Value) + c.WithLabelValues(m.Labels...).Observe(m.Value) case prometheus.Histogram: - c.(prometheus.Histogram).Observe(m.Value) + c.Observe(m.Value) case *prometheus.HistogramVec: if len(m.Labels) == 0 { return fmt.Errorf("required labels for collector `%s`", m.Name) } - c.(*prometheus.HistogramVec).WithLabelValues(m.Labels...).Observe(m.Value) + c.WithLabelValues(m.Labels...).Observe(m.Value) default: return fmt.Errorf("collector `%s` does not support method `Observe`", m.Name) } @@ -143,16 +143,16 @@ func (rpc *rpcServer) Set(m *Metric, ok *bool) (err error) { return fmt.Errorf("undefined collector `%s`", m.Name) } - switch c.(type) { + switch c := c.(type) { case prometheus.Gauge: - c.(prometheus.Gauge).Set(m.Value) + c.Set(m.Value) case *prometheus.GaugeVec: if len(m.Labels) == 0 { return fmt.Errorf("required labels for collector `%s`", m.Name) } - c.(*prometheus.GaugeVec).WithLabelValues(m.Labels...).Set(m.Value) + c.WithLabelValues(m.Labels...).Set(m.Value) default: return fmt.Errorf("collector `%s` does not support method `Set`", m.Name) diff --git a/service/metrics/rpc_test.go b/service/metrics/rpc_test.go index 6d061f1d..feae927a 100644 --- a/service/metrics/rpc_test.go +++ b/service/metrics/rpc_test.go @@ -42,7 +42,12 @@ func setup(t *testing.T, metric string, portNum string) (*rpc2.Client, service.C assert.True(t, s.(*Service).Enabled()) - go func() { c.Serve() }() + go func() { + err := c.Serve() + if err != nil { + t.Errorf("error during the Serve: error %v", err) + } + }() time.Sleep(time.Millisecond * 100) client, err := rs.Client() diff --git a/service/metrics/service.go b/service/metrics/service.go index 4916b3e0..9e2a1a71 100644 --- a/service/metrics/service.go +++ b/service/metrics/service.go @@ -2,6 +2,7 @@ package metrics import ( "context" + "fmt" "github.com/prometheus/client_golang/prometheus" "github.com/prometheus/client_golang/prometheus/promhttp" "github.com/spiral/roadrunner/service/rpc" @@ -86,7 +87,14 @@ func (s *Service) Stop() { if s.http != nil { // gracefully stop server - go s.http.Shutdown(context.Background()) + go func() { + err := s.http.Shutdown(context.Background()) + if err != nil { + // TODO how to show error message? + // Function should be Stop() error + fmt.Println(fmt.Errorf("error shutting down the server: error %v", err)) + } + }() } } diff --git a/service/metrics/service_test.go b/service/metrics/service_test.go index 0cf6fd95..62e6f6d7 100644 --- a/service/metrics/service_test.go +++ b/service/metrics/service_test.go @@ -43,9 +43,16 @@ func get(url string) (string, *http.Response, error) { if err != nil { return "", nil, err } - defer r.Body.Close() b, err := ioutil.ReadAll(r.Body) + if err != nil { + return "", nil, err + } + + err = r.Body.Close() + if err != nil { + return "", nil, err + } return string(b), r, err } @@ -63,7 +70,12 @@ func TestService_Serve(t *testing.T) { s, _ := c.Get(ID) assert.NotNil(t, s) - go func() { c.Serve() }() + go func() { + err := c.Serve() + if err != nil { + t.Errorf("error during the Serve: error %v", err) + } + }() time.Sleep(time.Millisecond * 100) defer c.Stop() @@ -94,7 +106,12 @@ func Test_ServiceCustomMetric(t *testing.T) { assert.NoError(t, s.(*Service).Register(collector)) - go func() { c.Serve() }() + go func() { + err := c.Serve() + if err != nil { + t.Errorf("error during the Serve: error %v", err) + } + }() time.Sleep(time.Millisecond * 100) defer c.Stop() @@ -127,7 +144,12 @@ func Test_ServiceCustomMetricMust(t *testing.T) { s.(*Service).MustRegister(collector) - go func() { c.Serve() }() + go func() { + err := c.Serve() + if err != nil { + t.Errorf("error during the Serve: error %v", err) + } + }() time.Sleep(time.Millisecond * 100) defer c.Stop() @@ -160,7 +182,12 @@ func Test_ConfiguredMetric(t *testing.T) { assert.True(t, s.(*Service).Enabled()) - go func() { c.Serve() }() + go func() { + err := c.Serve() + if err != nil { + t.Errorf("error during the Serve: error %v", err) + } + }() time.Sleep(time.Millisecond * 100) defer c.Stop() diff --git a/service/rpc/config_test.go b/service/rpc/config_test.go index af261698..6791128b 100644 --- a/service/rpc/config_test.go +++ b/service/rpc/config_test.go @@ -40,7 +40,12 @@ func TestConfig_Listener(t *testing.T) { ln, err := cfg.Listener() assert.NoError(t, err) assert.NotNil(t, ln) - defer ln.Close() + defer func() { + err := ln.Close() + if err != nil { + t.Errorf("error closing the listener: error %v", err) + } + }() assert.Equal(t, "tcp", ln.Addr().Network()) assert.Equal(t, "[::]:18001", ln.Addr().String()) @@ -56,7 +61,12 @@ func TestConfig_ListenerUnix(t *testing.T) { ln, err := cfg.Listener() assert.NoError(t, err) assert.NotNil(t, ln) - defer ln.Close() + defer func() { + err := ln.Close() + if err != nil { + t.Errorf("error closing the listener: error %v", err) + } + }() assert.Equal(t, "unix", ln.Addr().Network()) assert.Equal(t, "file.sock", ln.Addr().String()) @@ -86,12 +96,22 @@ func TestConfig_Dialer(t *testing.T) { cfg := &Config{Listen: "tcp://:18001"} ln, _ := cfg.Listener() - defer ln.Close() + defer func() { + err := ln.Close() + if err != nil { + t.Errorf("error closing the listener: error %v", err) + } + }() conn, err := cfg.Dialer() assert.NoError(t, err) assert.NotNil(t, conn) - defer conn.Close() + defer func() { + err := conn.Close() + if err != nil { + t.Errorf("error closing the connection: error %v", err) + } + }() assert.Equal(t, "tcp", conn.RemoteAddr().Network()) assert.Equal(t, "127.0.0.1:18001", conn.RemoteAddr().String()) @@ -105,12 +125,22 @@ func TestConfig_DialerUnix(t *testing.T) { cfg := &Config{Listen: "unix://file.sock"} ln, _ := cfg.Listener() - defer ln.Close() + defer func() { + err := ln.Close() + if err != nil { + t.Errorf("error closing the listener: error %v", err) + } + }() conn, err := cfg.Dialer() assert.NoError(t, err) assert.NotNil(t, conn) - defer conn.Close() + defer func() { + err := conn.Close() + if err != nil { + t.Errorf("error closing the connection: error %v", err) + } + }() assert.Equal(t, "unix", conn.RemoteAddr().Network()) assert.Equal(t, "file.sock", conn.RemoteAddr().String()) @@ -138,7 +168,10 @@ func Test_Config_DialerErrorMethod(t *testing.T) { func Test_Config_Defaults(t *testing.T) { c := &Config{} - c.InitDefaults() + err := c.InitDefaults() + if err != nil { + t.Errorf("error during the InitDefaults: error %v", err) + } assert.Equal(t, true, c.Enable) assert.Equal(t, "tcp://127.0.0.1:6001", c.Listen) } |