z_test.go 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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. }