Skip to content

Commit

Permalink
Merged in rrk/load-timescaledb-tests (pull request timescale#63)
Browse files Browse the repository at this point in the history
Add tests for tsbs_load_timescaledb scan functions

Approved-by: Lee Hampton <leejhampton@gmail.com>
  • Loading branch information
RobAtticus committed Jun 4, 2018
2 parents d147abc + ecd81c2 commit 26bcc59
Show file tree
Hide file tree
Showing 3 changed files with 126 additions and 7 deletions.
3 changes: 1 addition & 2 deletions cmd/tsbs_load_timescaledb/profile.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,7 @@ func profileCPUAndMem(file string) {
continue
}

f.Write([]byte(fmt.Sprintf("%f,%d,%d,%d\n", cpu, mem.RSS, mem.VMS, mem.Swap)))

fmt.Fprintf(f, "%f,%d,%d,%d\n", cpu, mem.RSS, mem.VMS, mem.Swap)
}
}
}
13 changes: 8 additions & 5 deletions cmd/tsbs_load_timescaledb/scan.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package main

import (
"bufio"
"log"
"strings"

"bitbucket.org/440-labs/tsbs/load"
Expand Down Expand Up @@ -43,27 +42,31 @@ type decoder struct {
scanner *bufio.Scanner
}

const tagsPrefix = "tags"

func (d *decoder) Decode(_ *bufio.Reader) *load.Point {
data := &insertData{}
ok := d.scanner.Scan()
if !ok && d.scanner.Err() == nil { // nothing scanned & no error = EOF
return nil
} else if !ok {
log.Fatalf("scan error: %v", d.scanner.Err())
fatal("scan error: %v", d.scanner.Err())
}

// The first line is a CSV line of tags with the first element being "tags"
parts := strings.SplitN(d.scanner.Text(), ",", 2) // prefix & then rest of line
prefix := parts[0]
if prefix != "tags" {
log.Fatalf("data file in invalid format; got %s expected %s", prefix, "tags")
if prefix != tagsPrefix {
fatal("data file in invalid format; got %s expected %s", prefix, tagsPrefix)
return nil
}
data.tags = parts[1]

// Scan again to get the data line
ok = d.scanner.Scan()
if !ok {
log.Fatalf("scan error: %v", d.scanner.Err())
fatal("scan error: %v", d.scanner.Err())
return nil
}
parts = strings.SplitN(d.scanner.Text(), ",", 2) // prefix & then rest of line
prefix = parts[0]
Expand Down
117 changes: 117 additions & 0 deletions cmd/tsbs_load_timescaledb/scan_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
package main

import (
"bufio"
"bytes"
"fmt"
"log"
"testing"

"bitbucket.org/440-labs/tsbs/load"
)

func TestHypertableArr(t *testing.T) {
f := &factory{}
ha := f.New().(*hypertableArr)
if ha.Len() != 0 {
t.Errorf("hypertableArr not initialized with count 0")
}
p := &load.Point{
Data: &point{
hypertable: "table1",
row: &insertData{
tags: "t1,t2",
fields: "0,f1,f2",
},
},
}
ha.Append(p)
if ha.Len() != 1 {
t.Errorf("hypertableArr count is not 1 after first append")
}
p = &load.Point{
Data: &point{
hypertable: "table2",
row: &insertData{
tags: "t3,t4",
fields: "1,f3,f4",
},
},
}
ha.Append(p)
if ha.Len() != 2 {
t.Errorf("hypertableArr count is not 2 after 2nd append")
}
if len(ha.m) != 2 {
t.Errorf("hypertableArr does not have 2 different hypertables")
}
}

func TestDecode(t *testing.T) {
cases := []struct {
desc string
input string
wantPrefix string
wantFields string
wantTags string
shouldFatal bool
}{
{
desc: "correct input",
input: "tags,tag1text,tag2text\ncpu,140,0.0,0.0\n",
wantPrefix: "cpu",
wantFields: "140,0.0,0.0",
wantTags: "tag1text,tag2text",
},
{
desc: "incorrect tags prefix",
input: "foo,bar,baz\ncpu,140,0.0,0.0\n",
shouldFatal: true,
},
{
desc: "missing values line",
input: "tags,tag1text,tag2text",
shouldFatal: true,
},
}
for _, c := range cases {
br := bufio.NewReader(bytes.NewReader([]byte(c.input)))
decoder := &decoder{scanner: bufio.NewScanner(br)}
if c.shouldFatal {
fmt.Println(c.desc)
isCalled := false
fatal = func(fmt string, args ...interface{}) {
isCalled = true
log.Printf(fmt, args...)
}
_ = decoder.Decode(br)
if !isCalled {
t.Errorf("%s: did not call fatal when it should", c.desc)
}
} else {
p := decoder.Decode(br)
data := p.Data.(*point)
if data.hypertable != c.wantPrefix {
t.Errorf("%s: incorrect prefix: got %s want %s", c.desc, data.hypertable, c.wantPrefix)
}
if data.row.fields != c.wantFields {
t.Errorf("%s: incorrect fields: got %s want %s", c.desc, data.row.fields, c.wantFields)
}
if data.row.tags != c.wantTags {
t.Errorf("%s: incorrect tags: got %s want %s", c.desc, data.row.tags, c.wantTags)
}
}
}
}

func TestDecodeEOF(t *testing.T) {
input := []byte("tags,tag1text,tag2text\ncpu,140,0.0,0.0\n")
br := bufio.NewReader(bytes.NewReader([]byte(input)))
decoder := &decoder{scanner: bufio.NewScanner(br)}
_ = decoder.Decode(br)
// nothing left, should be EOF
p := decoder.Decode(br)
if p != nil {
t.Errorf("expected p to be nil, got %v", p)
}
}

0 comments on commit 26bcc59

Please sign in to comment.