source_pool_test.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. // Copyright 2021-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 node
  15. import (
  16. "testing"
  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/state"
  20. "github.com/lf-edge/ekuiper/pkg/api"
  21. "github.com/lf-edge/ekuiper/pkg/ast"
  22. )
  23. func TestSourcePool(t *testing.T) {
  24. n := NewSourceNode("test", ast.TypeStream, nil, &ast.Options{
  25. DATASOURCE: "demo",
  26. TYPE: "mock",
  27. SHARED: true,
  28. }, false, nil)
  29. n.concurrency = 2
  30. contextLogger := conf.Log.WithField("rule", "mockRule0")
  31. ctx := context.WithValue(context.Background(), context.LoggerKey, contextLogger)
  32. tempStore, _ := state.CreateStore("mockRule0", api.AtMostOnce)
  33. n.ctx = ctx.WithMeta("mockRule0", "test", tempStore)
  34. n1 := NewSourceNode("test", ast.TypeStream, nil, &ast.Options{
  35. DATASOURCE: "demo1",
  36. TYPE: "mock",
  37. SHARED: true,
  38. }, false, nil)
  39. contextLogger = conf.Log.WithField("rule", "mockRule1")
  40. ctx = context.WithValue(context.Background(), context.LoggerKey, contextLogger)
  41. tempStore, _ = state.CreateStore("mockRule1", api.AtMostOnce)
  42. n1.ctx = ctx.WithMeta("mockRule1", "test1", tempStore)
  43. n2 := NewSourceNode("test2", ast.TypeStream, nil, &ast.Options{
  44. DATASOURCE: "demo1",
  45. TYPE: "mock",
  46. }, false, nil)
  47. contextLogger = conf.Log.WithField("rule", "mockRule2")
  48. ctx = context.WithValue(context.Background(), context.LoggerKey, contextLogger)
  49. tempStore, _ = state.CreateStore("mockRule2", api.AtMostOnce)
  50. n2.ctx = ctx.WithMeta("mockRule2", "test2", tempStore)
  51. // Test add source instance
  52. getSourceInstance(n, 0)
  53. getSourceInstance(n1, 0)
  54. getSourceInstance(n, 1)
  55. getSourceInstance(n2, 0)
  56. poolLen := len(pool.registry)
  57. if poolLen != 1 {
  58. t.Errorf("source instances length unmatch: expect %d but got %d", 1, poolLen)
  59. return
  60. }
  61. si, ok := pool.registry["mock.test"]
  62. if !ok {
  63. t.Errorf("source instances pool unmatch: can't find key %s", "mock.test")
  64. return
  65. }
  66. outputLen := len(si.outputs)
  67. if outputLen != 3 {
  68. t.Errorf("source instances length unmatch: expect %d but got %d", 3, outputLen)
  69. return
  70. }
  71. removeSourceInstance(n)
  72. poolLen = len(pool.registry)
  73. if poolLen != 1 {
  74. t.Errorf("source instances length unmatch: expect %d but got %d", 1, poolLen)
  75. return
  76. }
  77. si, ok = pool.registry["mock.test"]
  78. if !ok {
  79. t.Errorf("source instances pool unmatch: can't find key %s", "mock.test")
  80. return
  81. }
  82. outputLen = len(si.outputs)
  83. if outputLen != 1 {
  84. t.Errorf("source instances length unmatch: expect %d but got %d", 1, outputLen)
  85. return
  86. }
  87. removeSourceInstance(n1)
  88. poolLen = len(pool.registry)
  89. if poolLen != 0 {
  90. t.Errorf("source instances length unmatch: expect %d but got %d", 0, poolLen)
  91. return
  92. }
  93. removeSourceInstance(n2)
  94. }