expiration_cache_test.go 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. /*
  2. Copyright 2014 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. "reflect"
  16. "testing"
  17. "time"
  18. "k8s.io/kubernetes/pkg/util/clock"
  19. "k8s.io/kubernetes/pkg/util/sets"
  20. "k8s.io/kubernetes/pkg/util/wait"
  21. )
  22. func TestTTLExpirationBasic(t *testing.T) {
  23. testObj := testStoreObject{id: "foo", val: "bar"}
  24. deleteChan := make(chan string, 1)
  25. ttlStore := NewFakeExpirationStore(
  26. testStoreKeyFunc, deleteChan,
  27. &FakeExpirationPolicy{
  28. NeverExpire: sets.NewString(),
  29. RetrieveKeyFunc: func(obj interface{}) (string, error) {
  30. return obj.(*timestampedEntry).obj.(testStoreObject).id, nil
  31. },
  32. },
  33. clock.RealClock{},
  34. )
  35. err := ttlStore.Add(testObj)
  36. if err != nil {
  37. t.Errorf("Unable to add obj %#v", testObj)
  38. }
  39. item, exists, err := ttlStore.Get(testObj)
  40. if err != nil {
  41. t.Errorf("Failed to get from store, %v", err)
  42. }
  43. if exists || item != nil {
  44. t.Errorf("Got unexpected item %#v", item)
  45. }
  46. key, _ := testStoreKeyFunc(testObj)
  47. select {
  48. case delKey := <-deleteChan:
  49. if delKey != key {
  50. t.Errorf("Unexpected delete for key %s", key)
  51. }
  52. case <-time.After(wait.ForeverTestTimeout):
  53. t.Errorf("Unexpected timeout waiting on delete")
  54. }
  55. close(deleteChan)
  56. }
  57. func TestReAddExpiredItem(t *testing.T) {
  58. deleteChan := make(chan string, 1)
  59. exp := &FakeExpirationPolicy{
  60. NeverExpire: sets.NewString(),
  61. RetrieveKeyFunc: func(obj interface{}) (string, error) {
  62. return obj.(*timestampedEntry).obj.(testStoreObject).id, nil
  63. },
  64. }
  65. ttlStore := NewFakeExpirationStore(
  66. testStoreKeyFunc, deleteChan, exp, clock.RealClock{})
  67. testKey := "foo"
  68. testObj := testStoreObject{id: testKey, val: "bar"}
  69. err := ttlStore.Add(testObj)
  70. if err != nil {
  71. t.Errorf("Unable to add obj %#v", testObj)
  72. }
  73. // This get will expire the item.
  74. item, exists, err := ttlStore.Get(testObj)
  75. if err != nil {
  76. t.Errorf("Failed to get from store, %v", err)
  77. }
  78. if exists || item != nil {
  79. t.Errorf("Got unexpected item %#v", item)
  80. }
  81. key, _ := testStoreKeyFunc(testObj)
  82. differentValue := "different_bar"
  83. err = ttlStore.Add(
  84. testStoreObject{id: testKey, val: differentValue})
  85. if err != nil {
  86. t.Errorf("Failed to add second value")
  87. }
  88. select {
  89. case delKey := <-deleteChan:
  90. if delKey != key {
  91. t.Errorf("Unexpected delete for key %s", key)
  92. }
  93. case <-time.After(wait.ForeverTestTimeout):
  94. t.Errorf("Unexpected timeout waiting on delete")
  95. }
  96. exp.NeverExpire = sets.NewString(testKey)
  97. item, exists, err = ttlStore.GetByKey(testKey)
  98. if err != nil {
  99. t.Errorf("Failed to get from store, %v", err)
  100. }
  101. if !exists || item == nil || item.(testStoreObject).val != differentValue {
  102. t.Errorf("Got unexpected item %#v", item)
  103. }
  104. close(deleteChan)
  105. }
  106. func TestTTLList(t *testing.T) {
  107. testObjs := []testStoreObject{
  108. {id: "foo", val: "bar"},
  109. {id: "foo1", val: "bar1"},
  110. {id: "foo2", val: "bar2"},
  111. }
  112. expireKeys := sets.NewString(testObjs[0].id, testObjs[2].id)
  113. deleteChan := make(chan string, len(testObjs))
  114. defer close(deleteChan)
  115. ttlStore := NewFakeExpirationStore(
  116. testStoreKeyFunc, deleteChan,
  117. &FakeExpirationPolicy{
  118. NeverExpire: sets.NewString(testObjs[1].id),
  119. RetrieveKeyFunc: func(obj interface{}) (string, error) {
  120. return obj.(*timestampedEntry).obj.(testStoreObject).id, nil
  121. },
  122. },
  123. clock.RealClock{},
  124. )
  125. for _, obj := range testObjs {
  126. err := ttlStore.Add(obj)
  127. if err != nil {
  128. t.Errorf("Unable to add obj %#v", obj)
  129. }
  130. }
  131. listObjs := ttlStore.List()
  132. if len(listObjs) != 1 || !reflect.DeepEqual(listObjs[0], testObjs[1]) {
  133. t.Errorf("List returned unexpected results %#v", listObjs)
  134. }
  135. // Make sure all our deletes come through in an acceptable rate (1/100ms)
  136. for expireKeys.Len() != 0 {
  137. select {
  138. case delKey := <-deleteChan:
  139. if !expireKeys.Has(delKey) {
  140. t.Errorf("Unexpected delete for key %s", delKey)
  141. }
  142. expireKeys.Delete(delKey)
  143. case <-time.After(wait.ForeverTestTimeout):
  144. t.Errorf("Unexpected timeout waiting on delete")
  145. return
  146. }
  147. }
  148. }
  149. func TestTTLPolicy(t *testing.T) {
  150. fakeTime := time.Date(2009, time.November, 10, 23, 0, 0, 0, time.UTC)
  151. ttl := 30 * time.Second
  152. exactlyOnTTL := fakeTime.Add(-ttl)
  153. expiredTime := fakeTime.Add(-(ttl + 1))
  154. policy := TTLPolicy{ttl, clock.NewFakeClock(fakeTime)}
  155. fakeTimestampedEntry := &timestampedEntry{obj: struct{}{}, timestamp: exactlyOnTTL}
  156. if policy.IsExpired(fakeTimestampedEntry) {
  157. t.Errorf("TTL cache should not expire entries exactly on ttl")
  158. }
  159. fakeTimestampedEntry.timestamp = fakeTime
  160. if policy.IsExpired(fakeTimestampedEntry) {
  161. t.Errorf("TTL Cache should not expire entries before ttl")
  162. }
  163. fakeTimestampedEntry.timestamp = expiredTime
  164. if !policy.IsExpired(fakeTimestampedEntry) {
  165. t.Errorf("TTL Cache should expire entries older than ttl")
  166. }
  167. for _, ttl = range []time.Duration{0, -1} {
  168. policy.Ttl = ttl
  169. if policy.IsExpired(fakeTimestampedEntry) {
  170. t.Errorf("TTL policy should only expire entries when initialized with a ttl > 0")
  171. }
  172. }
  173. }