Skip to content

Commit 3befbb6

Browse files
wkinggregjones
authored andcommitted
test: Add a helper for testing cache implementations (gregjones#91)
This DRYs things up a bit (and gives me a single place to edit the tests when I get around to streaming caches).
1 parent 7a90257 commit 3befbb6

File tree

7 files changed

+60
-128
lines changed

7 files changed

+60
-128
lines changed

‎diskcache/diskcache_test.go‎

Lines changed: 3 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
package diskcache
22

33
import (
4-
"bytes"
54
"io/ioutil"
65
"os"
76
"testing"
7+
8+
"github.com/gregjones/httpcache/test"
89
)
910

1011
func TestDiskCache(t *testing.T) {
@@ -14,29 +15,5 @@ func TestDiskCache(t *testing.T) {
1415
}
1516
defer os.RemoveAll(tempDir)
1617

17-
cache := New(tempDir)
18-
19-
key := "testKey"
20-
_, ok := cache.Get(key)
21-
if ok {
22-
t.Fatal("retrieved key before adding it")
23-
}
24-
25-
val := []byte("some bytes")
26-
cache.Set(key, val)
27-
28-
retVal, ok := cache.Get(key)
29-
if !ok {
30-
t.Fatal("could not retrieve an element we just added")
31-
}
32-
if !bytes.Equal(retVal, val) {
33-
t.Fatal("retrieved a different value than what we put in")
34-
}
35-
36-
cache.Delete(key)
37-
38-
_, ok = cache.Get(key)
39-
if ok {
40-
t.Fatal("deleted key still present")
41-
}
18+
test.Cache(t, New(tempDir))
4219
}

‎leveldbcache/leveldbcache_test.go‎

Lines changed: 3 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
package leveldbcache
22

33
import (
4-
"bytes"
54
"io/ioutil"
65
"os"
76
"path/filepath"
87
"testing"
8+
9+
"github.com/gregjones/httpcache/test"
910
)
1011

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

23-
key := "testKey"
24-
_, ok := cache.Get(key)
25-
if ok {
26-
t.Fatal("retrieved key before adding it")
27-
}
28-
29-
val := []byte("some bytes")
30-
cache.Set(key, val)
31-
32-
retVal, ok := cache.Get(key)
33-
if !ok {
34-
t.Fatal("could not retrieve an element we just added")
35-
}
36-
if !bytes.Equal(retVal, val) {
37-
t.Fatal("retrieved a different value than what we put in")
38-
}
39-
40-
cache.Delete(key)
41-
42-
_, ok = cache.Get(key)
43-
if ok {
44-
t.Fatal("deleted key still present")
45-
}
24+
test.Cache(t, cache)
4625
}

‎memcache/appengine_test.go‎

Lines changed: 2 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@
33
package memcache
44

55
import (
6-
"bytes"
76
"testing"
87

98
"appengine/aetest"
9+
"github.com/gregjones/httpcache/test"
1010
)
1111

1212
func TestAppEngine(t *testing.T) {
@@ -16,29 +16,5 @@ func TestAppEngine(t *testing.T) {
1616
}
1717
defer ctx.Close()
1818

19-
cache := New(ctx)
20-
21-
key := "testKey"
22-
_, ok := cache.Get(key)
23-
if ok {
24-
t.Fatal("retrieved key before adding it")
25-
}
26-
27-
val := []byte("some bytes")
28-
cache.Set(key, val)
29-
30-
retVal, ok := cache.Get(key)
31-
if !ok {
32-
t.Fatal("could not retrieve an element we just added")
33-
}
34-
if !bytes.Equal(retVal, val) {
35-
t.Fatal("retrieved a different value than what we put in")
36-
}
37-
38-
cache.Delete(key)
39-
40-
_, ok = cache.Get(key)
41-
if ok {
42-
t.Fatal("deleted key still present")
43-
}
19+
test.Cache(t, New(ctx))
4420
}

‎memcache/memcache_test.go‎

Lines changed: 3 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,10 @@
33
package memcache
44

55
import (
6-
"bytes"
76
"net"
87
"testing"
8+
9+
"github.com/gregjones/httpcache/test"
910
)
1011

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

22-
cache := New(testServer)
23-
24-
key := "testKey"
25-
_, ok := cache.Get(key)
26-
if ok {
27-
t.Fatal("retrieved key before adding it")
28-
}
29-
30-
val := []byte("some bytes")
31-
cache.Set(key, val)
32-
33-
retVal, ok := cache.Get(key)
34-
if !ok {
35-
t.Fatal("could not retrieve an element we just added")
36-
}
37-
if !bytes.Equal(retVal, val) {
38-
t.Fatal("retrieved a different value than what we put in")
39-
}
40-
41-
cache.Delete(key)
42-
43-
_, ok = cache.Get(key)
44-
if ok {
45-
t.Fatal("deleted key still present")
46-
}
23+
test.Cache(t, New(testServer))
4724
}

‎redis/redis_test.go‎

Lines changed: 2 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
package redis
22

33
import (
4-
"bytes"
54
"testing"
65

76
"github.com/gomodule/redigo/redis"
7+
"github.com/gregjones/httpcache/test"
88
)
99

1010
func TestRedisCache(t *testing.T) {
@@ -15,29 +15,5 @@ func TestRedisCache(t *testing.T) {
1515
}
1616
conn.Do("FLUSHALL")
1717

18-
cache := NewWithClient(conn)
19-
20-
key := "testKey"
21-
_, ok := cache.Get(key)
22-
if ok {
23-
t.Fatal("retrieved key before adding it")
24-
}
25-
26-
val := []byte("some bytes")
27-
cache.Set(key, val)
28-
29-
retVal, ok := cache.Get(key)
30-
if !ok {
31-
t.Fatal("could not retrieve an element we just added")
32-
}
33-
if !bytes.Equal(retVal, val) {
34-
t.Fatal("retrieved a different value than what we put in")
35-
}
36-
37-
cache.Delete(key)
38-
39-
_, ok = cache.Get(key)
40-
if ok {
41-
t.Fatal("deleted key still present")
42-
}
18+
test.Cache(t, NewWithClient(conn))
4319
}

‎test/test.go‎

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package test
2+
3+
import (
4+
"bytes"
5+
"testing"
6+
7+
"github.com/gregjones/httpcache"
8+
)
9+
10+
// Cache excercises a httpcache.Cache implementation.
11+
func Cache(t *testing.T, cache httpcache.Cache) {
12+
key := "testKey"
13+
_, ok := cache.Get(key)
14+
if ok {
15+
t.Fatal("retrieved key before adding it")
16+
}
17+
18+
val := []byte("some bytes")
19+
cache.Set(key, val)
20+
21+
retVal, ok := cache.Get(key)
22+
if !ok {
23+
t.Fatal("could not retrieve an element we just added")
24+
}
25+
if !bytes.Equal(retVal, val) {
26+
t.Fatal("retrieved a different value than what we put in")
27+
}
28+
29+
cache.Delete(key)
30+
31+
_, ok = cache.Get(key)
32+
if ok {
33+
t.Fatal("deleted key still present")
34+
}
35+
}

‎test/test_test.go‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package test_test
2+
3+
import (
4+
"testing"
5+
6+
"github.com/gregjones/httpcache"
7+
"github.com/gregjones/httpcache/test"
8+
)
9+
10+
func TestMemoryCache(t *testing.T) {
11+
test.Cache(t, httpcache.NewMemoryCache())
12+
}

0 commit comments

Comments
 (0)