Skip to content

Commit

Permalink
wait for profiler to stop
Browse files Browse the repository at this point in the history
  • Loading branch information
vaind committed Aug 15, 2023
1 parent 2aab77b commit c5f1055
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 12 deletions.
13 changes: 8 additions & 5 deletions profiler.go
Expand Up @@ -70,7 +70,7 @@ const profilerRuntimeLimit = 30 // seconds
type profileRecorder struct {
startTime time.Time
stopSignal chan struct{}
stopped sync.WaitGroup
stopped int64
mutex sync.RWMutex
testProfilerPanic int64

Expand Down Expand Up @@ -133,8 +133,10 @@ func (p *profileRecorder) run(started chan struct{}) {
Logger.Printf("Profiler panic in run(): %v\n", err)
}
atomic.StoreInt64(&testProfilerPanic, 0)
atomic.StoreInt64(&profilerRunning, 0)
close(started)
p.stopSignal <- struct{}{}
atomic.StoreInt64(&p.stopped, 1)
atomic.StoreInt64(&profilerRunning, 0)
}()

p.testProfilerPanic = atomic.LoadInt64(&testProfilerPanic)
Expand All @@ -159,17 +161,18 @@ func (p *profileRecorder) run(started chan struct{}) {
p.onTick()
collectTicker.Ticked()
case <-p.stopSignal:
p.stopped.Done()
return
}
}
}

func (p *profileRecorder) Stop(wait bool) {
p.stopped.Add(1)
if atomic.LoadInt64(&p.stopped) == 1 {
return
}
p.stopSignal <- struct{}{}
if wait {
p.stopped.Wait()
<-p.stopSignal
}
}

Expand Down
14 changes: 7 additions & 7 deletions profiler_test.go
Expand Up @@ -35,7 +35,7 @@ func (t *profilerTestTicker) Ticked() {
}

func (t *profilerTestTicker) Stop() {
t.log("Ticker: stopped.\n")
t.log("Ticker: stopping.\n")
close(t.ticked)
}

Expand All @@ -48,7 +48,7 @@ func (t *profilerTestTicker) Tick() bool {
select {
case _, ok := <-t.ticked:
if ok {
t.log("Ticker: tick acknowledged by the profiler.\n") // logged on the test goroutine
t.log("Ticker: tick acknowledged (received on the test goroutine).\n") // logged on the test goroutine
return true
}
t.log("Ticker: tick not acknowledged (ticker stopped).\n")
Expand Down Expand Up @@ -83,7 +83,7 @@ func TestProfilerCollection(t *testing.T) {

start := time.Now()
profiler := startProfiling(start)
defer profiler.Stop(false)
defer profiler.Stop(true)
if isCI() {
doWorkFor(5 * time.Second)
} else {
Expand All @@ -106,7 +106,7 @@ func TestProfilerCollection(t *testing.T) {

start := time.Now()
profiler := startProfiling(start)
defer profiler.Stop(false)
defer profiler.Stop(true)
require.True(ticker.Tick())
end := time.Now()
result := profiler.GetSlice(start, end)
Expand Down Expand Up @@ -134,7 +134,7 @@ func TestProfilerStackTrace(t *testing.T) {

start := time.Now()
profiler := startProfiling(start)
defer profiler.Stop(false)
defer profiler.Stop(true)
require.True(ticker.Tick())
result := profiler.GetSlice(start, time.Now())
require.NotNil(result)
Expand Down Expand Up @@ -203,7 +203,7 @@ func TestProfilerPanicOnTick(t *testing.T) {

start := time.Now()
profiler := startProfiling(start)
defer profiler.Stop(false)
defer profiler.Stop(true)
assert.True(ticker.Tick())
assert.False(ticker.Tick())

Expand Down Expand Up @@ -307,7 +307,7 @@ func TestProfilerSamplingRate(t *testing.T) {

start := time.Now()
profiler := startProfiling(start)
defer profiler.Stop(false)
defer profiler.Stop(true)
doWorkFor(500 * time.Millisecond)
end := time.Now()
result := profiler.GetSlice(start, end)
Expand Down

0 comments on commit c5f1055

Please sign in to comment.