writebehind

Paddy 2015-03-31

0:85f9751b15ea Go to Latest

writebehind/writebehind_test.go

First pass implementation. Create our first implementation of this. Use a map of string=>int64 to store our values. The idea here is that users will create one cache for each metric they want to collect--each column that needs updating, in MySQL land. Release it under the MIT license, because why not? Very proud that the first commit carries 100% test coverage, no golint errors, no go vet errors, and has a benchmark.

History
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/writebehind_test.go	Tue Mar 31 23:09:02 2015 -0400
     1.3 @@ -0,0 +1,71 @@
     1.4 +package writebehind
     1.5 +
     1.6 +import (
     1.7 +	"encoding/json"
     1.8 +	"testing"
     1.9 +	"time"
    1.10 +)
    1.11 +
    1.12 +func TestSync(t *testing.T) {
    1.13 +	m := MemoryIncrementer{}
    1.14 +	c := NewCache(&m, time.Millisecond)
    1.15 +	defer c.Stop()
    1.16 +	c.Sync() // test that unnecessary sync is a no-op
    1.17 +	c.Increment("test1", 10)
    1.18 +	c.Increment("test2", 100)
    1.19 +	time.Sleep(time.Millisecond)
    1.20 +	if m.Get("test1") != 10 {
    1.21 +		t.Errorf("Expected test1 to be %d, got %d", 10, m.values["test1"])
    1.22 +	}
    1.23 +	if m.Get("test2") != 100 {
    1.24 +		t.Errorf("Expected test2 to be %d, got %d", 100, m.values["test2"])
    1.25 +	}
    1.26 +	c.Increment("test1", 10)
    1.27 +	c.Increment("test2", 100)
    1.28 +	time.Sleep(time.Millisecond)
    1.29 +	if m.Get("test1") != 20 {
    1.30 +		t.Errorf("Expected test1 to be %d, got %d", 20, m.values["test1"])
    1.31 +	}
    1.32 +	if m.Get("test2") != 200 {
    1.33 +		t.Errorf("Expected test2 to be %d, got %d", 200, m.values["test2"])
    1.34 +	}
    1.35 +}
    1.36 +
    1.37 +func TestString(t *testing.T) {
    1.38 +	m := MemoryIncrementer{}
    1.39 +	c := NewCache(&m, time.Millisecond)
    1.40 +	defer c.Stop()
    1.41 +	c.Increment("test1", 10)
    1.42 +	c.Increment("test2", 100)
    1.43 +	c.Increment(`"test3"`, 1000)
    1.44 +	str := c.String()
    1.45 +	var result map[string]int64
    1.46 +	err := json.Unmarshal([]byte(str), &result)
    1.47 +	if err != nil {
    1.48 +		t.Errorf("Error unmarshalling returned JSON: %#+v", err)
    1.49 +	}
    1.50 +	if result["test1"] != 10 {
    1.51 +		t.Errorf("Expected test1 to be %d, got %d", 10, result["test1"])
    1.52 +	}
    1.53 +	if result["test2"] != 100 {
    1.54 +		t.Errorf("Expected test2 to be %d, got %d", 100, result["test2"])
    1.55 +	}
    1.56 +	if result["\"test3\""] != 1000 {
    1.57 +		t.Errorf("Expected \"test3\" to be %d, got %d", 1000, result["\"test3\""])
    1.58 +	}
    1.59 +}
    1.60 +
    1.61 +func BenchmarkSync(b *testing.B) {
    1.62 +	m := MemoryIncrementer{}
    1.63 +	c := NewCache(&m, time.Second*5)
    1.64 +	defer c.Stop()
    1.65 +
    1.66 +	b.RunParallel(func(pb *testing.PB) {
    1.67 +		for pb.Next() {
    1.68 +			c.Increment("myvalue", 1)
    1.69 +			c.Increment("othervalue", 1)
    1.70 +			c.Increment("other other value", 1)
    1.71 +			c.Increment("weird value", 1)
    1.72 +		}
    1.73 +	})
    1.74 +}