From 60a3922446f4c210531b5df029644f4e06d70046 Mon Sep 17 00:00:00 2001 From: Kelvin Wang Date: Thu, 19 Mar 2020 17:50:41 -0400 Subject: [PATCH] fix(models): fix the line breaker --- models/points_parser.go | 2 +- models/points_test.go | 17 +++++++++++++++++ 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/models/points_parser.go b/models/points_parser.go index b57347321b5..bfe1d4d0043 100644 --- a/models/points_parser.go +++ b/models/points_parser.go @@ -152,7 +152,7 @@ func (pp *pointsParser) parsePoints(buf []byte) (err error) { } // strip the newline if one is present - if block[len(block)-1] == '\n' { + if lb := block[len(block)-1]; lb == '\n' || lb == '\r' { block = block[:len(block)-1] } diff --git a/models/points_test.go b/models/points_test.go index a3fa6aaec4b..f03fb42411d 100644 --- a/models/points_test.go +++ b/models/points_test.go @@ -832,6 +832,23 @@ func TestParsePointFloatMultipleDecimals(t *testing.T) { } } +func TestParseWithLineBreaks(t *testing.T) { + ss := []string{ + "cpu,host=serverA,region=us-west value=1i\ncpu,host=serverA,region=us-west value=2i", + "cpu,host=serverA,region=us-west value=1i\n\ncpu,host=serverA,region=us-west value=2i", + "cpu,host=serverA,region=us-west value=1i\r\ncpu,host=serverA,region=us-west value=2i", + } + for _, s := range ss { + pp, err := models.ParsePointsString(s, "mm") + if err != nil { + t.Errorf(`ParsePoints("%s") mismatch. got %v, exp nil`, s, err) + } + if l := len(pp); l != 2 { + t.Errorf(`ParsePoints("%s") mismatch. got %v, exp 2`, s, l) + } + } +} + func TestParsePointInteger(t *testing.T) { _, err := models.ParsePointsString(`cpu,host=serverA,region=us-west value=1i`, "mm") if err != nil {