source_pool_test.go 2.6 KB

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