multiple_test.go 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. // Copyright 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 neuron
  15. import (
  16. "reflect"
  17. "sync"
  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/mock"
  23. "github.com/lf-edge/ekuiper/pkg/api"
  24. )
  25. // Test scenario of multiple neuron instances
  26. func TestMultiNeuron(t *testing.T) {
  27. // start and test 2 sources
  28. url1 := "tcp://127.0.0.1:33331"
  29. url2 := "tcp://127.0.0.1:33332"
  30. mc := conf.Clock.(*clock.Mock)
  31. exp1 := []api.SourceTuple{
  32. api.NewDefaultSourceTupleWithTime(map[string]interface{}{"group_name": "group1", "timestamp": 1646125996000.0, "node_name": "node1", "values": map[string]interface{}{"tag_name1": 11.22, "tag_name2": "yellow"}, "errors": map[string]interface{}{"tag_name3": 122.0}}, map[string]interface{}{"topic": "$$neuron_tcp://127.0.0.1:33331"}, mc.Now()),
  33. api.NewDefaultSourceTupleWithTime(map[string]interface{}{"group_name": "group1", "timestamp": 1646125996000.0, "node_name": "node1", "values": map[string]interface{}{"tag_name1": 11.22, "tag_name2": "green", "tag_name3": 60.0}, "errors": map[string]interface{}{}}, map[string]interface{}{"topic": "$$neuron_tcp://127.0.0.1:33331"}, mc.Now()),
  34. api.NewDefaultSourceTupleWithTime(map[string]interface{}{"group_name": "group1", "timestamp": 1646125996000.0, "node_name": "node1", "values": map[string]interface{}{"tag_name1": 15.4, "tag_name2": "green", "tag_name3": 70.0}, "errors": map[string]interface{}{}}, map[string]interface{}{"topic": "$$neuron_tcp://127.0.0.1:33331"}, mc.Now()),
  35. }
  36. exp2 := []api.SourceTuple{
  37. api.NewDefaultSourceTupleWithTime(map[string]interface{}{"group_name": "group1", "timestamp": 1646125996000.0, "node_name": "node1", "values": map[string]interface{}{"tag_name1": 11.22, "tag_name2": "yellow"}, "errors": map[string]interface{}{"tag_name3": 122.0}}, map[string]interface{}{"topic": "$$neuron_tcp://127.0.0.1:33332"}, mc.Now()),
  38. api.NewDefaultSourceTupleWithTime(map[string]interface{}{"group_name": "group1", "timestamp": 1646125996000.0, "node_name": "node1", "values": map[string]interface{}{"tag_name1": 11.22, "tag_name2": "green", "tag_name3": 60.0}, "errors": map[string]interface{}{}}, map[string]interface{}{"topic": "$$neuron_tcp://127.0.0.1:33332"}, mc.Now()),
  39. api.NewDefaultSourceTupleWithTime(map[string]interface{}{"group_name": "group1", "timestamp": 1646125996000.0, "node_name": "node1", "values": map[string]interface{}{"tag_name1": 15.4, "tag_name2": "green", "tag_name3": 70.0}, "errors": map[string]interface{}{}}, map[string]interface{}{"topic": "$$neuron_tcp://127.0.0.1:33332"}, mc.Now()),
  40. }
  41. s1 := GetSource()
  42. err := s1.Configure("new", map[string]interface{}{"url": url1})
  43. if err != nil {
  44. t.Errorf(err.Error())
  45. return
  46. }
  47. s2 := GetSource()
  48. err = s2.Configure("new2", map[string]interface{}{"url": url2})
  49. if err != nil {
  50. t.Errorf(err.Error())
  51. return
  52. }
  53. sin1 := GetSink()
  54. sin1.Configure(map[string]interface{}{
  55. "nodeName": "testM",
  56. "raw": false,
  57. "groupName": "grp",
  58. "url": url1,
  59. })
  60. sin2 := GetSink()
  61. sin2.Configure(map[string]interface{}{
  62. "nodeName": "testM",
  63. "raw": false,
  64. "groupName": "grp",
  65. "url": url2,
  66. })
  67. wg := sync.WaitGroup{}
  68. wg.Add(4)
  69. go func() {
  70. mock.TestSourceOpen(s1, exp1, t)
  71. wg.Done()
  72. }()
  73. go func() {
  74. mock.TestSourceOpen(s2, exp2, t)
  75. wg.Done()
  76. }()
  77. server1, ch1 := mockNeuron(true, true, url1)
  78. defer server1.Close()
  79. server2, ch2 := mockNeuron(true, true, url2)
  80. defer server2.Close()
  81. data1 := []interface{}{
  82. map[string]interface{}{
  83. "temperature": 22,
  84. "humidity": 50,
  85. "status": "green",
  86. },
  87. map[string]interface{}{
  88. "temperature": 25,
  89. "humidity": 82,
  90. "status": "wet",
  91. },
  92. map[string]interface{}{
  93. "temperature": 33,
  94. "humidity": 60,
  95. "status": "hot",
  96. },
  97. }
  98. data2 := []interface{}{
  99. map[string]interface{}{
  100. "temperature": 22,
  101. "humidity": 50,
  102. "status": "green",
  103. },
  104. map[string]interface{}{
  105. "temperature": 25,
  106. "humidity": 82,
  107. "status": "wet",
  108. },
  109. map[string]interface{}{
  110. "temperature": 33,
  111. "humidity": 60,
  112. "status": "hot",
  113. },
  114. }
  115. go func() {
  116. time.Sleep(100 * time.Millisecond)
  117. err = mock.RunSinkCollect(sin1, data1)
  118. if err != nil {
  119. t.Errorf(err.Error())
  120. return
  121. }
  122. wg.Done()
  123. }()
  124. go func() {
  125. time.Sleep(100 * time.Millisecond)
  126. err = mock.RunSinkCollect(sin2, data2)
  127. if err != nil {
  128. t.Errorf(err.Error())
  129. return
  130. }
  131. wg.Done()
  132. }()
  133. sexp1 := []string{
  134. `{"group_name":"grp","node_name":"testM","tag_name":"humidity","value":50}`,
  135. `{"group_name":"grp","node_name":"testM","tag_name":"status","value":"green"}`,
  136. `{"group_name":"grp","node_name":"testM","tag_name":"temperature","value":22}`,
  137. `{"group_name":"grp","node_name":"testM","tag_name":"humidity","value":82}`,
  138. `{"group_name":"grp","node_name":"testM","tag_name":"status","value":"wet"}`,
  139. `{"group_name":"grp","node_name":"testM","tag_name":"temperature","value":25}`,
  140. `{"group_name":"grp","node_name":"testM","tag_name":"humidity","value":60}`,
  141. `{"group_name":"grp","node_name":"testM","tag_name":"status","value":"hot"}`,
  142. `{"group_name":"grp","node_name":"testM","tag_name":"temperature","value":33}`,
  143. }
  144. sexp2 := []string{
  145. `{"group_name":"grp","node_name":"testM","tag_name":"humidity","value":50}`,
  146. `{"group_name":"grp","node_name":"testM","tag_name":"status","value":"green"}`,
  147. `{"group_name":"grp","node_name":"testM","tag_name":"temperature","value":22}`,
  148. `{"group_name":"grp","node_name":"testM","tag_name":"humidity","value":82}`,
  149. `{"group_name":"grp","node_name":"testM","tag_name":"status","value":"wet"}`,
  150. `{"group_name":"grp","node_name":"testM","tag_name":"temperature","value":25}`,
  151. `{"group_name":"grp","node_name":"testM","tag_name":"humidity","value":60}`,
  152. `{"group_name":"grp","node_name":"testM","tag_name":"status","value":"hot"}`,
  153. `{"group_name":"grp","node_name":"testM","tag_name":"temperature","value":33}`,
  154. }
  155. var actual1, actual2 []string
  156. ticker := time.After(10 * time.Second)
  157. for i := 0; i < len(sexp1)+len(sexp2); i++ {
  158. select {
  159. case <-ticker:
  160. t.Errorf("timeout")
  161. return
  162. case d := <-ch1:
  163. actual1 = append(actual1, string(d))
  164. case d2 := <-ch2:
  165. actual2 = append(actual2, string(d2))
  166. }
  167. }
  168. if !reflect.DeepEqual(actual1, sexp1) {
  169. t.Errorf("result mismatch:\n\nexp=%#v\n\ngot=%#v\n\n", sexp1, actual1)
  170. }
  171. if !reflect.DeepEqual(actual2, sexp2) {
  172. t.Errorf("result mismatch:\n\nexp=%#v\n\ngot=%#v\n\n", sexp2, actual2)
  173. }
  174. wg.Wait()
  175. }