From 43f691e596c0f25683f35fd5771bbba72fc8193b Mon Sep 17 00:00:00 2001 From: vankichi Date: Thu, 4 Feb 2021 20:15:34 +0900 Subject: [PATCH 1/8] :white_check_mark: add test for internal/errors/runtime Signed-off-by: vankichi --- internal/errors/runtime.go | 5 +- internal/errors/runtime_test.go | 416 ++++++++++++++++++++++++++++++++ 2 files changed, 419 insertions(+), 2 deletions(-) create mode 100644 internal/errors/runtime_test.go diff --git a/internal/errors/runtime.go b/internal/errors/runtime.go index 74add69e88..e8b3733c5b 100644 --- a/internal/errors/runtime.go +++ b/internal/errors/runtime.go @@ -20,16 +20,17 @@ package errors import "runtime" var ( - // Runtime. - + // ErrPanicRecovered represents a function to generate an error that panic recovered. ErrPanicRecovered = func(err error, rec interface{}) error { return Wrap(err, Errorf("panic recovered: %v", rec).Error()) } + // ErrPanicString represents a function to generate an error that panic recovered with string message. ErrPanicString = func(err error, msg string) error { return Wrap(err, Errorf("panic recovered: %v", msg).Error()) } + // ErrRuntimeError represents a function to generate an error that panic caused by runtime error. ErrRuntimeError = func(err error, r runtime.Error) error { return Wrap(err, Errorf("system panicked caused by runtime error: %v", r).Error()) } diff --git a/internal/errors/runtime_test.go b/internal/errors/runtime_test.go new file mode 100644 index 0000000000..e09701b002 --- /dev/null +++ b/internal/errors/runtime_test.go @@ -0,0 +1,416 @@ +// +// Copyright (C) 2019-2021 vdaas.org vald team +// +// Licensed under the Apache LicensdefaultErr, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// https://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// + +package errors + +import ( + "fmt" + "math" + "runtime" + "testing" + "time" + + "go.uber.org/goleak" +) + +func TestErrPanicRecovered(t *testing.T) { + t.Parallel() + type args struct { + err error + rec interface{} + } + type want struct { + want error + } + type test struct { + name string + args args + want want + checkFunc func(want, error) error + beforeFunc func(args) + afterFunc func(args) + } + defaultCheckFunc := func(w want, got error) error { + if !Is(got, w.want) { + return Errorf("got: \"%#v\",\n\t\twant: \"%#v\"", got, w.want) + } + return nil + } + defaultErr := New("runtime error") + tests := []test{ + func() test { + r := math.MaxFloat64 + return test{ + name: "return an error when err is not empty and rec is int value", + args: args{ + err: defaultErr, + rec: r, + }, + want: want{ + want: Wrap(defaultErr, Errorf("panic recovered: %v", r).Error()), + }, + } + }(), + func() test { + r := math.MaxFloat64 + return test{ + name: "return an error when err is not empty and rec is float value", + args: args{ + err: defaultErr, + rec: r, + }, + want: want{ + want: Wrap(defaultErr, Errorf("panic recovered: %v", r).Error()), + }, + } + }(), + func() test { + tString := "10h" + var r time.Duration + r, _ = time.ParseDuration(tString) + return test{ + name: "return an error when err is not empty and rec is time.Duration value", + args: args{ + err: defaultErr, + rec: r, + }, + want: want{ + want: Wrap(defaultErr, Errorf("panic recovered: %v", r).Error()), + }, + } + }(), + func() test { + r := "10h" + return test{ + name: "return an error when err is not empty and rec is string value", + args: args{ + err: defaultErr, + rec: r, + }, + want: want{ + want: Wrap(defaultErr, Errorf("panic recovered: %v", r).Error()), + }, + } + }(), + func() test { + r := []byte{0x00, 0x01} + return test{ + name: "return an error when err is not empty and rec is byte slice value", + args: args{ + err: defaultErr, + rec: r, + }, + want: want{ + want: Wrap(defaultErr, Errorf("panic recovered: %v", r).Error()), + }, + } + }(), + func() test { + r := struct { + connectionType string + code int + }{ + connectionType: "grpc", + code: 404, + } + return test{ + name: "return an error when err is not empty and rec is struct value", + args: args{ + err: defaultErr, + rec: r, + }, + want: want{ + want: Wrap(defaultErr, Errorf("panic recovered: %v", r).Error()), + }, + } + }(), + func() test { + r := map[string]int{ + "uuid": 12345678, + } + return test{ + name: "return an error when err is not empty and rec is map value", + args: args{ + err: defaultErr, + rec: r, + }, + want: want{ + want: Wrap(defaultErr, Errorf("panic recovered: %v", r).Error()), + }, + } + }(), + func() test { + r := true + return test{ + name: "return an error when err is not empty and rec is boolean", + args: args{ + err: defaultErr, + rec: r, + }, + want: want{ + want: Wrap(defaultErr, Errorf("panic recovered: %v", r).Error()), + }, + } + }(), + func() test { + r := &defaultErr + return test{ + name: "return an error when err is not empty and rec is boolean", + args: args{ + err: defaultErr, + rec: r, + }, + want: want{ + want: Wrap(defaultErr, Errorf("panic recovered: %v", r).Error()), + }, + } + }(), + func() test { + r := "10h" + return test{ + name: "return an error when err is empty and rec has value", + args: args{ + rec: r, + }, + want: want{ + want: Wrap(nil, Errorf("panic recovered: %v", r).Error()), + }, + } + }(), + func() test { + return test{ + name: "return an error when err is not empty and rec is nil", + args: args{ + err: defaultErr, + }, + want: want{ + want: Wrap(defaultErr, Errorf("panic recovered: %v", nil).Error()), + }, + } + }(), + func() test { + return test{ + name: "return an error when err and rec are empty or nil", + args: args{}, + want: want{ + want: Wrap(nil, Errorf("panic recovered: %v", nil).Error()), + }, + } + }(), + } + for _, tc := range tests { + test := tc + t.Run(test.name, func(tt *testing.T) { + tt.Parallel() + defer goleak.VerifyNone(tt, goleak.IgnoreCurrent()) + if test.beforeFunc != nil { + test.beforeFunc(test.args) + } + if test.afterFunc != nil { + defer test.afterFunc(test.args) + } + if test.checkFunc == nil { + test.checkFunc = defaultCheckFunc + } + + got := ErrPanicRecovered(test.args.err, test.args.rec) + if err := test.checkFunc(test.want, got); err != nil { + tt.Errorf("error = %v", err) + } + }) + } +} + +func TestErrPanicString(t *testing.T) { + t.Parallel() + type args struct { + err error + msg string + } + type want struct { + want error + } + type test struct { + name string + args args + want want + checkFunc func(want, error) error + beforeFunc func(args) + afterFunc func(args) + } + defaultCheckFunc := func(w want, got error) error { + if !Is(got, w.want) { + return Errorf("got: \"%#v\",\n\t\t\t\twant: \"%#v\"", got, w.want) + } + return nil + } + defaultErr := New("runtime panic string error") + defaultMsg := "success" + tests := []test{ + func() test { + return test{ + name: "return an error when err is not nil and msg is not empty", + args: args{ + err: defaultErr, + msg: defaultMsg, + }, + want: want{ + want: Wrap(defaultErr, Errorf("panic recovered: %v", defaultMsg).Error()), + }, + } + }(), + func() test { + return test{ + name: "return an error when err is not nil and msg is empty", + args: args{ + err: defaultErr, + }, + want: want{ + want: Wrap(defaultErr, New("panic recovered: ").Error()), + }, + } + }(), + func() test { + return test{ + name: "return an error when err not nil and msg is not empty", + args: args{ + msg: defaultMsg, + }, + want: want{ + want: Wrap(nil, Errorf("panic recovered: %v", defaultMsg).Error()), + }, + } + }(), + } + for _, tc := range tests { + test := tc + t.Run(test.name, func(tt *testing.T) { + tt.Parallel() + defer goleak.VerifyNone(tt, goleak.IgnoreCurrent()) + if test.beforeFunc != nil { + test.beforeFunc(test.args) + } + if test.afterFunc != nil { + defer test.afterFunc(test.args) + } + if test.checkFunc == nil { + test.checkFunc = defaultCheckFunc + } + + got := ErrPanicString(test.args.err, test.args.msg) + if err := test.checkFunc(test.want, got); err != nil { + tt.Errorf("error = %v", err) + } + }) + } +} + +type runtimeError runtime.Error + +type runtimeErr struct { + err error +} + +func (e runtimeErr) RuntimeError() {} +func (e runtimeErr) Error() string { + return e.err.Error() +} + +func TestErrRuntimeError(t *testing.T) { + t.Parallel() + type args struct { + err error + r runtime.Error + } + type want struct { + want error + } + type test struct { + name string + args args + want want + checkFunc func(want, error) error + beforeFunc func(args) + afterFunc func(args) + } + defaultCheckFunc := func(w want, got error) error { + if !Is(got, w.want) { + return Errorf("got: \"%#v\",\n\t\t\t\twant: \"%#v\"", got, w.want) + } + return nil + } + defaultErr := New("runtime panic string error") + defaultRuntimeErr := &runtimeErr{ + err: New("runtime error is occured"), + } + tests := []test{ + func() test { + return test{ + name: "return an error when err is not nil and r is not empty", + args: args{ + err: defaultErr, + r: defaultRuntimeErr, + }, + want: want{ + want: Wrap(defaultErr, Errorf("system panicked caused by runtime error: %v", defaultRuntimeErr).Error()), + }, + } + }(), + func() test { + return test{ + name: "return an error when err is not nil and msg is nil", + args: args{ + err: defaultErr, + }, + want: want{ + want: Wrap(defaultErr, Errorf("system panicked caused by runtime error: %v", nil).Error()), + }, + } + }(), + func() test { + return test{ + name: "return an error when err is nil and msg is not empty", + args: args{ + r: defaultRuntimeErr, + }, + want: want{ + want: Wrap(nil, Errorf("system panicked caused by runtime error: %v", defaultRuntimeErr).Error()), + }, + } + }(), + } + for _, tc := range tests { + test := tc + t.Run(test.name, func(tt *testing.T) { + tt.Parallel() + defer goleak.VerifyNone(tt, goleak.IgnoreCurrent()) + if test.beforeFunc != nil { + test.beforeFunc(test.args) + } + if test.afterFunc != nil { + defer test.afterFunc(test.args) + } + if test.checkFunc == nil { + test.checkFunc = defaultCheckFunc + } + fmt.Println(test.args.r) + got := ErrRuntimeError(test.args.err, test.args.r) + if err := test.checkFunc(test.want, got); err != nil { + tt.Errorf("error = %v", err) + } + }) + } +} From b58d27afb352f7da88153e2671636f0d2d6a92ba Mon Sep 17 00:00:00 2001 From: vdaas-ci Date: Thu, 4 Feb 2021 11:19:02 +0000 Subject: [PATCH 2/8] :robot: Update license headers / Format go codes and yaml files Signed-off-by: vdaas-ci --- internal/errors/runtime_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/internal/errors/runtime_test.go b/internal/errors/runtime_test.go index e09701b002..cc9a8556ca 100644 --- a/internal/errors/runtime_test.go +++ b/internal/errors/runtime_test.go @@ -1,7 +1,7 @@ // // Copyright (C) 2019-2021 vdaas.org vald team // -// Licensed under the Apache LicensdefaultErr, Version 2.0 (the "License"); +// Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // From 883c6682617999e90d4bed4602f54964554bebff Mon Sep 17 00:00:00 2001 From: Kiichiro YUKAWA Date: Mon, 8 Feb 2021 10:00:29 +0900 Subject: [PATCH 3/8] Update internal/errors/runtime.go Co-authored-by: Hiroto Funakoshi --- internal/errors/runtime.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/internal/errors/runtime.go b/internal/errors/runtime.go index e8b3733c5b..147f402cd8 100644 --- a/internal/errors/runtime.go +++ b/internal/errors/runtime.go @@ -20,7 +20,7 @@ package errors import "runtime" var ( - // ErrPanicRecovered represents a function to generate an error that panic recovered. + // ErrPanicRecovered represents a function to generate an error that the panic recovered. ErrPanicRecovered = func(err error, rec interface{}) error { return Wrap(err, Errorf("panic recovered: %v", rec).Error()) } From 958a095ad2032e73c1e09233928926f01486606d Mon Sep 17 00:00:00 2001 From: Kiichiro YUKAWA Date: Mon, 8 Feb 2021 10:00:36 +0900 Subject: [PATCH 4/8] Update internal/errors/runtime.go Co-authored-by: Hiroto Funakoshi --- internal/errors/runtime.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/internal/errors/runtime.go b/internal/errors/runtime.go index 147f402cd8..d1f4c6a64f 100644 --- a/internal/errors/runtime.go +++ b/internal/errors/runtime.go @@ -30,7 +30,7 @@ var ( return Wrap(err, Errorf("panic recovered: %v", msg).Error()) } - // ErrRuntimeError represents a function to generate an error that panic caused by runtime error. + // ErrRuntimeError represents a function to generate an error that the panic caused by runtime error. ErrRuntimeError = func(err error, r runtime.Error) error { return Wrap(err, Errorf("system panicked caused by runtime error: %v", r).Error()) } From c482de6ac8570aee087ab23e65f7eadc3391a6a8 Mon Sep 17 00:00:00 2001 From: Kiichiro YUKAWA Date: Mon, 8 Feb 2021 10:00:44 +0900 Subject: [PATCH 5/8] Update internal/errors/runtime.go Co-authored-by: Hiroto Funakoshi --- internal/errors/runtime.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/internal/errors/runtime.go b/internal/errors/runtime.go index d1f4c6a64f..380006fb32 100644 --- a/internal/errors/runtime.go +++ b/internal/errors/runtime.go @@ -25,7 +25,7 @@ var ( return Wrap(err, Errorf("panic recovered: %v", rec).Error()) } - // ErrPanicString represents a function to generate an error that panic recovered with string message. + // ErrPanicString represents a function to generate an error that the panic recovered with a string message. ErrPanicString = func(err error, msg string) error { return Wrap(err, Errorf("panic recovered: %v", msg).Error()) } From 2f7c0a952d1de5024d2164598e50f5d737dabf7b Mon Sep 17 00:00:00 2001 From: Kiichiro YUKAWA Date: Mon, 8 Feb 2021 10:02:28 +0900 Subject: [PATCH 6/8] Update internal/errors/runtime_test.go --- internal/errors/runtime_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/internal/errors/runtime_test.go b/internal/errors/runtime_test.go index cc9a8556ca..bf4afecf8b 100644 --- a/internal/errors/runtime_test.go +++ b/internal/errors/runtime_test.go @@ -354,7 +354,7 @@ func TestErrRuntimeError(t *testing.T) { } defaultErr := New("runtime panic string error") defaultRuntimeErr := &runtimeErr{ - err: New("runtime error is occured"), + err: New("runtime error is occurred"), } tests := []test{ func() test { From 50ea9f01c829cc697d76134fbc379c48dc362dd7 Mon Sep 17 00:00:00 2001 From: vankichi Date: Mon, 8 Feb 2021 11:57:46 +0900 Subject: [PATCH 7/8] :white_check_mark: update runtime_test with feedback Signed-off-by: vankichi --- internal/errors/runtime_test.go | 25 ++++++++++++++++++++----- 1 file changed, 20 insertions(+), 5 deletions(-) diff --git a/internal/errors/runtime_test.go b/internal/errors/runtime_test.go index bf4afecf8b..aaf12a44b3 100644 --- a/internal/errors/runtime_test.go +++ b/internal/errors/runtime_test.go @@ -17,7 +17,6 @@ package errors import ( - "fmt" "math" "runtime" "testing" @@ -285,7 +284,7 @@ func TestErrPanicString(t *testing.T) { }(), func() test { return test{ - name: "return an error when err not nil and msg is not empty", + name: "return an error when err is nil and msg is not empty", args: args{ msg: defaultMsg, }, @@ -294,6 +293,15 @@ func TestErrPanicString(t *testing.T) { }, } }(), + func() test { + return test{ + name: "return an error when err is nil and msg is empty", + args: args{}, + want: want{ + want: Wrap(nil, New("panic recovered: ").Error()), + }, + } + }(), } for _, tc := range tests { test := tc @@ -318,8 +326,6 @@ func TestErrPanicString(t *testing.T) { } } -type runtimeError runtime.Error - type runtimeErr struct { err error } @@ -391,6 +397,15 @@ func TestErrRuntimeError(t *testing.T) { }, } }(), + func() test { + return test{ + name: "return an error when err is nil and msg is nil", + args: args{}, + want: want{ + want: Wrap(nil, Errorf("system panicked caused by runtime error: %v", nil).Error()), + }, + } + }(), } for _, tc := range tests { test := tc @@ -406,7 +421,7 @@ func TestErrRuntimeError(t *testing.T) { if test.checkFunc == nil { test.checkFunc = defaultCheckFunc } - fmt.Println(test.args.r) + got := ErrRuntimeError(test.args.err, test.args.r) if err := test.checkFunc(test.want, got); err != nil { tt.Errorf("error = %v", err) From 59273187cc9cda598b455b00806abbd8e5dc5ae8 Mon Sep 17 00:00:00 2001 From: Kiichiro YUKAWA Date: Mon, 8 Feb 2021 12:00:04 +0900 Subject: [PATCH 8/8] Update internal/errors/runtime_test.go Co-authored-by: Kevin Diu --- internal/errors/runtime_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/internal/errors/runtime_test.go b/internal/errors/runtime_test.go index aaf12a44b3..ea40d9a6af 100644 --- a/internal/errors/runtime_test.go +++ b/internal/errors/runtime_test.go @@ -167,7 +167,7 @@ func TestErrPanicRecovered(t *testing.T) { func() test { r := &defaultErr return test{ - name: "return an error when err is not empty and rec is boolean", + name: "return an error when err is not empty and rec is pointer", args: args{ err: defaultErr, rec: r,