From a862a4fd47ee2df6d1c300f9eb74cedec3618e70 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adam=20Medzi=C5=84ski?= Date: Wed, 27 Jun 2018 15:56:00 +0200 Subject: [PATCH] Fix scraping of an empty lines (#117) The previous logic was sending blank log entries for empty log lines. --- servicelog/scraper/json.go | 1 + servicelog/scraper/json_test.go | 36 +++++++++++++++++++++++++++++++-- 2 files changed, 35 insertions(+), 2 deletions(-) diff --git a/servicelog/scraper/json.go b/servicelog/scraper/json.go index 6b15ba02..cba6e5e0 100644 --- a/servicelog/scraper/json.go +++ b/servicelog/scraper/json.go @@ -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 { diff --git a/servicelog/scraper/json_test.go b/servicelog/scraper/json_test.go index b829d625..7151d5ac 100644 --- a/servicelog/scraper/json_test.go +++ b/servicelog/scraper/json_test.go @@ -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" ) @@ -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) } @@ -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 }