From 3befbb6ad0cc97d4c25d851e9528915809e1a22f Mon Sep 17 00:00:00 2001 From: "W. Trevor King" Date: Tue, 12 Feb 2019 13:27:10 -0800 Subject: [PATCH] test: Add a helper for testing cache implementations (#91) This DRYs things up a bit (and gives me a single place to edit the tests when I get around to streaming caches). --- diskcache/diskcache_test.go | 29 +++---------------------- leveldbcache/leveldbcache_test.go | 27 +++--------------------- memcache/appengine_test.go | 28 ++----------------------- memcache/memcache_test.go | 29 +++---------------------- redis/redis_test.go | 28 ++----------------------- test/test.go | 35 +++++++++++++++++++++++++++++++ test/test_test.go | 12 +++++++++++ 7 files changed, 60 insertions(+), 128 deletions(-) create mode 100644 test/test.go create mode 100644 test/test_test.go diff --git a/diskcache/diskcache_test.go b/diskcache/diskcache_test.go index 35c76cb..98e168b 100644 --- a/diskcache/diskcache_test.go +++ b/diskcache/diskcache_test.go @@ -1,10 +1,11 @@ package diskcache import ( - "bytes" "io/ioutil" "os" "testing" + + "github.com/gregjones/httpcache/test" ) func TestDiskCache(t *testing.T) { @@ -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)) } diff --git a/leveldbcache/leveldbcache_test.go b/leveldbcache/leveldbcache_test.go index b885c01..e301e7b 100644 --- a/leveldbcache/leveldbcache_test.go +++ b/leveldbcache/leveldbcache_test.go @@ -1,11 +1,12 @@ package leveldbcache import ( - "bytes" "io/ioutil" "os" "path/filepath" "testing" + + "github.com/gregjones/httpcache/test" ) func TestDiskCache(t *testing.T) { @@ -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) } diff --git a/memcache/appengine_test.go b/memcache/appengine_test.go index 818b277..f46c379 100644 --- a/memcache/appengine_test.go +++ b/memcache/appengine_test.go @@ -3,10 +3,10 @@ package memcache import ( - "bytes" "testing" "appengine/aetest" + "github.com/gregjones/httpcache/test" ) func TestAppEngine(t *testing.T) { @@ -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)) } diff --git a/memcache/memcache_test.go b/memcache/memcache_test.go index 4dcc547..cce9e02 100644 --- a/memcache/memcache_test.go +++ b/memcache/memcache_test.go @@ -3,9 +3,10 @@ package memcache import ( - "bytes" "net" "testing" + + "github.com/gregjones/httpcache/test" ) const testServer = "localhost:11211" @@ -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)) } diff --git a/redis/redis_test.go b/redis/redis_test.go index 56f7dd5..800863c 100644 --- a/redis/redis_test.go +++ b/redis/redis_test.go @@ -1,10 +1,10 @@ package redis import ( - "bytes" "testing" "github.com/gomodule/redigo/redis" + "github.com/gregjones/httpcache/test" ) func TestRedisCache(t *testing.T) { @@ -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)) } diff --git a/test/test.go b/test/test.go new file mode 100644 index 0000000..c2ff257 --- /dev/null +++ b/test/test.go @@ -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") + } +} diff --git a/test/test_test.go b/test/test_test.go new file mode 100644 index 0000000..41a9c20 --- /dev/null +++ b/test/test_test.go @@ -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()) +}