Add tests for runner.go

pull/3798/head
Michael Desa 2015-08-21 23:56:18 -07:00
parent 35a0477f05
commit d63ac6a842
1 changed files with 173 additions and 0 deletions

View File

@ -0,0 +1,173 @@
package runner_test
import (
"encoding/json"
"net/http"
"net/http/httptest"
"testing"
"time"
"github.com/influxdb/influxdb/client"
"github.com/influxdb/influxdb/cmd/influx_stress/runner"
)
func TestTimer_StartTimer(t *testing.T) {
var epoch time.Time
tmr := &runner.Timer{}
tmr.StartTimer()
s := tmr.Start()
if s == epoch {
t.Errorf("expected tmr.start to not be %v", s)
}
}
func TestNewTimer(t *testing.T) {
var epoch time.Time
tmr := runner.NewTimer()
s := tmr.Start()
if s == epoch {
t.Errorf("expected tmr.start to not be %v", s)
}
e := tmr.End()
if e != epoch {
t.Errorf("expected tmr.stop to be %v, got %v", epoch, e)
}
}
func TestTimer_StopTimer(t *testing.T) {
var epoch time.Time
tmr := runner.NewTimer()
tmr.StopTimer()
e := tmr.End()
if e == epoch {
t.Errorf("expected tmr.stop to not be %v", e)
}
}
func TestTimer_Elapsed(t *testing.T) {
tmr := runner.NewTimer()
time.Sleep(2 * time.Second)
tmr.StopTimer()
e := tmr.Elapsed()
if time.Duration(2*time.Second) > e || e > time.Duration(3*time.Second) {
t.Errorf("expected around %s got %s", time.Duration(2*time.Second), e)
}
}
func TestNewResponseTime(t *testing.T) {
r := runner.NewResponseTime(100)
if r.Value != 100 {
t.Errorf("expected Value to be %v, got %v", 100, r.Value)
}
var epoch time.Time
if r.Time == epoch {
t.Errorf("expected r.Time not to be %v", epoch)
}
}
func TestConfig_newClient(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("X-Influxdb-Version", "x.x")
return
}))
defer ts.Close()
url := ts.URL[7:]
cfg := &runner.Config{
BatchSize: 5000,
SeriesCount: 10000,
PointCount: 100,
Concurrency: 10,
BatchInterval: time.Duration(0),
Database: "stress",
Address: url,
}
// the client.NewClient method in the influxdb go
// client never returns an error that is not nil.
// To test that the client actually gets created without
// any error, I run the Ping() method to verify that everything
// is okay.
c, err := cfg.NewClient()
// This err will never be nil. See the following URL:
// https://github.com/influxdb/influxdb/blob/master/client/influxdb.go#L119
if err != nil {
t.Error(err)
}
// Verify that the client actually gets created correctly
d, version, err := c.Ping()
if err != nil {
t.Fatalf("unexpected error. expected %v, actual %v", nil, err)
}
if d == 0 {
t.Fatalf("expected a duration greater than zero. actual %v", d)
}
if version != "x.x" {
t.Fatalf("unexpected version. expected %s, actual %v", "x.x", version)
}
}
func TestRun(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
var data client.Response
w.WriteHeader(http.StatusOK)
_ = json.NewEncoder(w).Encode(data)
}))
defer ts.Close()
url := ts.URL[7:]
cfg := &runner.Config{
BatchSize: 5000,
SeriesCount: 10000,
PointCount: 100,
Concurrency: 10,
BatchInterval: time.Duration(0),
Database: "stress",
Address: url,
}
tp, rts, tmr := runner.Run(cfg)
if tp != cfg.SeriesCount*cfg.PointCount {
t.Fatalf("unexpected error. expected %v, actual %v", 1000000, tp)
}
if len(rts) != cfg.SeriesCount*cfg.PointCount/cfg.BatchSize {
t.Fatalf("unexpected error. expected %v, actual %v", 1000000, len(rts))
}
var epoch time.Time
if tmr.Start() == epoch {
t.Errorf("expected trm.start not to be %s", epoch)
}
if tmr.End() == epoch {
t.Errorf("expected trm.end not to be %s", epoch)
}
}