原文: https://www.jianshu.com/p/19089baf79ca


a.go
package main
import (
"fmt"
"bou.ke/monkey"
)
func foo1(s string) string {
return fmt.Sprintf("i am %s, calling function foo1", s)
}
func foo2(s string) string {
return fmt.Sprintf("i am %s, calling function foo2", s)
}
func main() {
monkey.Patch(foo1, foo2)
fmt.Println(foo1("apple")) //输出:i am apple, calling function foo2
}
------------------
背景:项目进行 mock 操作,选择了 monkey patch 的方式。
在 github 上找了一个开源项目 https://github.com/bouk/monkey
使用如下:
下载: go get github.com/bouk/monkey
package github.com/bouk/monkey: code in directory /xxxx/src/github.com/bouk/monkey expects import "bou.ke/monkey"
查看它的源码:github.com/bouk/monkey/monkey.go 第一行显示 package monkey // import "bou.ke/monkey"
正常的 import 方式为 import "github.com/bouk/monkey", 源码指定了 import 方式,
因此需要:mv github.com/bouk bou.ke
下载完后使用结果:
func foo1(s string) string { return fmt.Sprintf("i am %s, calling function foo1", s) } func foo2(s string) string { return fmt.Sprintf("i am %s, calling function foo2", s) } // result fmt.Println(foo1("apple")) // 输出:i am apple, calling function foo1 fmt.Println(foo2("pear")) // 输出:i am pear, calling function foo2
结果是符合预期的,场景是有时候我需要对函数 mock 操作,进行测试(例如 mock 掉数据库操作),代码如下:
func foo1(s string) string { return fmt.Sprintf("i am %s, calling function foo1", s) } func foo2(s string) string { return fmt.Sprintf("i am %s, calling function foo2", s) } monkey.Patch(foo1, foo2) fmt.Println(foo1("apple")) //输出:i am apple, calling function foo2
这也是符合预期的,因为 foo1 函数被 foo2 给替换了。
下面看另外的例子:
func foo3() bool { return true } func foo4() bool { return false } monkey.Patch(foo3, foo4) fmt.Println(foo3()) //实际输出:true
问题是我们期待的结果是应该是输出 false,因为 foo3 进行了 monkey patch 操作。看一下说明:
Monkey sometimes fails to patch a function if inlining is enabled. Try running your tests with inlining disabled, for example: go test -gcflags=-l. The same command line argument can also be used for build.
大概原因就是此 monkey patch 方式对 inline function 无效,因此需要 disable inlining ,添加参数 -gcflags=-l, 因为上述的例子执行 go run -gcflags=-l main.go 得到符合预期的和正确的输出结果 false。
上面是使用 https://github.com/bouk/monkey 的一些心得。
另: 开源项目的原作者实现的原理: https://bou.ke/blog/monkey-patching-in-go/
作者:七秒钟回忆待续
链接:https://www.jianshu.com/p/19089baf79ca
来源:简书
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
