lookupsource_test.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. // Copyright 2022 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/topo/context"
  19. "github.com/lf-edge/ekuiper/internal/topo/memory/pubsub"
  20. "github.com/lf-edge/ekuiper/pkg/api"
  21. "reflect"
  22. "testing"
  23. "time"
  24. )
  25. func TestNoIndexLookup(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{}{"option": "value"})
  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.Produce(ctx, "test", map[string]interface{}{"ff": "value2", "gg": "value2"})
  43. pubsub.Produce(ctx, "test", map[string]interface{}{"ff": "value1", "gg": "value4"})
  44. // wait for table accumulation
  45. time.Sleep(100 * time.Millisecond)
  46. canctx, cancel := gocontext.WithCancel(gocontext.Background())
  47. defer cancel()
  48. go func() {
  49. for {
  50. select {
  51. case <-canctx.Done():
  52. return
  53. case <-time.After(10 * time.Millisecond):
  54. pubsub.Produce(ctx, "test", map[string]interface{}{"ff": "value4", "gg": "value2"})
  55. }
  56. }
  57. }()
  58. expected := []api.SourceTuple{
  59. api.NewDefaultSourceTuple(map[string]interface{}{"ff": "value1", "gg": "value2"}, map[string]interface{}{"topic": "test"}),
  60. api.NewDefaultSourceTuple(map[string]interface{}{"ff": "value1", "gg": "value4"}, map[string]interface{}{"topic": "test"}),
  61. }
  62. result, err := ls.Lookup(ctx, []string{}, []string{"ff"}, []interface{}{"value1"})
  63. if !reflect.DeepEqual(result, expected) {
  64. t.Errorf("expect %v but got %v", expected, result)
  65. }
  66. err = ls.Close(ctx)
  67. if err != nil {
  68. t.Error(err)
  69. return
  70. }
  71. }
  72. func TestSingleIndexLookup(t *testing.T) {
  73. contextLogger := conf.Log.WithField("rule", "test2")
  74. ctx := context.WithValue(context.Background(), context.LoggerKey, contextLogger)
  75. ls := GetLookupSource()
  76. err := ls.Configure("test2", map[string]interface{}{"index": []string{"ff"}})
  77. if err != nil {
  78. t.Error(err)
  79. return
  80. }
  81. err = ls.Open(ctx)
  82. if err != nil {
  83. t.Error(err)
  84. return
  85. }
  86. // wait for the source to be ready
  87. time.Sleep(100 * time.Millisecond)
  88. pubsub.Produce(ctx, "test2", map[string]interface{}{"ff": "value1", "gg": "value2"})
  89. pubsub.Produce(ctx, "test2", map[string]interface{}{"ff": "value2", "gg": "value2"})
  90. pubsub.Produce(ctx, "test2", map[string]interface{}{"ff": "value1", "gg": "value4"})
  91. // wait for table accumulation
  92. time.Sleep(100 * time.Millisecond)
  93. canctx, cancel := gocontext.WithCancel(gocontext.Background())
  94. defer cancel()
  95. go func() {
  96. for {
  97. select {
  98. case <-canctx.Done():
  99. return
  100. case <-time.After(10 * time.Millisecond):
  101. pubsub.Produce(ctx, "test", map[string]interface{}{"ff": "value4", "gg": "value2"})
  102. }
  103. }
  104. }()
  105. expected := []api.SourceTuple{
  106. api.NewDefaultSourceTuple(map[string]interface{}{"ff": "value1", "gg": "value2"}, map[string]interface{}{"topic": "test2"}),
  107. api.NewDefaultSourceTuple(map[string]interface{}{"ff": "value1", "gg": "value4"}, map[string]interface{}{"topic": "test2"}),
  108. }
  109. result, err := ls.Lookup(ctx, []string{}, []string{"ff"}, []interface{}{"value1"})
  110. if !reflect.DeepEqual(result, expected) {
  111. t.Errorf("expect %v but got %v", expected, result)
  112. }
  113. err = ls.Close(ctx)
  114. if err != nil {
  115. t.Error(err)
  116. return
  117. }
  118. }