-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[fileconsumer] Move scanner into internal package (#23999)
Follows #23998 Moves the `PositionalScanner` into an `internal/scanner` package.
- Loading branch information
1 parent
914fd0b
commit 751c2e1
Showing
5 changed files
with
197 additions
and
133 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
# Use this changelog template to create an entry for release notes. | ||
# If your change doesn't affect end users, such as a test fix or a tooling change, | ||
# you should instead start your pull request title with [chore] or use the "Skip Changelog" label. | ||
|
||
# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix' | ||
change_type: breaking | ||
|
||
# The name of the component, or a single word describing the area of concern, (e.g. filelogreceiver) | ||
component: pkg/stanza | ||
|
||
# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`). | ||
note: Make fileconsumer.PositionalScanner internal | ||
|
||
# Mandatory: One or more tracking issues related to the change. You can use the PR number here if no issue exists. | ||
issues: [23999] | ||
|
||
# (Optional) One or more lines of additional information to render under the primary note. | ||
# These lines will be padded with 2 spaces and then inserted directly into the document. | ||
# Use pipe (|) for multiline entries. | ||
subtext: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
152 changes: 152 additions & 0 deletions
152
pkg/stanza/fileconsumer/internal/scanner/scanner_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,152 @@ | ||
// Copyright The OpenTelemetry Authors | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package scanner | ||
|
||
import ( | ||
"bufio" | ||
"bytes" | ||
"errors" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestScanner(t *testing.T) { | ||
testCases := []struct { | ||
name string | ||
stream []byte | ||
delimiter []byte | ||
startOffset int64 | ||
maxSize int | ||
bufferSize int | ||
expected [][]byte | ||
skipFirstDelimiter bool | ||
}{ | ||
{ | ||
name: "simple", | ||
stream: []byte("testlog1\ntestlog2\n"), | ||
delimiter: []byte("\n"), | ||
maxSize: 100, | ||
bufferSize: DefaultBufferSize, | ||
expected: [][]byte{ | ||
[]byte("testlog1"), | ||
[]byte("testlog2"), | ||
}, | ||
}, | ||
{ | ||
name: "empty_tokens", | ||
stream: []byte("\ntestlog1\n\ntestlog2\n\n"), | ||
delimiter: []byte("\n"), | ||
maxSize: 100, | ||
bufferSize: DefaultBufferSize, | ||
expected: [][]byte{ | ||
[]byte(""), | ||
[]byte("testlog1"), | ||
[]byte(""), | ||
[]byte("testlog2"), | ||
[]byte(""), | ||
}, | ||
}, | ||
{ | ||
name: "multichar_delimiter", | ||
stream: []byte("testlog1@#$testlog2@#$"), | ||
delimiter: []byte("@#$"), | ||
maxSize: 100, | ||
bufferSize: DefaultBufferSize, | ||
expected: [][]byte{ | ||
[]byte("testlog1"), | ||
[]byte("testlog2"), | ||
}, | ||
}, | ||
{ | ||
name: "multichar_delimiter_empty_tokens", | ||
stream: []byte("@#$testlog1@#$@#$testlog2@#$@#$"), | ||
delimiter: []byte("@#$"), | ||
maxSize: 100, | ||
bufferSize: DefaultBufferSize, | ||
expected: [][]byte{ | ||
[]byte(""), | ||
[]byte("testlog1"), | ||
[]byte(""), | ||
[]byte("testlog2"), | ||
[]byte(""), | ||
}, | ||
}, | ||
{ | ||
name: "overflow_maxlogsize", | ||
stream: []byte("testlog1islongerthanmaxlogsize\n"), | ||
delimiter: []byte("\n"), | ||
maxSize: 20, | ||
bufferSize: DefaultBufferSize, | ||
expected: [][]byte{ | ||
[]byte("testlog1islongerthan"), | ||
[]byte("maxlogsize"), | ||
}, | ||
skipFirstDelimiter: true, | ||
}, | ||
{ | ||
name: "overflow_buffer", | ||
stream: []byte("testlog1islongerthanbuffer\n"), | ||
delimiter: []byte("\n"), | ||
maxSize: 20, | ||
bufferSize: 20, | ||
expected: [][]byte{ | ||
[]byte("testlog1islongerthan"), | ||
[]byte("buffer"), | ||
}, | ||
skipFirstDelimiter: true, | ||
}, | ||
} | ||
|
||
for _, tc := range testCases { | ||
t.Run(tc.name, func(t *testing.T) { | ||
scanner := New(bytes.NewReader(tc.stream), tc.maxSize, tc.bufferSize, tc.startOffset, simpleSplit(tc.delimiter)) | ||
for i, p := 0, 0; scanner.Scan(); i++ { | ||
assert.NoError(t, scanner.Error()) | ||
|
||
token := scanner.Bytes() | ||
assert.Equal(t, tc.expected[i], token) | ||
|
||
p += len(tc.expected[i]) | ||
if i > 0 || !tc.skipFirstDelimiter { | ||
p += len(tc.delimiter) | ||
} | ||
assert.Equal(t, int64(p), scanner.Pos()) | ||
} | ||
assert.NoError(t, scanner.Error()) | ||
}) | ||
} | ||
} | ||
|
||
func simpleSplit(delim []byte) bufio.SplitFunc { | ||
return func(data []byte, atEOF bool) (advance int, token []byte, err error) { | ||
if atEOF && len(data) == 0 { | ||
return 0, nil, nil | ||
} | ||
if i := bytes.Index(data, delim); i >= 0 { | ||
return i + len(delim), data[:i], nil | ||
} | ||
return 0, nil, nil | ||
} | ||
} | ||
|
||
type errReader struct { | ||
err error | ||
} | ||
|
||
func (r *errReader) Read([]byte) (n int, err error) { | ||
return 0, r.err | ||
} | ||
|
||
func TestScannerError(t *testing.T) { | ||
reader := &errReader{err: bufio.ErrTooLong} | ||
scanner := New(reader, 100, 100, 0, simpleSplit([]byte("\n"))) | ||
assert.False(t, scanner.Scan()) | ||
assert.EqualError(t, scanner.Error(), "log entry too large") | ||
|
||
reader = &errReader{err: errors.New("some err")} | ||
scanner = New(reader, 100, 100, 0, simpleSplit([]byte("\n"))) | ||
assert.False(t, scanner.Scan()) | ||
assert.EqualError(t, scanner.Error(), "scanner error: some err") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.