Skip to content

Commit

Permalink
[chore][pkg/stanza] Adds a few more test cases to reader (#31022)
Browse files Browse the repository at this point in the history
  • Loading branch information
djaglowski authored Feb 2, 2024
1 parent 3efc3e4 commit 6baae7a
Showing 1 changed file with 47 additions and 0 deletions.
47 changes: 47 additions & 0 deletions pkg/stanza/fileconsumer/internal/reader/factory_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,14 @@ import (
"testing"
"time"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"golang.org/x/text/encoding"
"golang.org/x/text/encoding/unicode"

"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/stanza/fileconsumer/attrs"
"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/split"
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/stanza/testutil"
Expand All @@ -33,6 +35,9 @@ func testFactory(t *testing.T, opts ...testFactoryOpt) (*Factory, *emittest.Sink
trimFunc: trim.Whitespace,
flushPeriod: defaultFlushPeriod,
sinkCallBufferSize: 100,
attributes: attrs.Resolver{
IncludeFileName: true,
},
}
for _, opt := range opts {
opt(cfg)
Expand Down Expand Up @@ -99,3 +104,45 @@ func withSinkBufferSize(n int) testFactoryOpt {
c.sinkCallBufferSize = n
}
}

func fromEnd() testFactoryOpt {
return func(c *testFactoryCfg) {
c.fromBeginning = false
}
}

func TestStartAt(t *testing.T) {
tempDir := t.TempDir()
temp := filetest.OpenTemp(t, tempDir)
content := "some text\n"
_, err := temp.WriteString(content)
require.NoError(t, err)

f, _ := testFactory(t, withFingerprintSize(len(content)*2))
fp, err := f.NewFingerprint(temp)
require.NoError(t, err)
r, err := f.NewReader(temp, fp)
require.NoError(t, err)
assert.Equal(t, int64(0), r.Offset)

f, _ = testFactory(t, withFingerprintSize(len(content)/2), fromEnd())
fp, err = f.NewFingerprint(temp)
require.NoError(t, err)
r, err = f.NewReader(temp, fp)
require.NoError(t, err)
assert.Equal(t, int64(len(content)), r.Offset)

f, _ = testFactory(t, withFingerprintSize(len(content)/2))
fp, err = f.NewFingerprint(temp)
require.NoError(t, err)
r, err = f.NewReader(temp, fp)
require.NoError(t, err)
assert.Equal(t, int64(0), r.Offset)

f, _ = testFactory(t, withFingerprintSize(len(content)/2), fromEnd())
fp, err = f.NewFingerprint(temp)
require.NoError(t, err)
r, err = f.NewReader(temp, fp)
require.NoError(t, err)
assert.Equal(t, int64(len(content)), r.Offset)
}

0 comments on commit 6baae7a

Please sign in to comment.