From 970e13d37e51fa9700514618e07024e692710e78 Mon Sep 17 00:00:00 2001 From: Daniel Redondo Date: Fri, 7 Feb 2020 00:05:33 +0100 Subject: [PATCH 1/3] Fix support for import autoinstrument and TestMain scopeagent.Run call without creating agents twice. --- init.go | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/init.go b/init.go index 930d4abc..a1eaa58c 100644 --- a/init.go +++ b/init.go @@ -6,6 +6,7 @@ import ( "os" "os/signal" "runtime" + "sync" "syscall" "testing" @@ -15,10 +16,19 @@ import ( scopetesting "go.undefinedlabs.com/scopeagent/instrumentation/testing" ) -var defaultAgent *agent.Agent +var ( + defaultAgent *agent.Agent + runningMutex sync.Mutex + running bool +) // Helper function to run a `testing.M` object and gracefully stopping the agent afterwards func Run(m *testing.M, opts ...agent.Option) int { + if getRunningFlag() { + return m.Run() + } + setRunningFlag(true) + defer setRunningFlag(false) opts = append(opts, agent.WithTestingModeEnabled()) newAgent, err := agent.NewAgent(opts...) if err != nil { @@ -130,3 +140,14 @@ func StartBenchmark(b *testing.B, benchFunc func(b *testing.B)) { pc, _, _, _ := runtime.Caller(1) scopetesting.StartBenchmark(b, pc, benchFunc) } + +func setRunningFlag(value bool) { + runningMutex.Lock() + defer runningMutex.Unlock() + running = value +} +func getRunningFlag() bool { + runningMutex.Lock() + defer runningMutex.Unlock() + return running +} From c71df95922c2cb07c2c7b3a70d9ed1a919d13dc5 Mon Sep 17 00:00:00 2001 From: Daniel Redondo Date: Mon, 10 Feb 2020 14:32:18 +0100 Subject: [PATCH 2/3] restore go.mod --- go.mod | 3 +++ 1 file changed, 3 insertions(+) diff --git a/go.mod b/go.mod index 770c885d..e2c3541a 100644 --- a/go.mod +++ b/go.mod @@ -15,10 +15,13 @@ require ( github.com/stretchr/testify v1.4.0 github.com/undefinedlabs/go-mpatch v0.0.0-20200122175732-0044123dbb98 github.com/vmihailenco/msgpack v4.0.4+incompatible + golang.org/x/lint v0.0.0-20190313153728-d0100b6bd8b3 // indirect golang.org/x/net v0.0.0-20200114155413-6afb5195e5aa golang.org/x/sys v0.0.0-20200122134326-e047566fdf82 // indirect + golang.org/x/tools v0.0.0-20190524140312-2c0ae7006135 // indirect google.golang.org/appengine v1.6.5 // indirect google.golang.org/grpc v1.27.1 gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 // indirect gopkg.in/tomb.v2 v2.0.0-20161208151619-d5d1b5820637 + honnef.co/go/tools v0.0.0-20190523083050-ea95bdfd59fc // indirect ) From df8e52f0fca5a5b58bc1da7698fadbc1c311fdeb Mon Sep 17 00:00:00 2001 From: Daniel Redondo Date: Wed, 12 Feb 2020 14:11:52 +0100 Subject: [PATCH 3/3] lock type change --- go.mod | 3 --- init.go | 6 +++--- 2 files changed, 3 insertions(+), 6 deletions(-) diff --git a/go.mod b/go.mod index e2c3541a..770c885d 100644 --- a/go.mod +++ b/go.mod @@ -15,13 +15,10 @@ require ( github.com/stretchr/testify v1.4.0 github.com/undefinedlabs/go-mpatch v0.0.0-20200122175732-0044123dbb98 github.com/vmihailenco/msgpack v4.0.4+incompatible - golang.org/x/lint v0.0.0-20190313153728-d0100b6bd8b3 // indirect golang.org/x/net v0.0.0-20200114155413-6afb5195e5aa golang.org/x/sys v0.0.0-20200122134326-e047566fdf82 // indirect - golang.org/x/tools v0.0.0-20190524140312-2c0ae7006135 // indirect google.golang.org/appengine v1.6.5 // indirect google.golang.org/grpc v1.27.1 gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 // indirect gopkg.in/tomb.v2 v2.0.0-20161208151619-d5d1b5820637 - honnef.co/go/tools v0.0.0-20190523083050-ea95bdfd59fc // indirect ) diff --git a/init.go b/init.go index a1eaa58c..fb3ae83d 100644 --- a/init.go +++ b/init.go @@ -18,7 +18,7 @@ import ( var ( defaultAgent *agent.Agent - runningMutex sync.Mutex + runningMutex sync.RWMutex running bool ) @@ -147,7 +147,7 @@ func setRunningFlag(value bool) { running = value } func getRunningFlag() bool { - runningMutex.Lock() - defer runningMutex.Unlock() + runningMutex.RLock() + defer runningMutex.RUnlock() return running }