z_test.go.tmp 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. package cache
  2. import (
  3. "log"
  4. "testing"
  5. "time"
  6. )
  7. func TestMap(t *testing.T) {
  8. m := NewMap(time.Second*5, 10)
  9. m.Set("key", 10)
  10. log.Println(m.Get("key"))
  11. m.Delete("key")
  12. log.Println(m.Get("key"))
  13. log.Println(m.Get("key1"))
  14. go func() {
  15. for e := range m.CleanEvent() {
  16. log.Println("CLEANED", e)
  17. }
  18. }()
  19. for i := 0; i < 5; i++ {
  20. time.Sleep(time.Second * 10)
  21. m.Set("key", "oneee")
  22. }
  23. }
  24. func TestMapCheck(t *testing.T) {
  25. m := NewMap(time.Second*20, 0)
  26. m.Set("key", 10)
  27. log.Println(m.GetCheck("key", func(key, value interface{}, exists bool) (rKey, rVal interface{}, needUpdate bool) {
  28. log.Println("MCHECK", key, value, exists)
  29. rVal = "Cool!!!"
  30. rKey, needUpdate = "key1", true
  31. return
  32. }))
  33. log.Println("===================")
  34. log.Println(m.Get("key"))
  35. log.Println(m.Get("key1"))
  36. log.Println(m.GetCreateNew("key", func(key interface{}) (rKey, val interface{}, created bool) {
  37. return "oooone", 1000, true
  38. }))
  39. /*
  40. // test cleaner
  41. for {
  42. log.Println("enter")
  43. m.GetCheck("key", func(key, value interface{}, exists bool) (rKey, rVal interface{}, needUpdate bool) {
  44. log.Println("Program", key, value, exists)
  45. if exists {
  46. needUpdate = false
  47. }
  48. rKey = key
  49. rVal = 100
  50. needUpdate = true
  51. return
  52. })
  53. time.Sleep(time.Second * 10)
  54. }
  55. */
  56. }
  57. func TestMapEach(t *testing.T) {
  58. m := NewMap(time.Second*5, 10)
  59. m.SetMulti(map[interface{}]interface{}{
  60. 1: "one",
  61. 2: "two",
  62. 3: "three",
  63. })
  64. m.Each(func(key, val interface{}) bool {
  65. log.Println(key, val)
  66. return false
  67. })
  68. }
  69. func TestKeys(t *testing.T) {
  70. m := NewMap(time.Second*5, 20)
  71. m.SetMulti(map[interface{}]interface{}{
  72. "one": 1,
  73. "two": 2,
  74. })
  75. log.Println(m.Keys())
  76. m.Clear()
  77. log.Println(m.Keys())
  78. }
  79. func TestCleaner(t *testing.T) {
  80. m := NewMap(time.Second*5, 20)
  81. m.SetMulti(map[interface{}]interface{}{
  82. "one": 1,
  83. "two": 2,
  84. })
  85. time.Sleep(time.Second * 60)
  86. }