Comments (6)
go版本升级不考虑向前兼容,不符合里氏替换原则,前段时间有反射机制,这几天又有内联禁用。
go1.10之前内联禁用命令:-gcflags=-l
go1.10及之后内联禁用命令:-gcflags=all=-l
from gomonkey.
应该换成: -gcflags "all=-N -l"
我本地是 go1.11 版本,测试结果是 -gcflags=-l 生效啊
from gomonkey.
我是g1.10, 这段代码就不行
-i -v -gcflags=-l
b/b.go:
package b
func F2() string {
return "F2"
}
func F() string {
return F2()
}
main.go
import (
"b"
"fmt"
"github.com/agiledragon/gomonkey"
)
func main() {
gomonkey.ApplyFunc(b.F2, func() string {
return "patched"
})
fmt.Printf("%v\n", b.F())
}
from gomonkey.
我是go1.11,上述情况-gcflags=-l
的确不生效。我之前是在测试代码中验证的,同时我们只在测试代码中使用 gomonkey 了,于是问题来了,为啥-gcflags=-l
在go test中生效?
示例代码:
func gg() int {
return mm()
}
func mm() int {
return 1
}
func TestMMApplyFunc(t *testing.T) {
Convey("TestMMApplyFunc", t, func() {
Convey("inline", func() {
patches := ApplyFunc(mm, func() int {
return 2
})
defer patches.Reset()
i := gg()
So(i, ShouldEqual, 2)
})
})
}
运行结果:
$ go version
go version go1.11 darwin/amd64
$ go test -gcflags=-l apply_func_test.go
ok command-line-arguments (cached)
$ go test -gcflags "all=-N -l" apply_func_test.go
ok command-line-arguments (cached)
from gomonkey.
我简单修改了@fmnisme main.go的代码:
package main
import (
"fmt"
"github.com/agiledragon/gomonkey"
"zte.com.cn/knitter/gomon/b"
)
func G2() string {
return "G2"
}
func G() string {
return G2()
}
func main() {
gomonkey.ApplyFunc(b.F2, func() string {
return "patchedF2"
})
gomonkey.ApplyFunc(G2, func() string {
return "patchedG2"
})
fmt.Printf("b.F: %v\n", b.F())
fmt.Printf("main.G: %v\n", G())
}
结果是:
[root@LIN-98C5F567B8E gomon]# go version
go version go1.11.1 linux/amd64
[root@LIN-98C5F567B8E gomon]# go run main.go
b.F: F2
main.G: G2
[root@LIN-98C5F567B8E gomon]# go run -gcflags "-N -l" main.go
b.F: F2
main.G: patchedG2
[root@LIN-98C5F567B8E gomon]# go run -gcflags "all=-N -l" main.go
b.F: patchedF2
main.G: patchedG2
[root@LIN-98C5F567B8E gomon]# go run -gcflags "all=-l" main.go
b.F: patchedF2
main.G: patchedG2
在结合golang对“all”的说明:
go help packages
.....
- "all" expands to all packages found in all the GOPATH
trees. For example, 'go list all' lists all the packages on the local
system. When using modules, "all" expands to all packages in
the main module and their dependencies, including dependencies
needed by tests of any of those.
......
我认为问题在于,F2函数在main之外的b包里,G2函数在main包内,这样仅使用“-N -l”时,golang编译器默认只对当前的顶层main包使用“-N -l”参数,而使用“all=-N -l”时,对b包也使用了“-N -l”参数,从而使对b包内的F函数禁用内联功能发挥出来。
from gomonkey.
@agiledragon 我改了测试代码:
b.go
package b
func F2() string {
return "F2"
}
func F() string {
return F2()
}
func GG() int {
return MM()
}
func MM() int {
return 1
}
main_test.go
package main
import (
"testing"
. "github.com/agiledragon/gomonkey"
"github.com/smartystreets/goconvey/convey"
"zte.com.cn/knitter/gomon/b"
)
func gg() int {
return mm()
}
func mm() int {
return 1
}
func TestMMApplyFunc(t *testing.T) {
convey.Convey("TestMMApplyFunc", t, func() {
convey.Convey("inline", func() {
patches := ApplyFunc(mm, func() int {
return 2
})
defer patches.Reset()
i := gg()
convey.So(i, convey.ShouldEqual, 2)
})
convey.Convey("inter packages inline", func() {
patches := ApplyFunc(b.MM, func() int {
return 2
})
defer patches.Reset()
i := b.GG()
convey.So(i, convey.ShouldEqual, 2)
})
})
}
[root@LIN-98C5F567B8E gomon]# go version
go version go1.11.1 linux/amd64
[root@LIN-98C5F567B8E gomon]# go test
xx
Failures:
* /media/B/code/paasnw/src/zte.com.cn/knitter/gomon/gomon_test.go
Line 27:
Expected: '2'
Actual: '1'
(Should be equal)
* /media/B/code/paasnw/src/zte.com.cn/knitter/gomon/gomon_test.go
Line 36:
Expected: '2'
Actual: '1'
(Should be equal)
2 total assertions
--- FAIL: TestMMApplyFunc (0.00s)
FAIL
exit status 1
FAIL zte.com.cn/knitter/gomon 0.002s
[root@LIN-98C5F567B8E gomon]# go test -gcflags "all=-N -l"
..
2 total assertions
PASS
ok zte.com.cn/knitter/gomon 0.002s
[root@LIN-98C5F567B8E gomon]# go test -gcflags "all=-l"
..
2 total assertions
PASS
ok zte.com.cn/knitter/gomon 0.002s
[root@LIN-98C5F567B8E gomon]# go test -gcflags "-l"
.x
Failures:
* /media/B/code/paasnw/src/zte.com.cn/knitter/gomon/gomon_test.go
Line 36:
Expected: '2'
Actual: '1'
(Should be equal)
2 total assertions
--- FAIL: TestMMApplyFunc (0.00s)
FAIL
exit status 1
FAIL zte.com.cn/knitter/gomon 0.002s
验证了我在上面对非测试的业务代码中做的判断,当跨包打桩时需要添加all参数。
from gomonkey.
Related Issues (20)
- please add ApplyPrivateMethodFunc and ApplyPrivateMethodReturn
- 是否有并发问题 HOT 4
- 是否有人用gomonkey来实现游戏服务器热更新?
- Why ApplyFunc mocked func returns the same result in a for loop? HOT 1
- MacOS 上的 permission denied 问题,也可能和 cgo 有关 HOT 8
- why patches.Reset not effective? HOT 1
- panic with SIGBUS HOT 7
- MacOS ARM64 兼容性问题 HOT 6
- macos m2 兼容性问题 HOT 6
- 打桩的方法内仅执行 return 语句,打桩没有生效 HOT 2
- panic: runtime error: index out of range [0] with length 0 [darwin/arm64] [M1] HOT 8
- how to mock func which as a param in another func HOT 1
- mac os m1 ApplyFuncReturn not work and unit test many panic HOT 5
- i solve it successfully in mac M1 by update new gomonkey version for permission deny error HOT 3
- 内联优化导致apply func失效 HOT 3
- gomonkey 中有异步携程时,mock的方法会失效 HOT 3
- Private method tests fail on Linux/Arm64 HOT 2
- Is it possible to add a `init` func to check whether `-gcflags=all=-l` is in `os.Args`? HOT 1
- gomonky 为泛型结构体的方法打桩不生效 HOT 2
- Running multiple cases at the same time and reporting errors: panic: double seq is less than call seq HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from gomonkey.