From 390a1bf87ed96fe297988c9a913dc5958d8f91c8 Mon Sep 17 00:00:00 2001 From: samanhappy Date: Mon, 12 Aug 2024 17:00:12 +0800 Subject: [PATCH] Revert "fix PatchInstanceMethod" This reverts commit 2f94d77a406faa9a2fdca856e60ee76849ec741e. --- monkey/monkey.go | 5 +++-- notify/lark/lark_test.go | 11 ++++++----- 2 files changed, 9 insertions(+), 7 deletions(-) diff --git a/monkey/monkey.go b/monkey/monkey.go index 15e463ee..74f9c726 100644 --- a/monkey/monkey.go +++ b/monkey/monkey.go @@ -19,6 +19,7 @@ package monkey import ( "fmt" + "reflect" "github.com/agiledragon/gomonkey/v2" ) @@ -50,7 +51,7 @@ func Unpatch(target interface{}) bool { } // PatchInstanceMethod replaces an instance method methodName for the type target with replacement -func PatchInstanceMethod(target interface{}, methodName string, replacement interface{}) *gomonkey.Patches { +func PatchInstanceMethod(target reflect.Type, methodName string, replacement interface{}) *gomonkey.Patches { key := fmt.Sprintf("%v:%v", target, methodName) existingPatches, ok := patchesMap[key] if ok { @@ -62,7 +63,7 @@ func PatchInstanceMethod(target interface{}, methodName string, replacement inte } // UnpatchInstanceMethod unpatch a patch -func UnpatchInstanceMethod(target interface{}, methodName string) bool { +func UnpatchInstanceMethod(target reflect.Type, methodName string) bool { key := fmt.Sprintf("%v:%v", target, methodName) patches, ok := patchesMap[key] if !ok { diff --git a/notify/lark/lark_test.go b/notify/lark/lark_test.go index e9226535..27e49355 100644 --- a/notify/lark/lark_test.go +++ b/notify/lark/lark_test.go @@ -21,6 +21,7 @@ import ( "errors" "io" "net/http" + "reflect" "strings" "testing" @@ -43,7 +44,7 @@ func TestLark(t *testing.T) { assert.Equal(t, "lark", conf.Kind()) var client *http.Client - monkey.PatchInstanceMethod(client, "Do", func(_ *http.Client, req *http.Request) (*http.Response, error) { + monkey.PatchInstanceMethod(reflect.TypeOf(client), "Do", func(_ *http.Client, req *http.Request) (*http.Response, error) { r := io.NopCloser(strings.NewReader(`{"StatusCode": 0}`)) return &http.Response{ StatusCode: 200, @@ -54,7 +55,7 @@ func TestLark(t *testing.T) { assert.NoError(t, err) // bad response - monkey.PatchInstanceMethod(client, "Do", func(_ *http.Client, req *http.Request) (*http.Response, error) { + monkey.PatchInstanceMethod(reflect.TypeOf(client), "Do", func(_ *http.Client, req *http.Request) (*http.Response, error) { r := io.NopCloser(strings.NewReader(`{"StatusCode": "100"}`)) return &http.Response{ StatusCode: 200, @@ -66,7 +67,7 @@ func TestLark(t *testing.T) { assert.Contains(t, err.Error(), "Error response from Lark") assert.Contains(t, err.Error(), "code [0] - msg []") - monkey.PatchInstanceMethod(client, "Do", func(_ *http.Client, req *http.Request) (*http.Response, error) { + monkey.PatchInstanceMethod(reflect.TypeOf(client), "Do", func(_ *http.Client, req *http.Request) (*http.Response, error) { r := io.NopCloser(strings.NewReader(`{"StatusCode": "100", "code": 10, "msg": "lark error"}`)) return &http.Response{ StatusCode: 200, @@ -78,7 +79,7 @@ func TestLark(t *testing.T) { assert.Contains(t, err.Error(), "Error response from Lark") assert.Contains(t, err.Error(), "code [10] - msg [lark error]") - monkey.PatchInstanceMethod(client, "Do", func(_ *http.Client, req *http.Request) (*http.Response, error) { + monkey.PatchInstanceMethod(reflect.TypeOf(client), "Do", func(_ *http.Client, req *http.Request) (*http.Response, error) { r := io.NopCloser(strings.NewReader(`bad : json format`)) return &http.Response{ StatusCode: 200, @@ -94,7 +95,7 @@ func TestLark(t *testing.T) { err = conf.SendLark("title", "message") assertError(t, err, "read error") - monkey.PatchInstanceMethod(client, "Do", func(_ *http.Client, req *http.Request) (*http.Response, error) { + monkey.PatchInstanceMethod(reflect.TypeOf(client), "Do", func(_ *http.Client, req *http.Request) (*http.Response, error) { return nil, errors.New("http error") }) err = conf.SendLark("title", "message")