distcache_test.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. package distcache
  2. import (
  3. "encoding/gob"
  4. "testing"
  5. "time"
  6. "github.com/bmizerany/assert"
  7. "github.com/grafana/grafana/pkg/services/sqlstore"
  8. "github.com/grafana/grafana/pkg/setting"
  9. )
  10. type CacheableStruct struct {
  11. String string
  12. Int64 int64
  13. }
  14. func init() {
  15. gob.Register(CacheableStruct{})
  16. }
  17. func createTestClient(t *testing.T, opts *setting.CacheOpts, sqlstore *sqlstore.SqlStore) CacheStorage {
  18. t.Helper()
  19. dc := &DistributedCache{
  20. SQLStore: sqlstore,
  21. Cfg: &setting.Cfg{
  22. CacheOptions: opts,
  23. },
  24. }
  25. err := dc.Init()
  26. if err != nil {
  27. t.Fatalf("failed to init client for test. error: %v", err)
  28. }
  29. return dc.Client
  30. }
  31. func TestCachedBasedOnConfig(t *testing.T) {
  32. cfg := setting.NewCfg()
  33. cfg.Load(&setting.CommandLineArgs{
  34. HomePath: "../../../",
  35. })
  36. client := createTestClient(t, cfg.CacheOptions, sqlstore.InitTestDB(t))
  37. runTestsForClient(t, client)
  38. }
  39. func runTestsForClient(t *testing.T, client CacheStorage) {
  40. canPutGetAndDeleteCachedObjects(t, client)
  41. canNotFetchExpiredItems(t, client)
  42. canSetInfiniteCacheExpiration(t, client)
  43. }
  44. func canPutGetAndDeleteCachedObjects(t *testing.T, client CacheStorage) {
  45. cacheableStruct := CacheableStruct{String: "hej", Int64: 2000}
  46. err := client.Set("key", cacheableStruct, 0)
  47. assert.Equal(t, err, nil)
  48. data, err := client.Get("key")
  49. s, ok := data.(CacheableStruct)
  50. assert.Equal(t, ok, true)
  51. assert.Equal(t, s.String, "hej")
  52. assert.Equal(t, s.Int64, int64(2000))
  53. err = client.Delete("key")
  54. assert.Equal(t, err, nil)
  55. _, err = client.Get("key")
  56. assert.Equal(t, err, ErrCacheItemNotFound)
  57. }
  58. func canNotFetchExpiredItems(t *testing.T, client CacheStorage) {
  59. cacheableStruct := CacheableStruct{String: "hej", Int64: 2000}
  60. err := client.Set("key", cacheableStruct, time.Second)
  61. assert.Equal(t, err, nil)
  62. //not sure how this can be avoided when testing redis/memcached :/
  63. <-time.After(time.Second + time.Millisecond)
  64. // should not be able to read that value since its expired
  65. _, err = client.Get("key")
  66. assert.Equal(t, err, ErrCacheItemNotFound)
  67. }
  68. func canSetInfiniteCacheExpiration(t *testing.T, client CacheStorage) {
  69. cacheableStruct := CacheableStruct{String: "hej", Int64: 2000}
  70. // insert cache item one day back
  71. getTime = func() time.Time { return time.Now().AddDate(0, 0, -2) }
  72. err := client.Set("key", cacheableStruct, 0)
  73. assert.Equal(t, err, nil)
  74. // should not be able to read that value since its expired
  75. getTime = time.Now
  76. data, err := client.Get("key")
  77. s, ok := data.(CacheableStruct)
  78. assert.Equal(t, ok, true)
  79. assert.Equal(t, s.String, "hej")
  80. assert.Equal(t, s.Int64, int64(2000))
  81. }