-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: event stream with carriage return
- Loading branch information
1 parent
8364226
commit 8763a01
Showing
5 changed files
with
143 additions
and
7 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
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
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
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,36 @@ | ||
package event | ||
|
||
import ( | ||
"bytes" | ||
) | ||
|
||
// adapted from ScanLines in scan.go to handle carriage return characters as separators | ||
func scanLinesWithCarriage(data []byte, atEOF bool) (advance int, token []byte, err error) { | ||
if atEOF && len(data) == 0 { | ||
return 0, nil, nil | ||
} | ||
if i, j := bytes.IndexByte(data, '\n'), bytes.IndexByte(data, '\r'); i >= 0 || j >= 0 { | ||
in := i | ||
// Select the first index of \n or \r or the second index of \r if it is followed by \n | ||
if i < 0 || (i > j && i != j+1 && j >= 0) { | ||
in = j | ||
} | ||
|
||
// We have a full newline-terminated line. | ||
return in + 1, dropCR(data[0:in]), nil | ||
} | ||
// If we're at EOF, we have a final, non-terminated line. Return it. | ||
if atEOF { | ||
return len(data), dropCR(data), nil | ||
} | ||
// Request more data. | ||
return 0, nil, nil | ||
} | ||
|
||
// dropCR drops a terminal \r from the data. | ||
func dropCR(data []byte) []byte { | ||
if len(data) > 0 && data[len(data)-1] == '\r' { | ||
return data[0 : len(data)-1] | ||
} | ||
return data | ||
} |
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,97 @@ | ||
package event | ||
|
||
import ( | ||
"bufio" | ||
"bytes" | ||
"testing" | ||
|
||
"github.com/prysmaticlabs/prysm/v5/testing/require" | ||
) | ||
|
||
func TestScanLinesWithCarriage(t *testing.T) { | ||
testCases := []struct { | ||
name string | ||
input string | ||
expected []string | ||
}{ | ||
{ | ||
name: "LF line endings", | ||
input: "line1\nline2\nline3", | ||
expected: []string{"line1", "line2", "line3"}, | ||
}, | ||
{ | ||
name: "CR line endings", | ||
input: "line1\rline2\rline3", | ||
expected: []string{"line1", "line2", "line3"}, | ||
}, | ||
{ | ||
name: "CRLF line endings", | ||
input: "line1\r\nline2\r\nline3", | ||
expected: []string{"line1", "line2", "line3"}, | ||
}, | ||
{ | ||
name: "Mixed line endings", | ||
input: "line1\nline2\rline3\r\nline4", | ||
expected: []string{"line1", "line2", "line3", "line4"}, | ||
}, | ||
{ | ||
name: "Empty lines", | ||
input: "line1\n\nline2\r\rline3", | ||
expected: []string{"line1", "", "line2", "", "line3"}, | ||
}, | ||
{ | ||
name: "Empty lines 2", | ||
input: "line1\n\rline2\n\rline3", | ||
expected: []string{"line1", "", "line2", "", "line3"}, | ||
}, | ||
{ | ||
name: "No line endings", | ||
input: "single line without ending", | ||
expected: []string{"single line without ending"}, | ||
}, | ||
} | ||
|
||
for _, tc := range testCases { | ||
t.Run(tc.name, func(t *testing.T) { | ||
scanner := bufio.NewScanner(bytes.NewReader([]byte(tc.input))) | ||
scanner.Split(scanLinesWithCarriage) | ||
|
||
var lines []string | ||
for scanner.Scan() { | ||
lines = append(lines, scanner.Text()) | ||
} | ||
|
||
require.NoError(t, scanner.Err()) | ||
require.Equal(t, len(tc.expected), len(lines), "Number of lines does not match") | ||
for i, line := range lines { | ||
require.Equal(t, tc.expected[i], line, "Line %d does not match", i) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
// TestScanLinesWithCarriageEdgeCases tests edge cases and potential error scenarios | ||
func TestScanLinesWithCarriageEdgeCases(t *testing.T) { | ||
t.Run("Empty input", func(t *testing.T) { | ||
scanner := bufio.NewScanner(bytes.NewReader([]byte(""))) | ||
scanner.Split(scanLinesWithCarriage) | ||
require.Equal(t, scanner.Scan(), false) | ||
require.NoError(t, scanner.Err()) | ||
}) | ||
|
||
t.Run("Very long line", func(t *testing.T) { | ||
longLine := bytes.Repeat([]byte("a"), bufio.MaxScanTokenSize+1) | ||
scanner := bufio.NewScanner(bytes.NewReader(longLine)) | ||
scanner.Split(scanLinesWithCarriage) | ||
require.Equal(t, scanner.Scan(), false) | ||
require.NotNil(t, scanner.Err()) | ||
}) | ||
|
||
t.Run("Line ending at max token size", func(t *testing.T) { | ||
input := append(bytes.Repeat([]byte("a"), bufio.MaxScanTokenSize-1), '\n') | ||
scanner := bufio.NewScanner(bytes.NewReader(input)) | ||
scanner.Split(scanLinesWithCarriage) | ||
require.Equal(t, scanner.Scan(), true) | ||
require.Equal(t, string(bytes.Repeat([]byte("a"), bufio.MaxScanTokenSize-1)), scanner.Text()) | ||
}) | ||
} |