Skip to content

Commit

Permalink
Fix scraping of an empty lines (#117)
Browse files Browse the repository at this point in the history
The previous logic was sending blank log entries for empty log lines.
  • Loading branch information
medzin authored Jun 27, 2018
1 parent 18a921e commit a862a4f
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 2 deletions.
1 change: 1 addition & 0 deletions servicelog/scraper/json.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ func (j *JSON) scanLoop(reader io.Reader, logEntries chan<- servicelog.Entry) er
logEntry = j.wrapInDefault(scanner.Bytes())
} else {
fmt.Fprintf(invalidLogsWriter, "%s\n", scanner.Bytes())
continue
}
} else if j.KeyFilter != nil {
for key := range logEntry {
Expand Down
36 changes: 34 additions & 2 deletions servicelog/scraper/json_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,13 @@ package scraper

import (
"bytes"
"errors"
"fmt"
"io"
"testing"
"time"

"github.com/allegro/mesos-executor/servicelog"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
)
Expand Down Expand Up @@ -47,10 +50,11 @@ func TestIfPrintsToStdoutValuesInvalidLogEntriesWhenDisabled(t *testing.T) {
InvalidLogsWriter: mockStdout,
}

_ = scraper.StartScraping(reader)
entries := scraper.StartScraping(reader)
go writer.Write([]byte("ERROR my invalid format\n"))
time.Sleep(time.Millisecond)
err := noEntryWithTimeout(entries, time.Millisecond)

assert.NoError(t, err)
mockStdout.AssertExpectations(t)
}

Expand Down Expand Up @@ -90,6 +94,34 @@ func TestIfNotFailsWithTooLongTokens(t *testing.T) {
assert.Len(t, entry, 2)
}

func TestIfIgnoresEmptyLogLines(t *testing.T) {
reader, writer := io.Pipe()
scraper := JSON{}

entries := scraper.StartScraping(reader)

go writer.Write([]byte("\n"))
err1 := noEntryWithTimeout(entries, time.Millisecond)
assert.NoError(t, err1)

go writer.Write([]byte(" \t\n"))
err2 := noEntryWithTimeout(entries, time.Millisecond)
assert.NoError(t, err2)
}

func noEntryWithTimeout(entries <-chan servicelog.Entry, timeout time.Duration) error {
timeoutChan := time.After(timeout)
select {
case entry, ok := <-entries:
if ok {
return fmt.Errorf("entry %s was read before timeout", entry)
}
return errors.New("channel closed before timeout")
case <-timeoutChan:
return nil
}
}

type mockWriter struct {
mock.Mock
}
Expand Down

0 comments on commit a862a4f

Please sign in to comment.