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

Commit

Permalink
sub test and sub benchmark auto instrumentation
Browse files Browse the repository at this point in the history
  • Loading branch information
tonyredondo committed Feb 6, 2020
1 parent a672da8 commit 0f7eab7
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 14 deletions.
3 changes: 2 additions & 1 deletion instrumentation/testing/go_benchmark.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
The purpose with this file is to clone the struct alignment of the testing.B struct so we can assign a *testing.B
pointer to the *goB to have access to the internal private fields.
We use this to create a Run clone method to be called from the subtest auto instrumentation
We use this to create a Run clone method to be called from the sub benchmark auto instrumentation (because the original
method is replaced with the Patch)
*/

package testing
Expand Down
16 changes: 6 additions & 10 deletions instrumentation/testing/go_testing.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
The purpose with this file is to clone the struct alignment of the testing.T struct so we can assign a *testing.T
pointer to the *goT to have access to the internal private fields.
We use this to create a Run clone method to be called from the subtest auto instrumentation
We use this to create a Run clone method to be called from the sub test auto instrumentation (because the original
method is replaced with the Patch)
*/
package testing

Expand Down Expand Up @@ -61,8 +62,8 @@ const maxStackLen = 50
//go:linkname matchMutex testing.matchMutex
var matchMutex sync.Mutex

//go:linkname goTRunner testing.tRunner
func goTRunner(t *testing.T, fn func(t *testing.T))
//go:linkname tRunner testing.tRunner
func tRunner(t *testing.T, fn func(t *testing.T))

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

// this method calls the original testing.tRunner by converting *goT to *testing.T
func tRunner(t *goT, fn func(t *goT)) {
goTRunner(t.ToTestingT(), func(t *testing.T) { fn(FromTestingT(t)) })
}

// we clone the same (*testing.T).Run implementation because the Patch
// overwrites the original implementation with the jump
func (t *goT) Run(name string, f func(t *goT)) bool {
func (t *goT) Run(name string, f func(t *testing.T)) bool {
atomic.StoreInt32(&t.hasSub, 1)
testName, ok, _ := t.context.match.fullName(&t.goCommon, name)
if !ok || shouldFailFast() {
Expand Down Expand Up @@ -110,7 +106,7 @@ func (t *goT) Run(name string, f func(t *goT)) bool {
fmt.Fprintf(root.w, "=== RUN %s\n", t.name)
root.mu.Unlock()
}
go tRunner(t, f)
go tRunner(t.ToTestingT(), f)
if !<-t.signal {
runtime.Goexit()
}
Expand Down
7 changes: 4 additions & 3 deletions instrumentation/testing/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,9 @@ func Init(m *testing.M) {
var err error
runPatch, err = mpatch.PatchMethodByReflect(mRunMethod, func(m *testing.M) int {
logOnError(runPatch.Unpatch())
defer func() { logOnError(runPatch.Patch()) }()
defer func() {
logOnError(runPatch.Patch())
}()
PatchTestingLogger()
defer UnpatchTestingLogger()
return m.Run()
Expand All @@ -75,8 +77,7 @@ func Init(m *testing.M) {
_, err := mpatch.PatchMethodByReflect(tRunMethod, func(t *testing.T, name string, f func(t *testing.T)) bool {
pc, _, _, _ := runtime.Caller(1)
gT := FromTestingT(t)
return gT.Run(name, func(childGoT *goT) {
childT := childGoT.ToTestingT()
return gT.Run(name, func(childT *testing.T) {
addAutoInstrumentedTest(childT)
childTest := StartTestFromCaller(childT, pc)
defer childTest.end()
Expand Down

0 comments on commit 0f7eab7

Please sign in to comment.