cache_test.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. package collection
  2. import (
  3. "strconv"
  4. "sync"
  5. "sync/atomic"
  6. "testing"
  7. "time"
  8. "github.com/stretchr/testify/assert"
  9. )
  10. func TestCacheSet(t *testing.T) {
  11. cache, err := NewCache(time.Second*2, WithName("any"))
  12. assert.Nil(t, err)
  13. cache.Set("first", "first element")
  14. cache.Set("second", "second element")
  15. value, ok := cache.Get("first")
  16. assert.True(t, ok)
  17. assert.Equal(t, "first element", value)
  18. value, ok = cache.Get("second")
  19. assert.True(t, ok)
  20. assert.Equal(t, "second element", value)
  21. }
  22. func TestCacheDel(t *testing.T) {
  23. cache, err := NewCache(time.Second * 2)
  24. assert.Nil(t, err)
  25. cache.Set("first", "first element")
  26. cache.Set("second", "second element")
  27. cache.Del("first")
  28. _, ok := cache.Get("first")
  29. assert.False(t, ok)
  30. value, ok := cache.Get("second")
  31. assert.True(t, ok)
  32. assert.Equal(t, "second element", value)
  33. }
  34. func TestCacheTake(t *testing.T) {
  35. cache, err := NewCache(time.Second * 2)
  36. assert.Nil(t, err)
  37. var count int32
  38. var wg sync.WaitGroup
  39. for i := 0; i < 100; i++ {
  40. wg.Add(1)
  41. go func() {
  42. cache.Take("first", func() (interface{}, error) {
  43. atomic.AddInt32(&count, 1)
  44. time.Sleep(time.Millisecond * 100)
  45. return "first element", nil
  46. })
  47. wg.Done()
  48. }()
  49. }
  50. wg.Wait()
  51. assert.Equal(t, 1, cache.size())
  52. assert.Equal(t, int32(1), atomic.LoadInt32(&count))
  53. }
  54. func TestCacheWithLruEvicts(t *testing.T) {
  55. cache, err := NewCache(time.Minute, WithLimit(3))
  56. assert.Nil(t, err)
  57. cache.Set("first", "first element")
  58. cache.Set("second", "second element")
  59. cache.Set("third", "third element")
  60. cache.Set("fourth", "fourth element")
  61. value, ok := cache.Get("first")
  62. assert.False(t, ok)
  63. value, ok = cache.Get("second")
  64. assert.True(t, ok)
  65. assert.Equal(t, "second element", value)
  66. value, ok = cache.Get("third")
  67. assert.True(t, ok)
  68. assert.Equal(t, "third element", value)
  69. value, ok = cache.Get("fourth")
  70. assert.True(t, ok)
  71. assert.Equal(t, "fourth element", value)
  72. }
  73. func TestCacheWithLruEvicted(t *testing.T) {
  74. cache, err := NewCache(time.Minute, WithLimit(3))
  75. assert.Nil(t, err)
  76. cache.Set("first", "first element")
  77. cache.Set("second", "second element")
  78. cache.Set("third", "third element")
  79. cache.Set("fourth", "fourth element")
  80. value, ok := cache.Get("first")
  81. assert.False(t, ok)
  82. value, ok = cache.Get("second")
  83. assert.True(t, ok)
  84. assert.Equal(t, "second element", value)
  85. cache.Set("fifth", "fifth element")
  86. cache.Set("sixth", "sixth element")
  87. _, ok = cache.Get("third")
  88. assert.False(t, ok)
  89. _, ok = cache.Get("fourth")
  90. assert.False(t, ok)
  91. value, ok = cache.Get("second")
  92. assert.True(t, ok)
  93. assert.Equal(t, "second element", value)
  94. }
  95. func BenchmarkCache(b *testing.B) {
  96. cache, err := NewCache(time.Second*5, WithLimit(100000))
  97. if err != nil {
  98. b.Fatal(err)
  99. }
  100. for i := 0; i < 10000; i++ {
  101. for j := 0; j < 10; j++ {
  102. index := strconv.Itoa(i*10000 + j)
  103. cache.Set("key:"+index, "value:"+index)
  104. }
  105. }
  106. time.Sleep(time.Second * 5)
  107. b.RunParallel(func(pb *testing.PB) {
  108. for pb.Next() {
  109. for i := 0; i < b.N; i++ {
  110. index := strconv.Itoa(i % 10000)
  111. cache.Get("key:" + index)
  112. if i%100 == 0 {
  113. cache.Set("key1:"+index, "value1:"+index)
  114. }
  115. }
  116. }
  117. })
  118. }