Skip to content

Commit

Permalink
sdk/metric: Fix goroutine leaks in tests (#4352)
Browse files Browse the repository at this point in the history
  • Loading branch information
pellared authored Jul 25, 2023
1 parent 088ac8e commit d8d3502
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 9 deletions.
14 changes: 5 additions & 9 deletions sdk/metric/cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ func TestCacheConcurrentSafe(t *testing.T) {
const (
key = "k"
goroutines = 10
timeoutSec = 5
)

c := cache[string, int]{}
Expand All @@ -65,12 +64,9 @@ func TestCacheConcurrentSafe(t *testing.T) {
close(done)
}()

assert.Eventually(t, func() bool {
select {
case <-done:
return true
default:
return false
}
}, timeoutSec*time.Second, 10*time.Millisecond)
select {
case <-done:
case <-time.After(5 * time.Second):
assert.Fail(t, "timeout")
}
}
12 changes: 12 additions & 0 deletions sdk/metric/provider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,42 +35,54 @@ func TestMeterConcurrentSafe(t *testing.T) {
const name = "TestMeterConcurrentSafe meter"
mp := NewMeterProvider()

done := make(chan struct{})
go func() {
defer close(done)
_ = mp.Meter(name)
}()

_ = mp.Meter(name)
<-done
}

func TestForceFlushConcurrentSafe(t *testing.T) {
mp := NewMeterProvider()

done := make(chan struct{})
go func() {
defer close(done)
_ = mp.ForceFlush(context.Background())
}()

_ = mp.ForceFlush(context.Background())
<-done
}

func TestShutdownConcurrentSafe(t *testing.T) {
mp := NewMeterProvider()

done := make(chan struct{})
go func() {
defer close(done)
_ = mp.Shutdown(context.Background())
}()

_ = mp.Shutdown(context.Background())
<-done
}

func TestMeterAndShutdownConcurrentSafe(t *testing.T) {
const name = "TestMeterAndShutdownConcurrentSafe meter"
mp := NewMeterProvider()

done := make(chan struct{})
go func() {
defer close(done)
_ = mp.Shutdown(context.Background())
}()

_ = mp.Meter(name)
<-done
}

func TestMeterDoesNotPanicForEmptyMeterProvider(t *testing.T) {
Expand Down

0 comments on commit d8d3502

Please sign in to comment.