mapcache_test.go 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. /*
  2. * Public Domain Software
  3. *
  4. * I (Matthias Ladkau) am the author of the source code in this file.
  5. * I have placed the source code in this file in the public domain.
  6. *
  7. * For further information see: http://creativecommons.org/publicdomain/zero/1.0/
  8. */
  9. package datautil
  10. import (
  11. "testing"
  12. "time"
  13. )
  14. func TestMapCache(t *testing.T) {
  15. // Create a map cache which can hold a maximum of 3 items for no longer than
  16. // 5 seconds
  17. mc := NewMapCache(3, 5)
  18. mc.Put("k1", "aaa")
  19. mc.Put("k2", "bbb")
  20. mc.Put("k3", "ccc")
  21. if s := mc.Size(); s != 3 {
  22. t.Error("Unexpected size:", s)
  23. return
  24. }
  25. mc.Clear()
  26. if s := mc.Size(); s != 0 {
  27. t.Error("Unexpected size:", s)
  28. return
  29. }
  30. mc.Put("k1", "aaa")
  31. mc.Put("k2", "bbb")
  32. mc.Put("k3", "ccc")
  33. if s := mc.Size(); s != 3 {
  34. t.Error("Unexpected size:", s)
  35. return
  36. }
  37. // Test copy
  38. cp := mc.GetAll()
  39. if len(cp) != 3 {
  40. t.Error("Unexpected copy result:", cp)
  41. return
  42. }
  43. // Simulate different timings
  44. mc.ts["k1"] = time.Now().Unix() - 6 // Expired
  45. mc.ts["k2"] = time.Now().Unix() - 3 // Oldest entry
  46. if mc.String() != `
  47. k1:aaa
  48. k2:bbb
  49. k3:ccc
  50. `[1:] {
  51. t.Error("Unexpected cache content:", mc)
  52. return
  53. }
  54. // Do a read operation on an expired entry
  55. if e, ok := mc.Get("k1"); e != nil || ok {
  56. t.Error("Expired entry should not be returned", ok, e)
  57. return
  58. }
  59. if mc.String() != `
  60. k2:bbb
  61. k3:ccc
  62. `[1:] {
  63. t.Error("Unexpected cache content:", mc)
  64. return
  65. }
  66. // Do a read operation on a live entry
  67. if e, ok := mc.Get("k2"); e != "bbb" || !ok {
  68. t.Error("Live entry should be returned", ok, e)
  69. return
  70. }
  71. if mc.String() != `
  72. k2:bbb
  73. k3:ccc
  74. `[1:] {
  75. t.Error("Unexpected cache content:", mc)
  76. return
  77. }
  78. // Add 1 entry and update another
  79. mc.Put("k3", "updateccc")
  80. mc.Put("k4", "ddd")
  81. if mc.String() != `
  82. k2:bbb
  83. k3:updateccc
  84. k4:ddd
  85. `[1:] {
  86. t.Error("Unexpected cache content:", mc)
  87. return
  88. }
  89. // Add another entry which should push out the oldest
  90. mc.Put("k5", "eee")
  91. if mc.String() != `
  92. k3:updateccc
  93. k4:ddd
  94. k5:eee
  95. `[1:] {
  96. t.Error("Unexpected cache content:", mc)
  97. return
  98. }
  99. // Remove items
  100. if !mc.Remove("k3") {
  101. t.Error("Live item should be deleted")
  102. return
  103. }
  104. if mc.String() != `
  105. k4:ddd
  106. k5:eee
  107. `[1:] {
  108. t.Error("Unexpected cache content:", mc)
  109. return
  110. }
  111. if mc.Remove("k0") {
  112. t.Error("Removal of non-existing item should not return success")
  113. return
  114. }
  115. }