redis_test.go 824 B

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. package redis
  2. import (
  3. "bytes"
  4. "testing"
  5. "github.com/garyburd/redigo/redis"
  6. )
  7. func TestRedisCache(t *testing.T) {
  8. conn, err := redis.Dial("tcp", "localhost:6379")
  9. if err != nil {
  10. // TODO: rather than skip the test, fall back to a faked redis server
  11. t.Skipf("skipping test; no server running at localhost:6379")
  12. }
  13. conn.Do("FLUSHALL")
  14. cache := NewWithClient(conn)
  15. key := "testKey"
  16. _, ok := cache.Get(key)
  17. if ok {
  18. t.Fatal("retrieved key before adding it")
  19. }
  20. val := []byte("some bytes")
  21. cache.Set(key, val)
  22. retVal, ok := cache.Get(key)
  23. if !ok {
  24. t.Fatal("could not retrieve an element we just added")
  25. }
  26. if !bytes.Equal(retVal, val) {
  27. t.Fatal("retrieved a different value than what we put in")
  28. }
  29. cache.Delete(key)
  30. _, ok = cache.Get(key)
  31. if ok {
  32. t.Fatal("deleted key still present")
  33. }
  34. }