Skip to content

Commit

Permalink
fix: set read buffer size to 64kb to keep existing behaviour with sho…
Browse files Browse the repository at this point in the history
…rter lines

Fix some out of bounds errors
Don't run the tests twice
  • Loading branch information
hairyhum committed Feb 12, 2024
1 parent 8c15e43 commit dbdb985
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 11 deletions.
4 changes: 0 additions & 4 deletions pkg/output/output_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,10 @@ import (
"context"
"io"
"strings"
"testing"

. "gopkg.in/check.v1"
)

// Hook up gocheck into the "go test" runner.
func Test(t *testing.T) { TestingT(t) }

type OutputSuite struct{}

var _ = Suite(&OutputSuite{})
Expand Down
11 changes: 9 additions & 2 deletions pkg/output/stream.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ type scanState struct {
separatorSuffix []byte
}

const bufferSize64k = 64 * 1024

func splitLines(ctx context.Context, r io.ReadCloser, f func(context.Context, []byte) error) error {
// Call r.Close() if the context is canceled or if s.Scan() == false.
ctx, cancel := context.WithCancel(ctx)
Expand All @@ -42,7 +44,8 @@ func splitLines(ctx context.Context, r io.ReadCloser, f func(context.Context, []

state := InitState()

reader := bufio.NewReader(r)
reader := bufio.NewReaderSize(r, bufferSize64k)
// reader := bufio.NewReader(r)

// Run a simple state machine loop
for {
Expand All @@ -54,6 +57,10 @@ func splitLines(ctx context.Context, r io.ReadCloser, f func(context.Context, []
if err != nil {
return err
}
// Skip empty lines
if len(line) == 0 {
continue
}
if state.readingOutput {
if state, err = handleOutput(state, line, isPrefix, ctx, f); err != nil {
return err
Expand Down Expand Up @@ -162,7 +169,7 @@ func captureOutputContent(line []byte, isPrefix bool, startIndex int, ctx contex
func checkSplitSeparator(line []byte) (splitSeparator int, separatorSuffix []byte) {
lineLength := len(line)
phaseOpBytes := []byte(PhaseOpString)
for i := 1; i < len(phaseOpBytes); i++ {
for i := 1; i < len(phaseOpBytes) && i <= lineLength; i++ {
if bytes.Equal(line[lineLength-i:], phaseOpBytes[0:i]) {
return lineLength - i, phaseOpBytes[i:]
}
Expand Down
6 changes: 1 addition & 5 deletions pkg/output/stream_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import (
"context"
"io"
"math/rand"
"testing"
"time"

"github.com/kanisterio/kanister/pkg/output"
Expand All @@ -36,9 +35,6 @@ const (
EndlineRandom
)

// Hook up gocheck into the "go test" runner.
func Test(t *testing.T) { TestingT(t) }

type OutputTestSuite struct{}

var _ = Suite(&OutputTestSuite{})
Expand Down Expand Up @@ -148,7 +144,7 @@ func (s *OutputTestSuite) TestHugeStreamsWithoutPhaseOutput(c *C) {
// I expect that it will be logged as one line as it was before
// But in fact it is logged by chunks of ±4kb
// When we will fix the code behavior, numOfLines has to be set to 10, and avgPrefix len has to be set to 500000
cases := generateTestCases(1, 10000, 0, 0, EndlineRequired)
cases := generateTestCases(10, 50000, 0, 0, EndlineRequired)
r := getTestReaderCloser(done, cases)
m, e := output.LogAndParse(context.TODO(), r)
c.Check(e, IsNil)
Expand Down

0 comments on commit dbdb985

Please sign in to comment.