cache_test.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. /*
  2. Copyright © The ESO 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. https://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. "sync"
  16. "sync/atomic"
  17. "testing"
  18. "github.com/stretchr/testify/assert"
  19. )
  20. type client struct {
  21. id int
  22. }
  23. var cacheKey = Key{Name: "foo"}
  24. func TestCacheAdd(t *testing.T) {
  25. c, err := New[client](1, nil)
  26. if err != nil {
  27. t.Fail()
  28. }
  29. cl := client{}
  30. c.Add("", cacheKey, cl)
  31. cachedVal, _ := c.Get("", cacheKey)
  32. assert.EqualValues(t, cl, cachedVal)
  33. }
  34. func TestCacheContains(t *testing.T) {
  35. c, err := New[client](1, nil)
  36. if err != nil {
  37. t.Fail()
  38. }
  39. cl := client{}
  40. c.Add("", cacheKey, cl)
  41. exists := c.Contains(cacheKey)
  42. notExists := c.Contains(Key{Name: "does not exist"})
  43. assert.True(t, exists)
  44. assert.False(t, notExists)
  45. assert.Nil(t, err)
  46. }
  47. func TestCacheContainsOrAdd(t *testing.T) {
  48. c := Must[client](1, nil)
  49. first := client{id: 1}
  50. second := client{id: 2}
  51. assert.False(t, c.ContainsOrAdd("v1", cacheKey, first))
  52. assert.True(t, c.ContainsOrAdd("v1", cacheKey, second))
  53. cached, ok := c.Get("v1", cacheKey)
  54. assert.True(t, ok)
  55. assert.Equal(t, first, cached)
  56. }
  57. func TestCacheContainsOrAddVersionMismatch(t *testing.T) {
  58. c := Must[client](1, nil)
  59. first := client{id: 1}
  60. c.Add("v1", cacheKey, first)
  61. assert.True(t, c.ContainsOrAdd("v2", cacheKey, client{id: 2}))
  62. cached, ok := c.Get("v1", cacheKey)
  63. assert.True(t, ok)
  64. assert.Equal(t, first, cached)
  65. }
  66. func TestCacheContainsOrAddDoesNotCleanUpRejectedValue(t *testing.T) {
  67. var cleaned []client
  68. c := Must(1, func(value client) {
  69. cleaned = append(cleaned, value)
  70. })
  71. first := client{id: 1}
  72. assert.False(t, c.ContainsOrAdd("v1", cacheKey, first))
  73. assert.True(t, c.ContainsOrAdd("v1", cacheKey, client{id: 2}))
  74. assert.Empty(t, cleaned)
  75. assert.False(t, c.ContainsOrAdd("v1", Key{Name: "bar"}, client{id: 3}))
  76. assert.Equal(t, []client{first}, cleaned)
  77. }
  78. func TestCacheContainsOrAddConcurrent(t *testing.T) {
  79. const workers = 64
  80. c := Must[client](workers, nil)
  81. start := make(chan struct{})
  82. var inserted atomic.Int32
  83. var wg sync.WaitGroup
  84. for i := range workers {
  85. wg.Go(func() {
  86. <-start
  87. if !c.ContainsOrAdd("v1", cacheKey, client{id: i}) {
  88. inserted.Add(1)
  89. }
  90. })
  91. }
  92. close(start)
  93. wg.Wait()
  94. assert.EqualValues(t, 1, inserted.Load())
  95. _, ok := c.Get("v1", cacheKey)
  96. assert.True(t, ok)
  97. }
  98. func TestCacheGet(t *testing.T) {
  99. c, err := New[*client](1, nil)
  100. if err != nil {
  101. t.Fail()
  102. }
  103. cachedVal, ok := c.Get("", cacheKey)
  104. assert.Nil(t, cachedVal)
  105. assert.False(t, ok)
  106. }
  107. func TestCacheGetInvalidVersion(t *testing.T) {
  108. var cleanupCalled bool
  109. c, err := New(1, func(*client) {
  110. cleanupCalled = true
  111. })
  112. if err != nil {
  113. t.Fail()
  114. }
  115. cl := &client{}
  116. c.Add("", cacheKey, cl)
  117. cachedVal, ok := c.Get("invalid", cacheKey)
  118. assert.Nil(t, cachedVal)
  119. assert.False(t, ok)
  120. assert.True(t, cleanupCalled)
  121. }
  122. func TestCacheEvict(t *testing.T) {
  123. var cleanupCalled bool
  124. c, err := New(1, func(client) {
  125. cleanupCalled = true
  126. })
  127. if err != nil {
  128. t.Fail()
  129. }
  130. // add first version
  131. c.Add("", Key{Name: "foo"}, client{})
  132. assert.False(t, cleanupCalled)
  133. // adding a second version should evict old one
  134. c.Add("", Key{Name: "bar"}, client{})
  135. assert.True(t, cleanupCalled)
  136. }