Skip to content

Commit

Permalink
test: fix TestNewReceiver race conditions (#42084) (#42090)
Browse files Browse the repository at this point in the history
update zapcore to use observer zaptest package to avoid
race condition on zapLogs bytes.Buffer

use atomic for countLogs to avoid race condition on
primitive type

(cherry picked from commit d115014)

Co-authored-by: kruskall <[email protected]>
  • Loading branch information
mergify[bot] and kruskall authored Dec 17, 2024
1 parent 200b74a commit 6e72e29
Showing 1 changed file with 8 additions and 11 deletions.
19 changes: 8 additions & 11 deletions x-pack/metricbeat/mbreceiver/receiver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
package mbreceiver

import (
"bytes"
"context"
"sync/atomic"
"testing"
"time"

Expand All @@ -16,6 +16,7 @@ import (
"go.opentelemetry.io/collector/receiver"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
"go.uber.org/zap/zaptest/observer"
)

func TestNewReceiver(t *testing.T) {
Expand Down Expand Up @@ -45,24 +46,20 @@ func TestNewReceiver(t *testing.T) {
},
}

var zapLogs bytes.Buffer
core := zapcore.NewCore(
zapcore.NewJSONEncoder(zap.NewProductionEncoderConfig()),
zapcore.AddSync(&zapLogs),
zapcore.DebugLevel)
core, logs := observer.New(zapcore.DebugLevel)

receiverSettings := receiver.Settings{}
receiverSettings.Logger = zap.New(core)

var countLogs int
var countLogs atomic.Int64
logConsumer, err := consumer.NewLogs(func(ctx context.Context, ld plog.Logs) error {
countLogs = countLogs + ld.LogRecordCount()
countLogs.Add(int64(ld.LogRecordCount()))
return nil
})
require.NoError(t, err, "Error creating log consumer")

r, err := createReceiver(context.Background(), receiverSettings, &config, logConsumer)
require.NoErrorf(t, err, "Error creating receiver. Logs:\n %s", zapLogs.String())
require.NoErrorf(t, err, "Error creating receiver. Logs:\n %s", logs.All())
err = r.Start(context.Background(), nil)
require.NoError(t, err, "Error starting metricbeatreceiver")

Expand All @@ -75,10 +72,10 @@ func TestNewReceiver(t *testing.T) {
for tick := ticker.C; ; {
select {
case <-timer.C:
t.Fatalf("consumed logs didn't increase\nCount: %d\nLogs: %s\n", countLogs, zapLogs.String())
t.Fatalf("consumed logs didn't increase\nCount: %d\nLogs: %v\n", countLogs.Load(), logs.All())
case <-tick:
tick = nil
go func() { ch <- countLogs > 0 }()
go func() { ch <- countLogs.Load() > 0 }()
case v := <-ch:
if v {
goto found
Expand Down

0 comments on commit 6e72e29

Please sign in to comment.