11package testing
22
33import (
4- "os"
54 "reflect"
65 "runtime"
76 "testing"
@@ -11,6 +10,11 @@ import (
1110 "go.undefinedlabs.com/scopeagent/reflection"
1211)
1312
13+ var (
14+ tRunPatched bool
15+ bRunPatched bool
16+ )
17+
1418// Initialize the testing instrumentation
1519func Init (m * testing.M ) {
1620 if tPointer , err := reflection .GetFieldPointerOf (m , "tests" ); err == nil {
@@ -41,7 +45,7 @@ func Init(m *testing.M) {
4145 benchmarks = append (benchmarks , testing.InternalBenchmark {
4246 Name : benchmark .Name ,
4347 F : func (b * testing.B ) { // Indirection of the original benchmark
44- if envDMPatch , set := os . LookupEnv ( "SCOPE_DISABLE_MONKEY_PATCHING" ); ! set || envDMPatch == "" {
48+ if bRunPatched {
4549 funcValue (b )
4650 } else {
4751 startBenchmark (b , funcPointer , funcValue )
@@ -51,36 +55,50 @@ func Init(m *testing.M) {
5155 }
5256 * intBenchmarks = benchmarks
5357 }
58+ }
5459
55- if envDMPatch , set := os .LookupEnv ("SCOPE_DISABLE_MONKEY_PATCHING" ); ! set || envDMPatch == "" {
56- // We monkey patch the `testing.T.Run()` func to auto instrument sub tests
57- var t * testing.T
58- tType := reflect .TypeOf (t )
59- if tRunMethod , ok := tType .MethodByName ("Run" ); ok {
60- _ , err := mpatch .PatchMethodByReflect (tRunMethod , func (t * testing.T , name string , f func (t * testing.T )) bool {
61- pc , _ , _ , _ := runtime .Caller (1 )
62- gT := FromTestingT (t )
63- return gT .Run (name , func (childT * testing.T ) {
64- addAutoInstrumentedTest (childT )
65- childTest := StartTestFromCaller (childT , pc )
66- defer childTest .end ()
67- f (childT )
68- })
69- })
70- logOnError (err )
71- }
60+ func PatchTRun () {
61+ // We monkey patch the `testing.T.Run()` func to auto instrument sub tests
62+ var t * testing.T
63+ var tRunMethod reflect.Method
64+ var ok bool
65+ tType := reflect .TypeOf (t )
66+ if tRunMethod , ok = tType .MethodByName ("Run" ); ! ok {
67+ return
68+ }
7269
73- // We monkey patch the `testing.B.Run()` func to auto instrument sub benchmark
74- var b * testing.B
75- bType := reflect .TypeOf (b )
76- if bRunMethod , ok := bType .MethodByName ("Run" ); ok {
77- _ , err := mpatch .PatchMethodByReflect (bRunMethod , func (b * testing.B , name string , f func (b * testing.B )) bool {
78- pc , _ , _ , _ := runtime .Caller (1 )
79- return FromTestingB (b ).Run (name , func (b * testing.B ) {
80- StartBenchmark (b , pc , f )
81- })
82- })
83- logOnError (err )
84- }
70+ _ , err := mpatch .PatchMethodByReflect (tRunMethod , func (t * testing.T , name string , f func (t * testing.T )) bool {
71+ pc , _ , _ , _ := runtime .Caller (1 )
72+ gT := FromTestingT (t )
73+ return gT .Run (name , func (childT * testing.T ) {
74+ addAutoInstrumentedTest (childT )
75+ childTest := StartTestFromCaller (childT , pc )
76+ defer childTest .end ()
77+ f (childT )
78+ })
79+ })
80+ if ! logOnError (err ) {
81+ tRunPatched = true
82+ }
83+ }
84+
85+ func PatchBRun () {
86+ // We monkey patch the `testing.B.Run()` func to auto instrument sub benchmark
87+ var b * testing.B
88+ var bRunMethod reflect.Method
89+ var ok bool
90+ bType := reflect .TypeOf (b )
91+ if bRunMethod , ok = bType .MethodByName ("Run" ); ! ok {
92+ return
93+ }
94+
95+ _ , err := mpatch .PatchMethodByReflect (bRunMethod , func (b * testing.B , name string , f func (b * testing.B )) bool {
96+ pc , _ , _ , _ := runtime .Caller (1 )
97+ return FromTestingB (b ).Run (name , func (b * testing.B ) {
98+ StartBenchmark (b , pc , f )
99+ })
100+ })
101+ if ! logOnError (err ) {
102+ bRunPatched = true
85103 }
86104}
0 commit comments