2020-06-08 03:29:51 +03:00
|
|
|
package recoverfn_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"reflect"
|
|
|
|
"testing"
|
2021-08-17 13:06:32 +03:00
|
|
|
|
|
|
|
"github.com/wader/fq/internal/recoverfn"
|
2020-06-08 03:29:51 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
func test1() {
|
2023-03-31 13:29:11 +03:00
|
|
|
panic(testError(true))
|
2020-06-08 03:29:51 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func test2() {
|
|
|
|
test1()
|
|
|
|
}
|
|
|
|
|
2023-03-31 13:29:11 +03:00
|
|
|
type testError bool
|
|
|
|
|
|
|
|
func (t testError) IsRecoverableError() bool { return bool(t) }
|
|
|
|
|
2020-06-08 03:29:51 +03:00
|
|
|
func TestNormal(t *testing.T) {
|
|
|
|
_, rOk := recoverfn.Run(func() {})
|
|
|
|
expectedROK := true
|
|
|
|
if !reflect.DeepEqual(expectedROK, rOk) {
|
|
|
|
t.Errorf("expected v %v, got %v", expectedROK, rOk)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPanic(t *testing.T) {
|
|
|
|
r, rOk := recoverfn.Run(test2)
|
|
|
|
|
|
|
|
expectedROK := false
|
|
|
|
if !reflect.DeepEqual(expectedROK, rOk) {
|
|
|
|
t.Errorf("expected v %v, got %v", expectedROK, rOk)
|
|
|
|
}
|
|
|
|
|
2023-03-31 13:29:11 +03:00
|
|
|
if _, ok := r.RecoverV.(testError); !ok {
|
|
|
|
t.Errorf("expected v %v, got %v", testError(true), r.RecoverV)
|
2020-06-08 03:29:51 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
frames := r.Frames()
|
|
|
|
|
|
|
|
expectedFramesLen := 2
|
|
|
|
actualFramesLen := len(frames)
|
|
|
|
if !reflect.DeepEqual(expectedFramesLen, actualFramesLen) {
|
|
|
|
t.Errorf("expected len(frames) %v, got %v", actualFramesLen, actualFramesLen)
|
|
|
|
}
|
|
|
|
|
2021-08-17 13:06:32 +03:00
|
|
|
expectedFrame0Function := "github.com/wader/fq/internal/recoverfn_test.test1"
|
2020-06-08 03:29:51 +03:00
|
|
|
actualFrame0Function := frames[0].Function
|
|
|
|
if !reflect.DeepEqual(expectedFrame0Function, actualFrame0Function) {
|
|
|
|
t.Errorf("expected frames[0].Function %v, got %v", expectedFrame0Function, actualFrame0Function)
|
|
|
|
}
|
|
|
|
|
2021-08-17 13:06:32 +03:00
|
|
|
expectedFrame1Function := "github.com/wader/fq/internal/recoverfn_test.test2"
|
2020-06-08 03:29:51 +03:00
|
|
|
actualFrame1Function := frames[1].Function
|
|
|
|
if !reflect.DeepEqual(expectedFrame1Function, actualFrame1Function) {
|
|
|
|
t.Errorf("expected frames[1].Function %v, got %v", expectedFrame1Function, actualFrame1Function)
|
|
|
|
}
|
|
|
|
}
|