Permalink
Browse files

make test more useful

  • Loading branch information...
1 parent 1b8360b commit 181a67993fd7864b297ef5a755d8679d2419839b @cyberdelia committed Feb 17, 2012
Showing with 22 additions and 12 deletions.
  1. +22 −12 statsd_test.go
View
@@ -4,6 +4,7 @@ import (
"github.com/bmizerany/assert"
"io"
"testing"
+ "bufio"
"bytes"
)
@@ -12,36 +13,45 @@ type ReadWriter struct {
io.Writer
}
-func fake() (io.ReadWriter, *bytes.Buffer) {
+func fake() (io.ReadWriter) {
buffer := bytes.NewBufferString("")
- rd := new(bytes.Buffer)
- return &ReadWriter{rd, buffer}, buffer
+ return &ReadWriter{buffer, buffer}
+}
+
+func readData(rw *bufio.ReadWriter) string {
+ rw.Flush()
+ data, _ , _ := rw.ReadLine()
+ return string(data)
}
func TestIncrement(t *testing.T) {
- rw, _ := fake()
- c := newClient("<fake>", rw)
+ c := newClient("<fake>", fake())
err := c.Increment("incr", 1, 1)
assert.Equal(t, err, nil)
+ data := readData(c.rw)
+ assert.Equal(t, data, "incr:1|c")
}
func TestDecrement(t *testing.T) {
- rw, _ := fake()
- c := newClient("<fake>", rw)
+ c := newClient("<fake>", fake())
err := c.Decrement("decr", 1, 1)
assert.Equal(t, err, nil)
+ data := readData(c.rw)
+ assert.Equal(t, data, "decr:-1|c")
}
func TestTiming(t *testing.T) {
- rw, _ := fake()
- c := newClient("<fake>", rw)
+ c := newClient("<fake>", fake())
err := c.Timing("time", 350, 1)
assert.Equal(t, err, nil)
+ data := readData(c.rw)
+ assert.Equal(t, data, "time:350|ms")
}
func TestIncrementRate(t *testing.T) {
- rw, _ := fake()
- c := newClient("<fake>", rw)
+ c := newClient("<fake>", fake())
err := c.Increment("incr", 1, 0.99)
- assert.Equal(t, err, nil)
+ assert.Equal(t, err, nil)
+ data := readData(c.rw)
+ assert.Equal(t, data, "incr:1|c|@0.99")
}

0 comments on commit 181a679

Please sign in to comment.