Skip to content
This repository has been archived by the owner on Apr 20, 2023. It is now read-only.

Commit

Permalink
test: Add a helper for testing cache implementations (#91)
Browse files Browse the repository at this point in the history
This DRYs things up a bit (and gives me a single place to edit the
tests when I get around to streaming caches).
  • Loading branch information
wking authored and gregjones committed Feb 12, 2019
1 parent 7a90257 commit 3befbb6
Show file tree
Hide file tree
Showing 7 changed files with 60 additions and 128 deletions.
29 changes: 3 additions & 26 deletions diskcache/diskcache_test.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package diskcache

import (
"bytes"
"io/ioutil"
"os"
"testing"

"github.com/gregjones/httpcache/test"
)

func TestDiskCache(t *testing.T) {
Expand All @@ -14,29 +15,5 @@ func TestDiskCache(t *testing.T) {
}
defer os.RemoveAll(tempDir)

cache := New(tempDir)

key := "testKey"
_, ok := cache.Get(key)
if ok {
t.Fatal("retrieved key before adding it")
}

val := []byte("some bytes")
cache.Set(key, val)

retVal, ok := cache.Get(key)
if !ok {
t.Fatal("could not retrieve an element we just added")
}
if !bytes.Equal(retVal, val) {
t.Fatal("retrieved a different value than what we put in")
}

cache.Delete(key)

_, ok = cache.Get(key)
if ok {
t.Fatal("deleted key still present")
}
test.Cache(t, New(tempDir))
}
27 changes: 3 additions & 24 deletions leveldbcache/leveldbcache_test.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
package leveldbcache

import (
"bytes"
"io/ioutil"
"os"
"path/filepath"
"testing"

"github.com/gregjones/httpcache/test"
)

func TestDiskCache(t *testing.T) {
Expand All @@ -20,27 +21,5 @@ func TestDiskCache(t *testing.T) {
t.Fatalf("New leveldb,: %v", err)
}

key := "testKey"
_, ok := cache.Get(key)
if ok {
t.Fatal("retrieved key before adding it")
}

val := []byte("some bytes")
cache.Set(key, val)

retVal, ok := cache.Get(key)
if !ok {
t.Fatal("could not retrieve an element we just added")
}
if !bytes.Equal(retVal, val) {
t.Fatal("retrieved a different value than what we put in")
}

cache.Delete(key)

_, ok = cache.Get(key)
if ok {
t.Fatal("deleted key still present")
}
test.Cache(t, cache)
}
28 changes: 2 additions & 26 deletions memcache/appengine_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
package memcache

import (
"bytes"
"testing"

"appengine/aetest"
"github.com/gregjones/httpcache/test"
)

func TestAppEngine(t *testing.T) {
Expand All @@ -16,29 +16,5 @@ func TestAppEngine(t *testing.T) {
}
defer ctx.Close()

cache := New(ctx)

key := "testKey"
_, ok := cache.Get(key)
if ok {
t.Fatal("retrieved key before adding it")
}

val := []byte("some bytes")
cache.Set(key, val)

retVal, ok := cache.Get(key)
if !ok {
t.Fatal("could not retrieve an element we just added")
}
if !bytes.Equal(retVal, val) {
t.Fatal("retrieved a different value than what we put in")
}

cache.Delete(key)

_, ok = cache.Get(key)
if ok {
t.Fatal("deleted key still present")
}
test.Cache(t, New(ctx))
}
29 changes: 3 additions & 26 deletions memcache/memcache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@
package memcache

import (
"bytes"
"net"
"testing"

"github.com/gregjones/httpcache/test"
)

const testServer = "localhost:11211"
Expand All @@ -19,29 +20,5 @@ func TestMemCache(t *testing.T) {
conn.Write([]byte("flush_all\r\n")) // flush memcache
conn.Close()

cache := New(testServer)

key := "testKey"
_, ok := cache.Get(key)
if ok {
t.Fatal("retrieved key before adding it")
}

val := []byte("some bytes")
cache.Set(key, val)

retVal, ok := cache.Get(key)
if !ok {
t.Fatal("could not retrieve an element we just added")
}
if !bytes.Equal(retVal, val) {
t.Fatal("retrieved a different value than what we put in")
}

cache.Delete(key)

_, ok = cache.Get(key)
if ok {
t.Fatal("deleted key still present")
}
test.Cache(t, New(testServer))
}
28 changes: 2 additions & 26 deletions redis/redis_test.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package redis

import (
"bytes"
"testing"

"github.com/gomodule/redigo/redis"
"github.com/gregjones/httpcache/test"
)

func TestRedisCache(t *testing.T) {
Expand All @@ -15,29 +15,5 @@ func TestRedisCache(t *testing.T) {
}
conn.Do("FLUSHALL")

cache := NewWithClient(conn)

key := "testKey"
_, ok := cache.Get(key)
if ok {
t.Fatal("retrieved key before adding it")
}

val := []byte("some bytes")
cache.Set(key, val)

retVal, ok := cache.Get(key)
if !ok {
t.Fatal("could not retrieve an element we just added")
}
if !bytes.Equal(retVal, val) {
t.Fatal("retrieved a different value than what we put in")
}

cache.Delete(key)

_, ok = cache.Get(key)
if ok {
t.Fatal("deleted key still present")
}
test.Cache(t, NewWithClient(conn))
}
35 changes: 35 additions & 0 deletions test/test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package test

import (
"bytes"
"testing"

"github.com/gregjones/httpcache"
)

// Cache excercises a httpcache.Cache implementation.
func Cache(t *testing.T, cache httpcache.Cache) {
key := "testKey"
_, ok := cache.Get(key)
if ok {
t.Fatal("retrieved key before adding it")
}

val := []byte("some bytes")
cache.Set(key, val)

retVal, ok := cache.Get(key)
if !ok {
t.Fatal("could not retrieve an element we just added")
}
if !bytes.Equal(retVal, val) {
t.Fatal("retrieved a different value than what we put in")
}

cache.Delete(key)

_, ok = cache.Get(key)
if ok {
t.Fatal("deleted key still present")
}
}
12 changes: 12 additions & 0 deletions test/test_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package test_test

import (
"testing"

"github.com/gregjones/httpcache"
"github.com/gregjones/httpcache/test"
)

func TestMemoryCache(t *testing.T) {
test.Cache(t, httpcache.NewMemoryCache())
}

0 comments on commit 3befbb6

Please sign in to comment.