lookupsource_test.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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 memory
  15. import (
  16. gocontext "context"
  17. "reflect"
  18. "testing"
  19. "time"
  20. "github.com/benbjohnson/clock"
  21. "github.com/lf-edge/ekuiper/internal/conf"
  22. "github.com/lf-edge/ekuiper/internal/io/memory/pubsub"
  23. "github.com/lf-edge/ekuiper/internal/topo/context"
  24. "github.com/lf-edge/ekuiper/pkg/api"
  25. )
  26. func TestUpdateLookup(t *testing.T) {
  27. contextLogger := conf.Log.WithField("rule", "test")
  28. ctx := context.WithValue(context.Background(), context.LoggerKey, contextLogger)
  29. ls := GetLookupSource()
  30. err := ls.Configure("test", map[string]interface{}{"key": "ff"})
  31. if err != nil {
  32. t.Error(err)
  33. return
  34. }
  35. err = ls.Open(ctx)
  36. if err != nil {
  37. t.Error(err)
  38. return
  39. }
  40. // wait for the source to be ready
  41. time.Sleep(100 * time.Millisecond)
  42. pubsub.Produce(ctx, "test", map[string]interface{}{"ff": "value1", "gg": "value2"})
  43. pubsub.ProduceUpdatable(ctx, "test", map[string]interface{}{"ff": "value1", "gg": "value2"}, "delete", "value1")
  44. pubsub.ProduceUpdatable(ctx, "test", map[string]interface{}{"ff": "value2", "gg": "value2"}, "insert", "value2")
  45. pubsub.Produce(ctx, "test", map[string]interface{}{"ff": "value1", "gg": "value4"})
  46. pubsub.ProduceUpdatable(ctx, "test", map[string]interface{}{"ff": "value2", "gg": "value2"}, "delete", "value2")
  47. pubsub.Produce(ctx, "test", map[string]interface{}{"ff": "value1", "gg": "value2"})
  48. pubsub.Produce(ctx, "test", map[string]interface{}{"ff": "value2", "gg": "value2"})
  49. // wait for table accumulation
  50. time.Sleep(100 * time.Millisecond)
  51. canctx, cancel := gocontext.WithCancel(gocontext.Background())
  52. defer cancel()
  53. go func() {
  54. for {
  55. select {
  56. case <-canctx.Done():
  57. return
  58. case <-time.After(10 * time.Millisecond):
  59. pubsub.Produce(ctx, "test", map[string]interface{}{"ff": "value4", "gg": "value2"})
  60. }
  61. }
  62. }()
  63. mc := conf.Clock.(*clock.Mock)
  64. expected := []api.SourceTuple{
  65. api.NewDefaultSourceTupleWithTime(map[string]interface{}{"ff": "value1", "gg": "value2"}, map[string]interface{}{"topic": "test"}, mc.Now()),
  66. }
  67. result, err := ls.Lookup(ctx, []string{}, []string{"ff"}, []interface{}{"value1"})
  68. if err != nil {
  69. t.Error(err)
  70. return
  71. }
  72. if !reflect.DeepEqual(result, expected) {
  73. t.Errorf("expect %v but got %v", expected, result)
  74. }
  75. err = ls.Close(ctx)
  76. if err != nil {
  77. t.Error(err)
  78. return
  79. }
  80. }
  81. func TestLookup(t *testing.T) {
  82. contextLogger := conf.Log.WithField("rule", "test2")
  83. ctx := context.WithValue(context.Background(), context.LoggerKey, contextLogger)
  84. ls := GetLookupSource()
  85. err := ls.Configure("test2", map[string]interface{}{"key": "gg"})
  86. if err != nil {
  87. t.Error(err)
  88. return
  89. }
  90. err = ls.Open(ctx)
  91. if err != nil {
  92. t.Error(err)
  93. return
  94. }
  95. // wait for the source to be ready
  96. time.Sleep(100 * time.Millisecond)
  97. pubsub.Produce(ctx, "test2", map[string]interface{}{"ff": "value1", "gg": "value2"})
  98. pubsub.Produce(ctx, "test2", map[string]interface{}{"ff": "value2", "gg": "value3"})
  99. pubsub.Produce(ctx, "test2", map[string]interface{}{"ff": "value1", "gg": "value4"})
  100. // wait for table accumulation
  101. time.Sleep(100 * time.Millisecond)
  102. canctx, cancel := gocontext.WithCancel(gocontext.Background())
  103. defer cancel()
  104. go func() {
  105. for {
  106. select {
  107. case <-canctx.Done():
  108. return
  109. case <-time.After(10 * time.Millisecond):
  110. pubsub.Produce(ctx, "test", map[string]interface{}{"ff": "value4", "gg": "value5"})
  111. }
  112. }
  113. }()
  114. mc := conf.Clock.(*clock.Mock)
  115. expected := []api.SourceTuple{
  116. api.NewDefaultSourceTupleWithTime(map[string]interface{}{"ff": "value1", "gg": "value2"}, map[string]interface{}{"topic": "test2"}, mc.Now()),
  117. api.NewDefaultSourceTupleWithTime(map[string]interface{}{"ff": "value1", "gg": "value4"}, map[string]interface{}{"topic": "test2"}, mc.Now()),
  118. }
  119. result, _ := ls.Lookup(ctx, []string{}, []string{"ff"}, []interface{}{"value1"})
  120. if len(result) != 2 {
  121. t.Errorf("expect %v but got %v", expected, result)
  122. } else {
  123. if result[0].Message()["gg"] != "value2" {
  124. result[0], result[1] = result[1], result[0]
  125. }
  126. }
  127. if !reflect.DeepEqual(result, expected) {
  128. t.Errorf("expect %v but got %v", expected, result)
  129. }
  130. err = ls.Close(ctx)
  131. if err != nil {
  132. t.Error(err)
  133. return
  134. }
  135. }