distcache_test.go 2.5 KB

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