Skip to content

Commit

Permalink
[chore][pkg/stanza] Move some reader tests into reader package (open-…
Browse files Browse the repository at this point in the history
…telemetry#29664)

This localizes a few tests by moving them from `fileconsumer` to
`reader` package.
  • Loading branch information
djaglowski authored Dec 5, 2023
1 parent 4c16e26 commit d6dda62
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 34 deletions.
49 changes: 49 additions & 0 deletions pkg/stanza/fileconsumer/internal/reader/factory_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

package reader

import (
"testing"
"time"

"github.com/stretchr/testify/require"

"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/stanza/decode"
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/stanza/fileconsumer/internal/emittest"
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/stanza/fileconsumer/internal/fingerprint"
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/stanza/split"
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/stanza/testutil"
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/stanza/trim"
)

const (
defaultMaxLogSize = 1024 * 1024
defaultMaxConcurrentFiles = 1024
defaultEncoding = "utf-8"
defaultPollInterval = 200 * time.Millisecond
defaultFlushPeriod = 500 * time.Millisecond
)

func testFactory(t *testing.T, sCfg split.Config, maxLogSize int, flushPeriod time.Duration) (*Factory, *emittest.Sink) {
enc, err := decode.LookupEncoding(defaultEncoding)
require.NoError(t, err)

splitFunc, err := sCfg.Func(enc, false, maxLogSize)
require.NoError(t, err)

sink := emittest.NewSink()
return &Factory{
SugaredLogger: testutil.Logger(t),
Config: &Config{
FingerprintSize: fingerprint.DefaultSize,
MaxLogSize: maxLogSize,
Emit: sink.Callback,
FlushTimeout: flushPeriod,
},
FromBeginning: true,
Encoding: enc,
SplitFunc: splitFunc,
TrimFunc: trim.Whitespace,
}, sink
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

package fileconsumer
package reader

import (
"context"
Expand All @@ -12,21 +12,16 @@ import (
"github.com/stretchr/testify/require"

"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/stanza/decode"
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/stanza/fileconsumer/internal/emittest"
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/stanza/fileconsumer/internal/filetest"
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/stanza/fileconsumer/internal/fingerprint"
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/stanza/fileconsumer/internal/header"
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/stanza/fileconsumer/internal/reader"
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/stanza/operator"
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/stanza/operator/parser/regex"
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/stanza/split"
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/stanza/testutil"
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/stanza/trim"
)

func TestPersistFlusher(t *testing.T) {
flushPeriod := 100 * time.Millisecond
f, sink := testReaderFactory(t, split.Config{}, defaultMaxLogSize, flushPeriod)
f, sink := testFactory(t, split.Config{}, defaultMaxLogSize, flushPeriod)

temp := filetest.OpenTemp(t, t.TempDir())
fp, err := f.NewFingerprint(temp)
Expand Down Expand Up @@ -112,7 +107,7 @@ func TestTokenization(t *testing.T) {

for _, tc := range testCases {
t.Run(tc.testName, func(t *testing.T) {
f, sink := testReaderFactory(t, split.Config{}, defaultMaxLogSize, defaultFlushPeriod)
f, sink := testFactory(t, split.Config{}, defaultMaxLogSize, defaultFlushPeriod)

temp := filetest.OpenTemp(t, t.TempDir())
_, err := temp.Write(tc.fileContent)
Expand Down Expand Up @@ -142,7 +137,7 @@ func TestTokenizationTooLong(t *testing.T) {
[]byte("aaa"),
}

f, sink := testReaderFactory(t, split.Config{}, 10, defaultFlushPeriod)
f, sink := testFactory(t, split.Config{}, 10, defaultFlushPeriod)

temp := filetest.OpenTemp(t, t.TempDir())
_, err := temp.Write(fileContent)
Expand Down Expand Up @@ -174,7 +169,7 @@ func TestTokenizationTooLongWithLineStartPattern(t *testing.T) {

sCfg := split.Config{}
sCfg.LineStartPattern = `\d+-\d+-\d+`
f, sink := testReaderFactory(t, sCfg, 15, defaultFlushPeriod)
f, sink := testFactory(t, sCfg, 15, defaultFlushPeriod)

temp := filetest.OpenTemp(t, t.TempDir())
_, err := temp.Write(fileContent)
Expand All @@ -196,7 +191,7 @@ func TestTokenizationTooLongWithLineStartPattern(t *testing.T) {
func TestHeaderFingerprintIncluded(t *testing.T) {
fileContent := []byte("#header-line\naaa\n")

f, _ := testReaderFactory(t, split.Config{}, 10, defaultFlushPeriod)
f, _ := testFactory(t, split.Config{}, 10, defaultFlushPeriod)

regexConf := regex.NewConfig()
regexConf.Regex = "^#(?P<header>.*)"
Expand All @@ -223,26 +218,3 @@ func TestHeaderFingerprintIncluded(t *testing.T) {

require.Equal(t, []byte("#header-line\naaa\n"), r.Fingerprint.FirstBytes)
}

func testReaderFactory(t *testing.T, sCfg split.Config, maxLogSize int, flushPeriod time.Duration) (*reader.Factory, *emittest.Sink) {
enc, err := decode.LookupEncoding(defaultEncoding)
require.NoError(t, err)

splitFunc, err := sCfg.Func(enc, false, maxLogSize)
require.NoError(t, err)

sink := emittest.NewSink()
return &reader.Factory{
SugaredLogger: testutil.Logger(t),
Config: &reader.Config{
FingerprintSize: fingerprint.DefaultSize,
MaxLogSize: maxLogSize,
Emit: sink.Callback,
FlushTimeout: flushPeriod,
},
FromBeginning: true,
Encoding: enc,
SplitFunc: splitFunc,
TrimFunc: trim.Whitespace,
}, sink
}

0 comments on commit d6dda62

Please sign in to comment.