cache_test.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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. "testing"
  16. )
  17. const (
  18. maxTestCacheSize int = shardsCount * 2
  19. )
  20. func ExpectEntry(t *testing.T, cache Cache, index uint64, expectedValue interface{}) bool {
  21. elem, found := cache.Get(index)
  22. if !found {
  23. t.Errorf("Expected to find entry with key %d", index)
  24. return false
  25. } else if elem != expectedValue {
  26. t.Errorf("Expected to find %v, got %v", expectedValue, elem)
  27. return false
  28. }
  29. return true
  30. }
  31. func TestBasic(t *testing.T) {
  32. cache := NewCache(maxTestCacheSize)
  33. cache.Add(1, "xxx")
  34. ExpectEntry(t, cache, 1, "xxx")
  35. }
  36. func TestOverflow(t *testing.T) {
  37. cache := NewCache(maxTestCacheSize)
  38. for i := 0; i < maxTestCacheSize+1; i++ {
  39. cache.Add(uint64(i), "xxx")
  40. }
  41. foundIndexes := make([]uint64, 0)
  42. for i := 0; i < maxTestCacheSize+1; i++ {
  43. _, found := cache.Get(uint64(i))
  44. if found {
  45. foundIndexes = append(foundIndexes, uint64(i))
  46. }
  47. }
  48. if len(foundIndexes) != maxTestCacheSize {
  49. t.Errorf("Expect to find %d elements, got %d %v", maxTestCacheSize, len(foundIndexes), foundIndexes)
  50. }
  51. }
  52. func TestOverwrite(t *testing.T) {
  53. cache := NewCache(maxTestCacheSize)
  54. cache.Add(1, "xxx")
  55. ExpectEntry(t, cache, 1, "xxx")
  56. cache.Add(1, "yyy")
  57. ExpectEntry(t, cache, 1, "yyy")
  58. }
  59. // TestEvict this test will fail sporatically depending on what add()
  60. // selects for the randomKey to be evicted. Ensure that randomKey
  61. // is never the key we most recently added. Since the chance of failure
  62. // on each evict is 50%, if we do it 7 times, it should catch the problem
  63. // if it exists >99% of the time.
  64. func TestEvict(t *testing.T) {
  65. cache := NewCache(shardsCount)
  66. var found bool
  67. for retry := 0; retry < 7; retry++ {
  68. cache.Add(uint64(shardsCount), "xxx")
  69. found = ExpectEntry(t, cache, uint64(shardsCount), "xxx")
  70. if !found {
  71. break
  72. }
  73. cache.Add(0, "xxx")
  74. found = ExpectEntry(t, cache, 0, "xxx")
  75. if !found {
  76. break
  77. }
  78. }
  79. }