diff --git a/internal/component/faro/receiver/receiver_test.go b/internal/component/faro/receiver/receiver_test.go index 15c732e3a..5945e276f 100644 --- a/internal/component/faro/receiver/receiver_test.go +++ b/internal/component/faro/receiver/receiver_test.go @@ -4,7 +4,9 @@ import ( "fmt" "net/http" "strings" + "sync" "testing" + "time" "github.com/grafana/loki/v3/pkg/logproto" "github.com/phayes/freeport" @@ -139,3 +141,52 @@ func Test(t *testing.T) { }) } } + +type fakeLogsReceiver struct { + ch chan loki.Entry + + entriesMut sync.RWMutex + entries []loki.Entry +} + +var _ loki.LogsReceiver = (*fakeLogsReceiver)(nil) + +func newFakeLogsReceiver(t *testing.T) *fakeLogsReceiver { + ctx := componenttest.TestContext(t) + + lr := &fakeLogsReceiver{ + ch: make(chan loki.Entry), + } + + go func() { + defer close(lr.ch) + + select { + case <-ctx.Done(): + return + case ent := <-lr.Chan(): + lr.entriesMut.Lock() + lr.entries = append(lr.entries, loki.Entry{ + Labels: ent.Labels, + Entry: logproto.Entry{ + Timestamp: time.Time{}, // Use consistent time for testing. + Line: ent.Line, + StructuredMetadata: ent.StructuredMetadata, + }, + }) + lr.entriesMut.Unlock() + } + }() + + return lr +} + +func (lr *fakeLogsReceiver) Chan() chan loki.Entry { + return lr.ch +} + +func (lr *fakeLogsReceiver) GetEntries() []loki.Entry { + lr.entriesMut.RLock() + defer lr.entriesMut.RUnlock() + return lr.entries +} diff --git a/internal/component/faro/receiver/test_utils.go b/internal/component/faro/receiver/test_utils.go deleted file mode 100644 index cc0376756..000000000 --- a/internal/component/faro/receiver/test_utils.go +++ /dev/null @@ -1,60 +0,0 @@ -package receiver - -import ( - "sync" - "testing" - "time" - - "github.com/grafana/alloy/internal/component/common/loki" - "github.com/grafana/alloy/internal/runtime/componenttest" - "github.com/grafana/loki/v3/pkg/logproto" -) - -type fakeLogsReceiver struct { - ch chan loki.Entry - - entriesMut sync.RWMutex - entries []loki.Entry -} - -var _ loki.LogsReceiver = (*fakeLogsReceiver)(nil) - -func newFakeLogsReceiver(t *testing.T) *fakeLogsReceiver { - ctx := componenttest.TestContext(t) - - lr := &fakeLogsReceiver{ - ch: make(chan loki.Entry), - } - - go func() { - defer close(lr.ch) - - select { - case <-ctx.Done(): - return - case ent := <-lr.Chan(): - lr.entriesMut.Lock() - lr.entries = append(lr.entries, loki.Entry{ - Labels: ent.Labels, - Entry: logproto.Entry{ - Timestamp: time.Time{}, // Use consistent time for testing. - Line: ent.Line, - StructuredMetadata: ent.StructuredMetadata, - }, - }) - lr.entriesMut.Unlock() - } - }() - - return lr -} - -func (lr *fakeLogsReceiver) Chan() chan loki.Entry { - return lr.ch -} - -func (lr *fakeLogsReceiver) GetEntries() []loki.Entry { - lr.entriesMut.RLock() - defer lr.entriesMut.RUnlock() - return lr.entries -}