Skip to content

Commit

Permalink
Fix data race in scraper_test.
Browse files Browse the repository at this point in the history
  • Loading branch information
igorpeshansky committed Sep 12, 2024
1 parent 5de8b1c commit ea0eba5
Showing 1 changed file with 8 additions and 0 deletions.
8 changes: 8 additions & 0 deletions receiver/dcgmreceiver/scraper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ package dcgmreceiver
import (
"context"
"strings"
"sync"
"testing"

"github.com/stretchr/testify/assert"
Expand All @@ -33,10 +34,13 @@ import (

func TestScraperWithoutDcgm(t *testing.T) {
var settings receiver.CreateSettings
var mu sync.Mutex
seenDcgmNotInstalledWarning := false
settings.Logger = zaptest.NewLogger(t, zaptest.WrapOptions(zap.Hooks(func(e zapcore.Entry) error {
if e.Level == zap.WarnLevel && strings.Contains(e.Message, "Unable to connect to DCGM daemon at localhost:5555 on libdcgm.so not Found; Is the DCGM daemon running") {
mu.Lock()
seenDcgmNotInstalledWarning = true
mu.Unlock()
}
return nil
})))
Expand All @@ -48,13 +52,17 @@ func TestScraperWithoutDcgm(t *testing.T) {
require.NoError(t, err)

metrics, err := scraper.scrape(context.Background())
mu.Lock()
assert.Equal(t, true, seenDcgmNotInstalledWarning)
mu.Unlock()
assert.NoError(t, err) // If failed to init DCGM, should have no error
assert.Equal(t, 0, metrics.MetricCount())

// Scrape again with DCGM not available
metrics, err = scraper.scrape(context.Background())
mu.Lock()
assert.Equal(t, true, seenDcgmNotInstalledWarning)
mu.Unlock()
assert.NoError(t, err)
assert.Equal(t, 0, metrics.MetricCount())

Expand Down

0 comments on commit ea0eba5

Please sign in to comment.