Skip to content

Commit

Permalink
[chore][pkg/stanza] Migrate more tests to new splittest framework (#2…
Browse files Browse the repository at this point in the history
  • Loading branch information
djaglowski authored Sep 29, 2023
1 parent 8ea9357 commit 203014c
Show file tree
Hide file tree
Showing 5 changed files with 118 additions and 172 deletions.
176 changes: 68 additions & 108 deletions pkg/stanza/fileconsumer/internal/splitter/factory_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,115 +8,75 @@ import (
"testing"
"time"

"github.com/stretchr/testify/assert"

"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/stanza/split/splittest"
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/stanza/trim"
)

func TestFactory(t *testing.T) {
factory := NewFactory(bufio.ScanLines, trim.Nop, 0)
splitFunc := factory.SplitFunc()
assert.NotNil(t, splitFunc)

input := []byte(" hello \n world \n extra ")

advance, token, err := splitFunc(input, false)
assert.NoError(t, err)
assert.Equal(t, 8, advance)
assert.Equal(t, []byte(" hello "), token)

advance, token, err = splitFunc(input[8:], false)
assert.NoError(t, err)
assert.Equal(t, 8, advance)
assert.Equal(t, []byte(" world "), token)

advance, token, err = splitFunc(input[16:], false)
assert.NoError(t, err)
assert.Equal(t, 0, advance)
assert.Nil(t, token)
}

func TestCustomWithTrim(t *testing.T) {
factory := NewFactory(bufio.ScanLines, trim.Whitespace, 0)
splitFunc := factory.SplitFunc()
assert.NotNil(t, splitFunc)

input := []byte(" hello \n world \n extra ")

advance, token, err := splitFunc(input, false)
assert.NoError(t, err)
assert.Equal(t, 8, advance)
assert.Equal(t, []byte("hello"), token)

advance, token, err = splitFunc(input[8:], false)
assert.NoError(t, err)
assert.Equal(t, 8, advance)
assert.Equal(t, []byte("world"), token)

advance, token, err = splitFunc(input[16:], false)
assert.NoError(t, err)
assert.Equal(t, 0, advance)
assert.Nil(t, token)
}

func TestCustomWithFlush(t *testing.T) {
flushPeriod := 100 * time.Millisecond
factory := NewFactory(bufio.ScanLines, trim.Nop, flushPeriod)
splitFunc := factory.SplitFunc()
assert.NotNil(t, splitFunc)

input := []byte(" hello \n world \n extra ")

advance, token, err := splitFunc(input, false)
assert.NoError(t, err)
assert.Equal(t, 8, advance)
assert.Equal(t, []byte(" hello "), token)

advance, token, err = splitFunc(input[8:], false)
assert.NoError(t, err)
assert.Equal(t, 8, advance)
assert.Equal(t, []byte(" world "), token)

advance, token, err = splitFunc(input[16:], false)
assert.NoError(t, err)
assert.Equal(t, 0, advance)
assert.Nil(t, token)

time.Sleep(2 * flushPeriod)

advance, token, err = splitFunc(input[16:], false)
assert.NoError(t, err)
assert.Equal(t, 7, advance)
assert.Equal(t, []byte(" extra "), token)
}

func TestCustomWithFlushTrim(t *testing.T) {
flushPeriod := 100 * time.Millisecond
factory := NewFactory(bufio.ScanLines, trim.Whitespace, flushPeriod)
splitFunc := factory.SplitFunc()
assert.NotNil(t, splitFunc)

input := []byte(" hello \n world \n extra ")

advance, token, err := splitFunc(input, false)
assert.NoError(t, err)
assert.Equal(t, 8, advance)
assert.Equal(t, []byte("hello"), token)

advance, token, err = splitFunc(input[8:], false)
assert.NoError(t, err)
assert.Equal(t, 8, advance)
assert.Equal(t, []byte("world"), token)

advance, token, err = splitFunc(input[16:], false)
assert.NoError(t, err)
assert.Equal(t, 0, advance)
assert.Nil(t, token)

time.Sleep(2 * flushPeriod)

advance, token, err = splitFunc(input[16:], false)
assert.NoError(t, err)
assert.Equal(t, 7, advance)
assert.Equal(t, []byte("extra"), token) // Ensure trim applies to flushed token
func TestFactorySplitFunc(t *testing.T) {
testCases := []struct {
name string
baseFunc bufio.SplitFunc
trimFunc trim.Func
flushPeriod time.Duration
input []byte
steps []splittest.Step
}{
{
name: "ScanLinesStrict",
input: []byte(" hello \n world \n extra "),
baseFunc: splittest.ScanLinesStrict,
trimFunc: trim.Nop,
flushPeriod: 0,
steps: []splittest.Step{
splittest.ExpectAdvanceToken(len(" hello \n"), " hello "),
splittest.ExpectAdvanceToken(len(" world \n"), " world "),
},
},
{
name: "ScanLinesStrictWithTrim",
input: []byte(" hello \n world \n extra "),
baseFunc: splittest.ScanLinesStrict,
trimFunc: trim.Whitespace,
flushPeriod: 0,
steps: []splittest.Step{
splittest.ExpectAdvanceToken(len(" hello \n"), "hello"),
splittest.ExpectAdvanceToken(len(" world \n"), "world"),
},
},
{
name: "ScanLinesStrictWithFlush",
input: []byte(" hello \n world \n extra "),
baseFunc: splittest.ScanLinesStrict,
trimFunc: trim.Nop,
flushPeriod: 100 * time.Millisecond,
steps: []splittest.Step{
splittest.ExpectAdvanceToken(len(" hello \n"), " hello "),
splittest.ExpectAdvanceToken(len(" world \n"), " world "),
splittest.ExpectReadMore(),
splittest.Eventually(
splittest.ExpectAdvanceToken(len(" extra "), " extra "), 200*time.Millisecond, 10*time.Millisecond,
),
},
},
{
name: "ScanLinesStrictWithTrimAndFlush",
input: []byte(" hello \n world \n extra "),
baseFunc: splittest.ScanLinesStrict,
trimFunc: trim.Whitespace,
flushPeriod: 100 * time.Millisecond,
steps: []splittest.Step{
splittest.ExpectAdvanceToken(len(" hello \n"), "hello"),
splittest.ExpectAdvanceToken(len(" world \n"), "world"),
splittest.ExpectReadMore(),
splittest.Eventually(
splittest.ExpectAdvanceToken(len(" extra "), "extra"), 200*time.Millisecond, 10*time.Millisecond,
),
},
},
}

for _, tc := range testCases {
factory := NewFactory(tc.baseFunc, tc.trimFunc, tc.flushPeriod)
t.Run(tc.name, splittest.New(factory.SplitFunc(), tc.input, tc.steps...))
}
}
12 changes: 2 additions & 10 deletions pkg/stanza/flush/flush_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,15 @@ func TestNewlineSplitFunc(t *testing.T) {
{
name: "FlushNoPeriod",
input: []byte("complete line\nincomplete"),
baseFunc: scanLinesStrict,
baseFunc: splittest.ScanLinesStrict,
steps: []splittest.Step{
splittest.ExpectAdvanceToken(len("complete line\n"), "complete line"),
},
},
{
name: "FlushIncompleteLineAfterPeriod",
input: []byte("complete line\nincomplete"),
baseFunc: scanLinesStrict,
baseFunc: splittest.ScanLinesStrict,
flushPeriod: 100 * time.Millisecond,
steps: []splittest.Step{
splittest.ExpectAdvanceToken(len("complete line\n"), "complete line"),
Expand All @@ -45,11 +45,3 @@ func TestNewlineSplitFunc(t *testing.T) {
t.Run(tc.name, splittest.New(splitFunc, tc.input, tc.steps...))
}
}

func scanLinesStrict(data []byte, atEOF bool) (advance int, token []byte, err error) {
advance, token, err = bufio.ScanLines(data, atEOF)
if advance == len(token) {
return 0, nil, nil
}
return
}
9 changes: 9 additions & 0 deletions pkg/stanza/split/splittest/splittest_detailed.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,3 +109,12 @@ func New(splitFunc bufio.SplitFunc, input []byte, steps ...Step) func(*testing.T
func needMoreData(advance int, token []byte, err error) bool {
return advance == 0 && token == nil && err == nil
}

// ScanLinesStrict behaves like bufio.ScanLines except EOF is not considered a line ending.
func ScanLinesStrict(data []byte, atEOF bool) (advance int, token []byte, err error) {
advance, token, err = bufio.ScanLines(data, atEOF)
if advance == len(token) {
return 0, nil, nil
}
return
}
12 changes: 2 additions & 10 deletions pkg/stanza/split/splittest/splittest_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ func TestNew(t *testing.T) {
},
{
name: "ScanLinesNoEOF",
splitFunc: scanLinesStrict,
splitFunc: ScanLinesStrict,
input: []byte("foo bar.\nhello world!\nincomplete line"),
steps: []Step{
ExpectAdvanceToken(len("foo bar.\n"), "foo bar."),
Expand Down Expand Up @@ -130,14 +130,6 @@ func TestNew(t *testing.T) {
}
}

func scanLinesStrict(data []byte, atEOF bool) (advance int, token []byte, err error) {
advance, token, err = bufio.ScanLines(data, atEOF)
if advance == len(token) {
return 0, nil, nil
}
return
}

func scanLinesError(data []byte, atEOF bool) (advance int, token []byte, err error) {
advance, token, err = bufio.ScanLines(data, atEOF)
if strings.Contains(string(token), "error") {
Expand All @@ -149,7 +141,7 @@ func scanLinesError(data []byte, atEOF bool) (advance int, token []byte, err err
func scanLinesStrictWithFlush(flushPeriod time.Duration) bufio.SplitFunc {
now := time.Now()
return func(data []byte, atEOF bool) (advance int, token []byte, err error) {
advance, token, err = scanLinesStrict(data, atEOF)
advance, token, err = ScanLinesStrict(data, atEOF)
if advance > 0 || token != nil || err != nil {
return
}
Expand Down
81 changes: 37 additions & 44 deletions pkg/stanza/trim/trim_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import (
"testing"

"github.com/stretchr/testify/assert"

"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/stanza/split/splittest"
)

func TestTrim(t *testing.T) {
Expand Down Expand Up @@ -75,49 +77,40 @@ func TestTrim(t *testing.T) {
}

func TestWithFunc(t *testing.T) {
scanAndTrimLines := WithFunc(bufio.ScanLines, Config{
PreserveLeading: false,
PreserveTrailing: false,
}.Func())

input := []byte(" hello \n world \n extra ")

advance, token, err := scanAndTrimLines(input, false)
assert.NoError(t, err)
assert.Equal(t, 8, advance)
assert.Equal(t, []byte("hello"), token)

advance, token, err = scanAndTrimLines(input[8:], false)
assert.NoError(t, err)
assert.Equal(t, 8, advance)
assert.Equal(t, []byte("world"), token)

advance, token, err = scanAndTrimLines(input[16:], false)
assert.NoError(t, err)
assert.Equal(t, 0, advance)
assert.Nil(t, token)
}

func TestWithNilTrimFunc(t *testing.T) {
// Same test as above, but pass nil instead of a trim func
// In other words, we should expect exactly the behavior of bufio.ScanLines.

scanLines := WithFunc(bufio.ScanLines, nil)

input := []byte(" hello \n world \n extra ")

advance, token, err := scanLines(input, false)
assert.NoError(t, err)
assert.Equal(t, 8, advance)
assert.Equal(t, []byte(" hello "), token)

advance, token, err = scanLines(input[8:], false)
assert.NoError(t, err)
assert.Equal(t, 8, advance)
assert.Equal(t, []byte(" world "), token)
testCases := []struct {
name string
baseFunc bufio.SplitFunc
trimFunc Func
input []byte
steps []splittest.Step
}{
{
// nil trim func should return original split func
name: "NilTrimFunc",
input: []byte(" hello \n world \n extra "),
baseFunc: bufio.ScanLines,
trimFunc: nil,
steps: []splittest.Step{
splittest.ExpectAdvanceToken(len(" hello \n"), " hello "),
splittest.ExpectAdvanceToken(len(" world \n"), " world "),
splittest.ExpectAdvanceToken(len(" extra "), " extra "),
},
},
{
name: "ScanLinesStrictWithTrim",
input: []byte(" hello \n world \n extra "),
baseFunc: bufio.ScanLines,
trimFunc: Whitespace,
steps: []splittest.Step{
splittest.ExpectAdvanceToken(len(" hello \n"), "hello"),
splittest.ExpectAdvanceToken(len(" world \n"), "world"),
splittest.ExpectAdvanceToken(len(" extra "), "extra"),
},
},
}

advance, token, err = scanLines(input[16:], false)
assert.NoError(t, err)
assert.Equal(t, 0, advance)
assert.Nil(t, token)
for _, tc := range testCases {
splitFunc := WithFunc(tc.baseFunc, tc.trimFunc)
t.Run(tc.name, splittest.New(splitFunc, tc.input, tc.steps...))
}
}

0 comments on commit 203014c

Please sign in to comment.