cache_test.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. // Copyright 2022-2023 EMQ Technologies Co., Ltd.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package cache
  15. import (
  16. "reflect"
  17. "testing"
  18. "time"
  19. "github.com/benbjohnson/clock"
  20. "github.com/lf-edge/ekuiper/internal/conf"
  21. "github.com/lf-edge/ekuiper/pkg/api"
  22. )
  23. func TestExpiration(t *testing.T) {
  24. c := NewCache(20, false)
  25. defer c.Close()
  26. clock := conf.Clock.(*clock.Mock)
  27. expects := [][]api.SourceTuple{
  28. {api.NewDefaultSourceTupleWithTime(map[string]interface{}{"a": 1}, nil, clock.Now())},
  29. {api.NewDefaultSourceTupleWithTime(map[string]interface{}{"a": 2}, nil, clock.Now()), api.NewDefaultSourceTupleWithTime(map[string]interface{}{"a": 3}, nil, clock.Now())},
  30. {},
  31. }
  32. c.Set("a", expects[0])
  33. clock.Add(10 * time.Second)
  34. c.Set("b", expects[1])
  35. c.Set("c", expects[2])
  36. clock.Add(1 * time.Second)
  37. r1, ok := c.Get("a")
  38. if !ok {
  39. t.Error("a should exist")
  40. return
  41. }
  42. if !reflect.DeepEqual(r1, expects[0]) {
  43. t.Errorf("expect %v but get %v", expects[0], r1)
  44. }
  45. r2, ok := c.Get("b")
  46. if !ok {
  47. t.Error("b should exist")
  48. return
  49. }
  50. if !reflect.DeepEqual(r2, expects[1]) {
  51. t.Errorf("expect %v but get %v", expects[1], r2)
  52. }
  53. _, ok = c.Get("c")
  54. if ok {
  55. t.Error("c should not exist")
  56. return
  57. }
  58. clock.Add(10 * time.Second)
  59. _, ok = c.Get("a")
  60. if ok {
  61. t.Error("a should not exist after expiration")
  62. return
  63. }
  64. r2, ok = c.Get("b")
  65. if !ok {
  66. t.Error("b should exist")
  67. return
  68. }
  69. if !reflect.DeepEqual(r2, expects[1]) {
  70. t.Errorf("expect %v but get %v", expects[1], r2)
  71. }
  72. clock.Add(10 * time.Second)
  73. _, ok = c.Get("b")
  74. if ok {
  75. t.Error("b should not exist after expiration")
  76. return
  77. }
  78. }
  79. func TestNoExpiration(t *testing.T) {
  80. c := NewCache(0, true)
  81. defer c.Close()
  82. clock := conf.Clock.(*clock.Mock)
  83. expects := [][]api.SourceTuple{
  84. {api.NewDefaultSourceTupleWithTime(map[string]interface{}{"a": 1}, nil, clock.Now())},
  85. {api.NewDefaultSourceTupleWithTime(map[string]interface{}{"a": 2}, nil, clock.Now()), api.NewDefaultSourceTupleWithTime(map[string]interface{}{"a": 3}, nil, clock.Now())},
  86. {},
  87. }
  88. c.Set("a", expects[0])
  89. clock.Add(10 * time.Second)
  90. c.Set("b", expects[1])
  91. c.Set("c", expects[2])
  92. clock.Add(100 * time.Second)
  93. r1, ok := c.Get("a")
  94. if !ok {
  95. t.Error("a should exist")
  96. return
  97. }
  98. if !reflect.DeepEqual(r1, expects[0]) {
  99. t.Errorf("expect %v but get %v", expects[0], r1)
  100. }
  101. r2, ok := c.Get("b")
  102. if !ok {
  103. t.Error("b should exist")
  104. return
  105. }
  106. if !reflect.DeepEqual(r2, expects[1]) {
  107. t.Errorf("expect %v but get %v", expects[1], r2)
  108. }
  109. _, ok = c.Get("c")
  110. if !ok {
  111. t.Error("c should exist")
  112. return
  113. }
  114. }