lookupsource_test.go 4.5 KB

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