File tree Expand file tree Collapse file tree 7 files changed +60
-128
lines changed Expand file tree Collapse file tree 7 files changed +60
-128
lines changed Original file line number Diff line number Diff line change 11package diskcache
22
33import (
4- "bytes"
54 "io/ioutil"
65 "os"
76 "testing"
7+
8+ "github.com/gregjones/httpcache/test"
89)
910
1011func 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}
Original file line number Diff line number Diff line change 11package leveldbcache
22
33import (
4- "bytes"
54 "io/ioutil"
65 "os"
76 "path/filepath"
87 "testing"
8+
9+ "github.com/gregjones/httpcache/test"
910)
1011
1112func 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}
Original file line number Diff line number Diff line change 33package memcache
44
55import (
6- "bytes"
76 "testing"
87
98 "appengine/aetest"
9+ "github.com/gregjones/httpcache/test"
1010)
1111
1212func 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}
Original file line number Diff line number Diff line change 33package memcache
44
55import (
6- "bytes"
76 "net"
87 "testing"
8+
9+ "github.com/gregjones/httpcache/test"
910)
1011
1112const 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}
Original file line number Diff line number Diff line change 11package redis
22
33import (
4- "bytes"
54 "testing"
65
76 "github.com/gomodule/redigo/redis"
7+ "github.com/gregjones/httpcache/test"
88)
99
1010func 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}
Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change 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+ }
You can’t perform that action at this time.
0 commit comments