memcache_test.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. package cache
  2. import (
  3. "testing"
  4. )
  5. type simpleStruct struct {
  6. int
  7. string
  8. }
  9. type complexStruct struct {
  10. int
  11. simpleStruct
  12. }
  13. var getTests = []struct {
  14. name string
  15. keyToAdd string
  16. keyToGet string
  17. expectedOk bool
  18. }{
  19. {"string_hit", "myKey", "myKey", true},
  20. {"string_miss", "myKey", "nonsense", false},
  21. }
  22. func TestSet(t *testing.T) {
  23. var cache Cache
  24. cache = NewMemCache(0)
  25. values := []string{"test1", "test2", "test3"}
  26. key := "key1"
  27. for _, v := range values {
  28. cache.Set(key, v)
  29. val, ok := cache.Get(key)
  30. if !ok{
  31. t.Fatalf("expect key:%v ,value:%v", key, v)
  32. } else if ok && val != v {
  33. t.Fatalf("expect value:%v, get value:%v", key, v, val)
  34. }
  35. t.Logf("value:%v ", val)
  36. }
  37. }
  38. func TestGet(t *testing.T) {
  39. var cache Cache
  40. cache = NewMemCache(0)
  41. for _, tt := range getTests {
  42. cache.Set(tt.keyToAdd, 1234)
  43. val, ok := cache.Get(tt.keyToGet)
  44. if ok != tt.expectedOk {
  45. t.Fatalf("%s: val:%v cache hit = %v; want %v", tt.name, val, ok, !ok)
  46. } else if ok && val != 1234 {
  47. t.Fatalf("%s expected get to return 1234 but got %v", tt.name, val)
  48. }
  49. }
  50. }
  51. func TestDelete(t *testing.T) {
  52. var cache Cache
  53. cache = NewMemCache(0)
  54. cache.Set("myKey", 1234)
  55. if val, ok := cache.Get("myKey"); !ok {
  56. t.Fatal("TestRemove returned no match")
  57. } else if val != 1234 {
  58. t.Fatalf("TestRemove failed. Expected %d, got %v", 1234, val)
  59. }
  60. cache.Delete("myKey")
  61. if _, ok := cache.Get("myKey"); ok {
  62. t.Fatal("TestRemove returned a removed item")
  63. }
  64. }
  65. func TestStatus(t *testing.T) {
  66. keys := []string{"1", "2", "3", "4", "5"}
  67. var gets int64
  68. var hits int64
  69. var maxSize int
  70. var currentSize int
  71. maxSize = 20
  72. var cache Cache
  73. cache = NewMemCache(maxSize)
  74. //keys := []string{"1", "2", "3", "4", "5"}
  75. for _, key := range keys {
  76. cache.Set(key, 1234)
  77. currentSize++
  78. }
  79. newKeys := []string{"1", "2", "3", "4", "5", "6", "7", "8", "9", "10"}
  80. for _, newKey := range newKeys {
  81. _, ok := cache.Get(newKey)
  82. if ok == true {
  83. hits++
  84. }
  85. gets++
  86. }
  87. t.Logf("gets:%v, hits:%v, maxSize:%v, currentSize:%v", gets, hits, maxSize, currentSize)
  88. status := cache.Status()
  89. if status.CurrentSize != currentSize || status.MaxItemSize != maxSize ||
  90. status.Gets != gets || status.Hits != hits {
  91. t.Fatalf("get status maxSize:%v, currentSize:%v, nget:%v, nhit:%v",
  92. status.MaxItemSize, status.CurrentSize, status.Gets, status.Hits)
  93. }
  94. }
  95. func TestLRU(t *testing.T) {
  96. keys := []string{"1", "2", "3", "4", "2", "1", "3", "5", "6", "5", "6"}
  97. maxSize := 3
  98. var cache Cache
  99. cache = NewMemCache(maxSize)
  100. for i, key := range keys {
  101. cache.Set(key, 1234)
  102. if i == 3 {
  103. status := cache.Status()
  104. if status.CurrentSize != maxSize {
  105. t.Fatalf("expected maxSize %v,currentSize:%v", maxSize, status.CurrentSize)
  106. }
  107. _, ok1 := cache.Get("2")
  108. _, ok2 := cache.Get("3")
  109. _, ok3 := cache.Get("4")
  110. if !(ok1 && ok2 && ok3) {
  111. t.Fatalf("expected remains key 2:%v,3:%v, 4:%v", ok1, ok2, ok3)
  112. }
  113. }
  114. }
  115. status := cache.Status()
  116. if status.CurrentSize != maxSize {
  117. t.Fatalf("expected maxSize %v,currentSize:%v", maxSize, status.CurrentSize)
  118. }
  119. _, ok1 := cache.Get("3")
  120. _, ok2 := cache.Get("5")
  121. _, ok3 := cache.Get("6")
  122. if !(ok1 && ok2 && ok3) {
  123. t.Fatalf("expected remains key 3:%v,5:%v, 6:%v", ok1, ok2, ok3)
  124. }
  125. }