summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
-rw-r--r--go.mod1
-rw-r--r--go.sum9
-rw-r--r--storage/dummy.go38
-rw-r--r--storage/dummy_test.go86
4 files changed, 134 insertions, 0 deletions
diff --git a/go.mod b/go.mod
index debb5aa..c026243 100644
--- a/go.mod
+++ b/go.mod
@@ -7,6 +7,7 @@ require (
github.com/go-redis/redis/v7 v7.0.0-beta.4
github.com/golang/protobuf v1.3.1 // indirect
github.com/klauspost/compress v1.9.2 // indirect
+ github.com/stretchr/testify v1.4.0
github.com/twmb/murmur3 v1.0.0
github.com/valyala/fasthttp v1.6.0
github.com/valyala/quicktemplate v1.4.1
diff --git a/go.sum b/go.sum
index a955a71..6c0b253 100644
--- a/go.sum
+++ b/go.sum
@@ -1,3 +1,5 @@
+github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
+github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/fasthttp/router v0.5.2 h1:xdmx8uYc9IFDtlbG2/FhE1Gyowv7/sqMgMonRjoW0Yo=
github.com/fasthttp/router v0.5.2/go.mod h1:Y5JAeRTSPwSLoUgH4x75UnT1j1IcAgVshMDMMrnNmKQ=
github.com/fsnotify/fsnotify v1.4.7 h1:IXs+QLmnXW2CcXuY+8Mzv/fWEsPGWxqefPtCP5CnV9I=
@@ -25,8 +27,13 @@ github.com/onsi/ginkgo v1.8.0 h1:VkHVNpR4iVnU8XQR6DBm8BqYjN7CRzw+xKUbVVbbW9w=
github.com/onsi/ginkgo v1.8.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE=
github.com/onsi/gomega v1.5.0 h1:izbySO9zDPmjJ8rDjLvkA2zJHIo+HkYXHnf7eN7SSyo=
github.com/onsi/gomega v1.5.0/go.mod h1:ex+gbHU/CVuBBDIJjb2X0qEXbFg53c61hWP/1CpauHY=
+github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
+github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/savsgio/gotils v0.0.0-20190925070755-524bc4f47500 h1:9Pi10H7E8E79/x2HSe1FmMGd7BJ1WAqDKzwjpv+ojFg=
github.com/savsgio/gotils v0.0.0-20190925070755-524bc4f47500/go.mod h1:lHhJedqxCoHN+zMtwGNTXWmF0u9Jt363FYRhV6g0CdY=
+github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
+github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJyk=
+github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
github.com/twmb/murmur3 v1.0.0 h1:MLMwMEQRKsu94uJnoveYjjHmcLwI3HNcWXP4LJuNe3I=
github.com/twmb/murmur3 v1.0.0/go.mod h1:5Y5m8Y8WIyucaICVP+Aep5C8ydggjEuRQHDq1icoOYo=
github.com/valyala/bytebufferpool v1.0.0 h1:GqA5TC/0021Y/b9FG4Oi9Mr3q7XYx6KllzawFIhcdPw=
@@ -61,3 +68,5 @@ gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7 h1:uRGJdciOHaEIrze2W8Q3AKkep
gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7/go.mod h1:dt/ZhP58zS4L8KSrWDmTeBkI65Dw0HsyUHuEVlX15mw=
gopkg.in/yaml.v2 v2.2.1 h1:mUhvW9EsL+naU5Q3cakzfE91YhliOondGd6ZrsDBHQE=
gopkg.in/yaml.v2 v2.2.1/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
+gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw=
+gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
diff --git a/storage/dummy.go b/storage/dummy.go
new file mode 100644
index 0000000..3d60921
--- /dev/null
+++ b/storage/dummy.go
@@ -0,0 +1,38 @@
+package storage
+
+// Dummy is a dummy storage device that acts predictably for tests
+type Dummy struct {
+ collide, healthy bool
+ err error
+}
+
+// Create is a noop
+// if the backend was created unhealthy, it will error out with the unhealthy error
+// if the backend was set as colliding, and collision detection is enabled, it will error out with a collision
+// if the backend was set as erroring, it will error with that error
+func (d *Dummy) Create(key, value string, checkcollision bool) error {
+ if !d.Healthy() {
+ return Unhealthy
+ }
+ if checkcollision && d.collide {
+ return Collision
+ }
+ if d.err != nil {
+ return d.err
+ }
+ return nil
+}
+
+// Read will return the key that is sent to it, or error out if the backend was created unhealthy
+// Read echoes the key given to it back
+// if the backend was set to unhealthy, it also returns the Unhealthy error
+func (d *Dummy) Read(key string) (string, error) {
+ var err error
+ if !d.Healthy() {
+ err = Unhealthy
+ }
+ return key, err
+}
+
+// Healthy checks whether or not the backend was created healthy
+func (d *Dummy) Healthy() bool { return d.healthy }
diff --git a/storage/dummy_test.go b/storage/dummy_test.go
new file mode 100644
index 0000000..d5aa664
--- /dev/null
+++ b/storage/dummy_test.go
@@ -0,0 +1,86 @@
+package storage
+
+import (
+ "testing"
+
+ "github.com/stretchr/testify/assert"
+)
+
+func TestDummy(t *testing.T) {
+ assert := assert.New(t)
+
+ const (
+ k = "key"
+ v = "val"
+ e = "key"
+ )
+
+ var (
+ // setup
+ err = Error("testing error")
+ dumC = Dummy{collide: true, healthy: true, err: nil}
+ dumE = Dummy{collide: false, healthy: true, err: err}
+ dumH = Dummy{collide: false, healthy: false, err: nil}
+ dumN = Dummy{collide: false, healthy: true, err: nil}
+
+ // ---- TESTS
+ // Health
+ hc = dumC.Healthy()
+ he = dumE.Healthy()
+ hh = dumH.Healthy()
+ hn = dumN.Healthy()
+
+ // Write - no collisions
+ wc = dumC.Create(k, v, false)
+ we = dumE.Create(k, v, false)
+ wh = dumH.Create(k, v, false)
+ wn = dumN.Create(k, v, false)
+
+ // Write - collisions
+ cc = dumC.Create(k, v, true)
+ ce = dumE.Create(k, v, true)
+ ch = dumH.Create(k, v, true)
+ cn = dumN.Create(k, v, true)
+
+ // Read
+ rc, erc = dumC.Read(k)
+ re, ere = dumE.Read(k)
+ rh, erh = dumH.Read(k)
+ rn, ern = dumN.Read(k)
+ )
+ assert.Equal("testing error", err.Error())
+
+ assert.True(hc)
+ assert.True(he)
+ assert.False(hh)
+ assert.True(hn)
+
+ assert.Nil(wc)
+ assert.NotNil(we)
+ assert.NotNil(wh)
+ assert.Nil(wn)
+
+ assert.Equal(err, we)
+ assert.Equal(Unhealthy, wh)
+
+ assert.NotNil(cc)
+ assert.NotNil(ce)
+ assert.NotNil(ch)
+ assert.Nil(cn)
+
+ assert.Equal(Collision, cc)
+ assert.Equal(err, ce)
+ assert.Equal(Unhealthy, ch)
+
+ assert.Nil(erc)
+ assert.Nil(ere)
+ assert.NotNil(erh)
+ assert.Nil(ern)
+
+ assert.Equal(Unhealthy, erh)
+
+ assert.Equal(e, rc)
+ assert.Equal(e, re)
+ assert.Equal(e, rh)
+ assert.Equal(e, rn)
+}