object_cache_test.go 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. /*
  2. Copyright 2016 The Kubernetes Authors.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package cache
  14. import (
  15. "fmt"
  16. "testing"
  17. "time"
  18. "k8s.io/apimachinery/pkg/util/clock"
  19. expirationcache "k8s.io/client-go/tools/cache"
  20. )
  21. type testObject struct {
  22. key string
  23. val string
  24. }
  25. // A fake objectCache for unit test.
  26. func NewFakeObjectCache(f func() (interface{}, error), ttl time.Duration, clock clock.Clock) *ObjectCache {
  27. ttlPolicy := &expirationcache.TTLPolicy{Ttl: ttl, Clock: clock}
  28. deleteChan := make(chan string, 1)
  29. return &ObjectCache{
  30. updater: f,
  31. cache: expirationcache.NewFakeExpirationStore(stringKeyFunc, deleteChan, ttlPolicy, clock),
  32. }
  33. }
  34. func TestAddAndGet(t *testing.T) {
  35. testObj := testObject{
  36. key: "foo",
  37. val: "bar",
  38. }
  39. objectCache := NewFakeObjectCache(func() (interface{}, error) {
  40. return nil, fmt.Errorf("Unexpected Error: updater should never be called in this test")
  41. }, 1*time.Hour, clock.NewFakeClock(time.Now()))
  42. err := objectCache.Add(testObj.key, testObj.val)
  43. if err != nil {
  44. t.Errorf("Unable to add obj %#v by key: %s", testObj, testObj.key)
  45. }
  46. value, err := objectCache.Get(testObj.key)
  47. if err != nil {
  48. t.Errorf("Unable to get obj %#v by key: %s", testObj, testObj.key)
  49. }
  50. if value.(string) != testObj.val {
  51. t.Errorf("Expected to get cached value: %#v, but got: %s", testObj.val, value.(string))
  52. }
  53. }
  54. func TestExpirationBasic(t *testing.T) {
  55. unexpectedVal := "bar"
  56. expectedVal := "bar2"
  57. testObj := testObject{
  58. key: "foo",
  59. val: unexpectedVal,
  60. }
  61. fakeClock := clock.NewFakeClock(time.Now())
  62. objectCache := NewFakeObjectCache(func() (interface{}, error) {
  63. return expectedVal, nil
  64. }, 1*time.Second, fakeClock)
  65. err := objectCache.Add(testObj.key, testObj.val)
  66. if err != nil {
  67. t.Errorf("Unable to add obj %#v by key: %s", testObj, testObj.key)
  68. }
  69. // sleep 2s so cache should be expired.
  70. fakeClock.Sleep(2 * time.Second)
  71. value, err := objectCache.Get(testObj.key)
  72. if err != nil {
  73. t.Errorf("Unable to get obj %#v by key: %s", testObj, testObj.key)
  74. }
  75. if value.(string) != expectedVal {
  76. t.Errorf("Expected to get cached value: %#v, but got: %s", expectedVal, value.(string))
  77. }
  78. }