forked from megaease/easeprobe
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
052c266
commit f5dfdb7
Showing
51 changed files
with
215 additions
and
58 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
/* | ||
* Copyright (c) 2022, MegaEase | ||
* All rights reserved. | ||
* | ||
* 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 | ||
* | ||
* http://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 monkey | ||
|
||
import ( | ||
"reflect" | ||
|
||
"github.com/agiledragon/gomonkey/v2" | ||
) | ||
|
||
var patchesMap = make(map[string]*gomonkey.Patches) | ||
|
||
// Patch replaces a function with another | ||
func Patch(target, replacement interface{}) *gomonkey.Patches { | ||
key := reflect.TypeOf(target).String() | ||
existingPatches, ok := patchesMap[key] | ||
if ok { | ||
existingPatches.Reset() | ||
} | ||
patches := gomonkey.ApplyFunc(target, replacement) | ||
patchesMap[key] = patches | ||
return patches | ||
} | ||
|
||
// Unpatch unpatch a patch | ||
func Unpatch(target interface{}) bool { | ||
patches, ok := patchesMap[reflect.TypeOf(target).String()] | ||
if !ok { | ||
return false | ||
} | ||
patches.Reset() | ||
delete(patchesMap, reflect.TypeOf(target).String()) | ||
return true | ||
} | ||
|
||
// PatchInstanceMethod replaces an instance method methodName for the type target with replacement | ||
func PatchInstanceMethod(target reflect.Type, methodName string, replacement interface{}) *gomonkey.Patches { | ||
key := target.String() + methodName | ||
existingPatches, ok := patchesMap[key] | ||
if ok { | ||
existingPatches.Reset() | ||
} | ||
patches := gomonkey.ApplyMethod(target, methodName, replacement) | ||
patchesMap[key] = patches | ||
return patches | ||
} | ||
|
||
// UnpatchInstanceMethod unpatch a patch | ||
func UnpatchInstanceMethod(target reflect.Type, methodName string) bool { | ||
patches, ok := patchesMap[target.String()+methodName] | ||
if !ok { | ||
return false | ||
} | ||
patches.Reset() | ||
delete(patchesMap, target.String()+methodName) | ||
return true | ||
} | ||
|
||
// UnpatchAll unpatch all patches | ||
func UnpatchAll() { | ||
for _, patches := range patchesMap { | ||
patches.Reset() | ||
} | ||
patchesMap = make(map[string]*gomonkey.Patches) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
/* | ||
* Copyright (c) 2022, MegaEase | ||
* All rights reserved. | ||
* | ||
* 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 | ||
* | ||
* http://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 monkey | ||
|
||
import ( | ||
"reflect" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
type MyStruct struct{} | ||
|
||
func (s *MyStruct) Method() string { | ||
return "original" | ||
} | ||
|
||
func TestPatch(t *testing.T) { | ||
originalFunc := func() string { return "original" } | ||
replacementFunc := func() string { return "replacement" } | ||
|
||
patch := Patch(originalFunc, replacementFunc) | ||
defer patch.Reset() | ||
|
||
assert.Equal(t, "replacement", originalFunc()) | ||
} | ||
|
||
func TestUnpatch(t *testing.T) { | ||
originalFunc := func() string { return "original" } | ||
replacementFunc := func() string { return "replacement" } | ||
|
||
Patch(originalFunc, replacementFunc) | ||
Unpatch(originalFunc) | ||
|
||
assert.Equal(t, "original", originalFunc()) | ||
} | ||
|
||
func TestPatchInstanceMethod(t *testing.T) { | ||
PatchInstanceMethod(reflect.TypeOf(&MyStruct{}), "Method", func(*MyStruct) string { return "replacement" }) | ||
defer UnpatchInstanceMethod(reflect.TypeOf(&MyStruct{}), "Method") | ||
|
||
assert.Equal(t, "replacement", (&MyStruct{}).Method()) | ||
} | ||
|
||
func TestUnpatchInstanceMethod(t *testing.T) { | ||
PatchInstanceMethod(reflect.TypeOf(&MyStruct{}), "Method", func(*MyStruct) string { return "replacement" }) | ||
UnpatchInstanceMethod(reflect.TypeOf(&MyStruct{}), "Method") | ||
|
||
assert.Equal(t, "original", (&MyStruct{}).Method()) | ||
} | ||
|
||
func TestUnpatchAll(t *testing.T) { | ||
originalFunc := func() string { return "original" } | ||
replacementFunc := func() string { return "replacement" } | ||
|
||
Patch(originalFunc, replacementFunc) | ||
PatchInstanceMethod(reflect.TypeOf(&MyStruct{}), "Method", func(*MyStruct) string { return "replacement" }) | ||
UnpatchAll() | ||
|
||
assert.Equal(t, "original", originalFunc()) | ||
assert.Equal(t, "original", (&MyStruct{}).Method()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.