Skip to content
This repository was archived by the owner on Aug 17, 2020. It is now read-only.

Commit 0f7eab7

Browse files
committed
sub test and sub benchmark auto instrumentation
1 parent a672da8 commit 0f7eab7

File tree

3 files changed

+12
-14
lines changed

3 files changed

+12
-14
lines changed

instrumentation/testing/go_benchmark.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22
The purpose with this file is to clone the struct alignment of the testing.B struct so we can assign a *testing.B
33
pointer to the *goB to have access to the internal private fields.
44
5-
We use this to create a Run clone method to be called from the subtest auto instrumentation
5+
We use this to create a Run clone method to be called from the sub benchmark auto instrumentation (because the original
6+
method is replaced with the Patch)
67
*/
78

89
package testing

instrumentation/testing/go_testing.go

+6-10
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22
The purpose with this file is to clone the struct alignment of the testing.T struct so we can assign a *testing.T
33
pointer to the *goT to have access to the internal private fields.
44
5-
We use this to create a Run clone method to be called from the subtest auto instrumentation
5+
We use this to create a Run clone method to be called from the sub test auto instrumentation (because the original
6+
method is replaced with the Patch)
67
*/
78
package testing
89

@@ -61,8 +62,8 @@ const maxStackLen = 50
6162
//go:linkname matchMutex testing.matchMutex
6263
var matchMutex sync.Mutex
6364

64-
//go:linkname goTRunner testing.tRunner
65-
func goTRunner(t *testing.T, fn func(t *testing.T))
65+
//go:linkname tRunner testing.tRunner
66+
func tRunner(t *testing.T, fn func(t *testing.T))
6667

6768
//go:linkname rewrite testing.rewrite
6869
func rewrite(s string) string
@@ -73,14 +74,9 @@ func shouldFailFast() bool
7374
//go:linkname (*goMatcher).fullName testing.(*matcher).fullName
7475
func (m *goMatcher) fullName(c *goCommon, subname string) (name string, ok, partial bool)
7576

76-
// this method calls the original testing.tRunner by converting *goT to *testing.T
77-
func tRunner(t *goT, fn func(t *goT)) {
78-
goTRunner(t.ToTestingT(), func(t *testing.T) { fn(FromTestingT(t)) })
79-
}
80-
8177
// we clone the same (*testing.T).Run implementation because the Patch
8278
// overwrites the original implementation with the jump
83-
func (t *goT) Run(name string, f func(t *goT)) bool {
79+
func (t *goT) Run(name string, f func(t *testing.T)) bool {
8480
atomic.StoreInt32(&t.hasSub, 1)
8581
testName, ok, _ := t.context.match.fullName(&t.goCommon, name)
8682
if !ok || shouldFailFast() {
@@ -110,7 +106,7 @@ func (t *goT) Run(name string, f func(t *goT)) bool {
110106
fmt.Fprintf(root.w, "=== RUN %s\n", t.name)
111107
root.mu.Unlock()
112108
}
113-
go tRunner(t, f)
109+
go tRunner(t.ToTestingT(), f)
114110
if !<-t.signal {
115111
runtime.Goexit()
116112
}

instrumentation/testing/init.go

+4-3
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,9 @@ func Init(m *testing.M) {
6060
var err error
6161
runPatch, err = mpatch.PatchMethodByReflect(mRunMethod, func(m *testing.M) int {
6262
logOnError(runPatch.Unpatch())
63-
defer func() { logOnError(runPatch.Patch()) }()
63+
defer func() {
64+
logOnError(runPatch.Patch())
65+
}()
6466
PatchTestingLogger()
6567
defer UnpatchTestingLogger()
6668
return m.Run()
@@ -75,8 +77,7 @@ func Init(m *testing.M) {
7577
_, err := mpatch.PatchMethodByReflect(tRunMethod, func(t *testing.T, name string, f func(t *testing.T)) bool {
7678
pc, _, _, _ := runtime.Caller(1)
7779
gT := FromTestingT(t)
78-
return gT.Run(name, func(childGoT *goT) {
79-
childT := childGoT.ToTestingT()
80+
return gT.Run(name, func(childT *testing.T) {
8081
addAutoInstrumentedTest(childT)
8182
childTest := StartTestFromCaller(childT, pc)
8283
defer childTest.end()

0 commit comments

Comments
 (0)