2019-05-22 21:16:55 +00:00
|
|
|
package graphite
|
|
|
|
|
|
|
|
import (
|
2020-07-08 10:59:19 +00:00
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/fasttime"
|
2019-05-22 21:16:55 +00:00
|
|
|
"reflect"
|
2020-07-08 10:59:19 +00:00
|
|
|
"strings"
|
2019-05-22 21:16:55 +00:00
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestRowsUnmarshalFailure(t *testing.T) {
|
|
|
|
f := func(s string) {
|
|
|
|
t.Helper()
|
|
|
|
var rows Rows
|
2019-08-24 09:42:59 +00:00
|
|
|
rows.Unmarshal(s)
|
|
|
|
if len(rows.Rows) != 0 {
|
|
|
|
t.Fatalf("unexpected number of rows parsed; got %d; want 0", len(rows.Rows))
|
2019-05-22 21:16:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Try again
|
2019-08-24 09:42:59 +00:00
|
|
|
rows.Unmarshal(s)
|
|
|
|
if len(rows.Rows) != 0 {
|
|
|
|
t.Fatalf("unexpected number of rows parsed; got %d; want 0", len(rows.Rows))
|
2019-05-22 21:16:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-24 10:35:29 +00:00
|
|
|
// Missing metric
|
|
|
|
f(" 123 455")
|
|
|
|
|
2019-05-22 21:16:55 +00:00
|
|
|
// Missing value
|
|
|
|
f("aaa")
|
|
|
|
|
|
|
|
// missing tag
|
|
|
|
f("aa; 12 34")
|
|
|
|
|
|
|
|
// missing tag value
|
|
|
|
f("aa;bb 23 34")
|
2020-09-15 22:33:32 +00:00
|
|
|
|
|
|
|
// unexpected space in tag value
|
|
|
|
// See https://github.com/VictoriaMetrics/VictoriaMetrics/issues/99
|
|
|
|
f("s;tag1=aaa1;tag2=bb b2;tag3=ccc3 1")
|
|
|
|
|
|
|
|
// invalid value
|
|
|
|
f("aa bb")
|
|
|
|
|
|
|
|
// invalid timestamp
|
|
|
|
f("aa 123 bar")
|
2019-05-22 21:16:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestRowsUnmarshalSuccess(t *testing.T) {
|
|
|
|
f := func(s string, rowsExpected *Rows) {
|
|
|
|
t.Helper()
|
|
|
|
var rows Rows
|
2019-08-24 09:42:59 +00:00
|
|
|
rows.Unmarshal(s)
|
2019-05-22 21:16:55 +00:00
|
|
|
if !reflect.DeepEqual(rows.Rows, rowsExpected.Rows) {
|
|
|
|
t.Fatalf("unexpected rows;\ngot\n%+v;\nwant\n%+v", rows.Rows, rowsExpected.Rows)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Try unmarshaling again
|
2019-08-24 09:42:59 +00:00
|
|
|
rows.Unmarshal(s)
|
2019-05-22 21:16:55 +00:00
|
|
|
if !reflect.DeepEqual(rows.Rows, rowsExpected.Rows) {
|
|
|
|
t.Fatalf("unexpected rows;\ngot\n%+v;\nwant\n%+v", rows.Rows, rowsExpected.Rows)
|
|
|
|
}
|
|
|
|
|
|
|
|
rows.Reset()
|
|
|
|
if len(rows.Rows) != 0 {
|
|
|
|
t.Fatalf("non-empty rows after reset: %+v", rows.Rows)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Empty line
|
|
|
|
f("", &Rows{})
|
2019-08-24 09:42:59 +00:00
|
|
|
f("\r", &Rows{})
|
2019-05-22 21:16:55 +00:00
|
|
|
f("\n\n", &Rows{})
|
2019-08-24 09:42:59 +00:00
|
|
|
f("\n\r\n", &Rows{})
|
2019-05-22 21:16:55 +00:00
|
|
|
|
|
|
|
// Single line
|
|
|
|
f("foobar -123.456 789", &Rows{
|
|
|
|
Rows: []Row{{
|
|
|
|
Metric: "foobar",
|
|
|
|
Value: -123.456,
|
|
|
|
Timestamp: 789,
|
|
|
|
}},
|
|
|
|
})
|
|
|
|
f("foo.bar 123.456 789\n", &Rows{
|
|
|
|
Rows: []Row{{
|
|
|
|
Metric: "foo.bar",
|
|
|
|
Value: 123.456,
|
|
|
|
Timestamp: 789,
|
|
|
|
}},
|
|
|
|
})
|
|
|
|
|
2019-06-18 16:04:02 +00:00
|
|
|
// Missing timestamp
|
|
|
|
f("aaa 1123", &Rows{
|
|
|
|
Rows: []Row{{
|
|
|
|
Metric: "aaa",
|
|
|
|
Value: 1123,
|
|
|
|
}},
|
|
|
|
})
|
2020-07-08 11:10:31 +00:00
|
|
|
f("aaa 1123 -1", &Rows{
|
|
|
|
Rows: []Row{{
|
2020-07-08 11:12:10 +00:00
|
|
|
Metric: "aaa",
|
|
|
|
Value: 1123,
|
2020-07-08 11:10:31 +00:00
|
|
|
Timestamp: -1,
|
|
|
|
}},
|
|
|
|
})
|
2019-06-18 16:04:02 +00:00
|
|
|
|
2019-10-17 15:22:56 +00:00
|
|
|
// Timestamp bigger than 1<<31
|
|
|
|
f("aaa 1123 429496729600", &Rows{
|
|
|
|
Rows: []Row{{
|
2019-10-17 17:04:26 +00:00
|
|
|
Metric: "aaa",
|
|
|
|
Value: 1123,
|
2019-10-17 15:22:56 +00:00
|
|
|
Timestamp: 429496729600,
|
|
|
|
}},
|
|
|
|
})
|
|
|
|
|
2019-05-22 21:16:55 +00:00
|
|
|
// Tags
|
|
|
|
f("foo;bar=baz 1 2", &Rows{
|
|
|
|
Rows: []Row{{
|
|
|
|
Metric: "foo",
|
|
|
|
Tags: []Tag{{
|
|
|
|
Key: "bar",
|
|
|
|
Value: "baz",
|
|
|
|
}},
|
|
|
|
Value: 1,
|
|
|
|
Timestamp: 2,
|
|
|
|
}},
|
|
|
|
})
|
2019-08-24 10:35:29 +00:00
|
|
|
// Empty tags
|
|
|
|
f("foo;bar=baz;aa=;x=y;=z 1 2", &Rows{
|
2019-05-22 21:16:55 +00:00
|
|
|
Rows: []Row{{
|
|
|
|
Metric: "foo",
|
|
|
|
Tags: []Tag{
|
|
|
|
{
|
|
|
|
Key: "bar",
|
|
|
|
Value: "baz",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Key: "x",
|
|
|
|
Value: "y",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Value: 1,
|
|
|
|
Timestamp: 2,
|
|
|
|
}},
|
|
|
|
})
|
|
|
|
|
|
|
|
// Multi lines
|
2019-06-18 16:04:02 +00:00
|
|
|
f("foo 0.3 2\naaa 3\nbar.baz 0.34 43\n", &Rows{
|
2019-05-22 21:16:55 +00:00
|
|
|
Rows: []Row{
|
|
|
|
{
|
|
|
|
Metric: "foo",
|
|
|
|
Value: 0.3,
|
|
|
|
Timestamp: 2,
|
|
|
|
},
|
2019-06-18 16:04:02 +00:00
|
|
|
{
|
|
|
|
Metric: "aaa",
|
|
|
|
Value: 3,
|
|
|
|
},
|
2019-05-22 21:16:55 +00:00
|
|
|
{
|
|
|
|
Metric: "bar.baz",
|
|
|
|
Value: 0.34,
|
|
|
|
Timestamp: 43,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
2019-08-24 09:42:59 +00:00
|
|
|
|
|
|
|
// Multi lines with invalid line
|
|
|
|
f("foo 0.3 2\naaa\nbar.baz 0.34 43\n", &Rows{
|
|
|
|
Rows: []Row{
|
|
|
|
{
|
|
|
|
Metric: "foo",
|
|
|
|
Value: 0.3,
|
|
|
|
Timestamp: 2,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Metric: "bar.baz",
|
|
|
|
Value: 0.34,
|
|
|
|
Timestamp: 43,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
2019-05-22 21:16:55 +00:00
|
|
|
}
|
2020-07-08 10:59:19 +00:00
|
|
|
|
|
|
|
func Test_streamContext_Read(t *testing.T) {
|
|
|
|
f := func(s string, rowsExpected *Rows) {
|
|
|
|
t.Helper()
|
|
|
|
ctx := &streamContext{}
|
|
|
|
ctx.Read(strings.NewReader(s))
|
|
|
|
if len(ctx.Rows.Rows) != len(rowsExpected.Rows) {
|
|
|
|
t.Fatalf("different len of expected rows;\ngot\n%+v;\nwant\n%+v", ctx.Rows, rowsExpected.Rows)
|
|
|
|
}
|
|
|
|
if !reflect.DeepEqual(ctx.Rows.Rows, rowsExpected.Rows) {
|
|
|
|
t.Fatalf("unexpected rows;\ngot\n%+v;\nwant\n%+v", ctx.Rows.Rows, rowsExpected.Rows)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-08 11:10:31 +00:00
|
|
|
// Full line without tags
|
|
|
|
f("aaa 1123 345", &Rows{
|
|
|
|
Rows: []Row{{
|
|
|
|
Metric: "aaa",
|
|
|
|
Value: 1123,
|
2020-07-08 11:12:10 +00:00
|
|
|
Timestamp: 345 * 1000,
|
2020-07-08 11:10:31 +00:00
|
|
|
}},
|
|
|
|
})
|
|
|
|
// Full line with tags
|
|
|
|
f("aaa;x=y 1123 345", &Rows{
|
|
|
|
Rows: []Row{{
|
2020-07-08 11:12:10 +00:00
|
|
|
Metric: "aaa",
|
2020-07-08 11:10:31 +00:00
|
|
|
Tags: []Tag{{
|
2020-07-08 11:12:10 +00:00
|
|
|
Key: "x",
|
2020-07-08 11:10:31 +00:00
|
|
|
Value: "y",
|
|
|
|
}},
|
|
|
|
Value: 1123,
|
2020-07-08 11:12:10 +00:00
|
|
|
Timestamp: 345 * 1000,
|
2020-07-08 11:10:31 +00:00
|
|
|
}},
|
|
|
|
})
|
|
|
|
// missing timestamp.
|
|
|
|
// Note that this test may be flaky due to timing issues. TODO: fix it
|
2020-07-08 10:59:19 +00:00
|
|
|
f("aaa 1123", &Rows{
|
|
|
|
Rows: []Row{{
|
|
|
|
Metric: "aaa",
|
|
|
|
Value: 1123,
|
2020-07-08 11:10:31 +00:00
|
|
|
Timestamp: int64(fasttime.UnixTimestamp()) * 1000,
|
|
|
|
}},
|
|
|
|
})
|
|
|
|
// -1 timestamp. See https://github.com/VictoriaMetrics/VictoriaMetrics/issues/610
|
|
|
|
// Note that this test may be flaky due to timing issues. TODO: fix it.
|
|
|
|
f("aaa 1123 -1", &Rows{
|
|
|
|
Rows: []Row{{
|
|
|
|
Metric: "aaa",
|
|
|
|
Value: 1123,
|
|
|
|
Timestamp: int64(fasttime.UnixTimestamp()) * 1000,
|
2020-07-08 10:59:19 +00:00
|
|
|
}},
|
|
|
|
})
|
|
|
|
}
|