neuron_test.go 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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 neuron
  15. import (
  16. "fmt"
  17. "log"
  18. "reflect"
  19. "sync"
  20. "testing"
  21. "time"
  22. "github.com/benbjohnson/clock"
  23. "go.nanomsg.org/mangos/v3"
  24. "go.nanomsg.org/mangos/v3/protocol/pair"
  25. _ "go.nanomsg.org/mangos/v3/transport/ipc"
  26. "github.com/lf-edge/ekuiper/internal/conf"
  27. "github.com/lf-edge/ekuiper/internal/io/mock"
  28. "github.com/lf-edge/ekuiper/pkg/api"
  29. )
  30. var data = [][]byte{
  31. []byte("{\"timestamp\": 1646125996000, \"node_name\": \"node1\", \"group_name\": \"group1\", \"values\": {\"tag_name1\": 11.22, \"tag_name2\": \"yellow\"}, \"errors\": {\"tag_name3\": 122}}"),
  32. []byte(`{"timestamp": 1646125996000, "node_name": "node1", "group_name": "group1", "values": {"tag_name1": 11.22, "tag_name2": "green","tag_name3":60}, "errors": {}}`),
  33. []byte(`{"timestamp": 1646125996000, "node_name": "node1", "group_name": "group1", "values": {"tag_name1": 15.4, "tag_name2": "green","tag_name3":70}, "errors": {}}`),
  34. }
  35. // mockNeuron start the nng pair server
  36. func mockNeuron(send bool, recv bool, url string) (mangos.Socket, chan []byte) {
  37. var (
  38. sock mangos.Socket
  39. err error
  40. ch chan []byte
  41. )
  42. if sock, err = pair.NewSocket(); err != nil {
  43. log.Fatalf("can't get new pair socket: %s", err)
  44. }
  45. if err = sock.Listen(url); err != nil {
  46. log.Fatalf("can't listen on pair socket: %s", err.Error())
  47. }
  48. log.Printf("listen on pair socket")
  49. if recv {
  50. ch = make(chan []byte)
  51. go func() {
  52. for {
  53. var msg []byte
  54. var err error
  55. if msg, err = sock.Recv(); err == nil {
  56. fmt.Printf("Neuron RECEIVED: \"%s\"\n", string(msg))
  57. ch <- msg
  58. fmt.Println("Neuron Sent out")
  59. }
  60. }
  61. }()
  62. }
  63. if send {
  64. go func() {
  65. for _, msg := range data {
  66. time.Sleep(10 * time.Millisecond)
  67. fmt.Printf("Neuron SENDING \"%s\"\n", msg)
  68. if err := sock.Send(msg); err != nil {
  69. fmt.Printf("failed sending: %s\n", err)
  70. }
  71. }
  72. }()
  73. }
  74. return sock, ch
  75. }
  76. // Test scenario of multiple neuron sources and sinks
  77. func TestMultiSourceSink(t *testing.T) {
  78. mc := conf.Clock.(*clock.Mock)
  79. // start and test 2 sources
  80. exp := []api.SourceTuple{
  81. 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_ipc:///tmp/neuron-ekuiper.ipc"}, mc.Now()),
  82. 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_ipc:///tmp/neuron-ekuiper.ipc"}, mc.Now()),
  83. 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_ipc:///tmp/neuron-ekuiper.ipc"}, mc.Now()),
  84. }
  85. s1 := GetSource()
  86. err := s1.Configure("new", nil)
  87. if err != nil {
  88. t.Errorf(err.Error())
  89. return
  90. }
  91. s2 := GetSource()
  92. err = s2.Configure("new2", nil)
  93. if err != nil {
  94. t.Errorf(err.Error())
  95. return
  96. }
  97. sin := GetSink()
  98. sin.Configure(map[string]interface{}{
  99. "nodeName": "testM",
  100. "raw": false,
  101. "groupName": "grp",
  102. })
  103. wg := sync.WaitGroup{}
  104. wg.Add(3)
  105. go func() {
  106. mock.TestSourceOpen(s1, exp, t)
  107. wg.Done()
  108. }()
  109. go func() {
  110. mock.TestSourceOpen(s2, exp, t)
  111. wg.Done()
  112. }()
  113. // let the server start after the rule to test async dial behavior
  114. server, ch := mockNeuron(true, true, "ipc:///tmp/neuron-ekuiper.ipc")
  115. data := []interface{}{
  116. map[string]interface{}{
  117. "temperature": 22,
  118. "humidity": 50,
  119. "status": "green",
  120. },
  121. map[string]interface{}{
  122. "temperature": 25,
  123. "humidity": 82,
  124. "status": "wet",
  125. },
  126. map[string]interface{}{
  127. "temperature": 33,
  128. "humidity": 60,
  129. "status": "hot",
  130. },
  131. }
  132. go func() {
  133. time.Sleep(100 * time.Millisecond)
  134. err = mock.RunSinkCollect(sin, data)
  135. if err != nil {
  136. t.Errorf(err.Error())
  137. return
  138. }
  139. wg.Done()
  140. }()
  141. sexp := []string{
  142. `{"group_name":"grp","node_name":"testM","tag_name":"humidity","value":50}`,
  143. `{"group_name":"grp","node_name":"testM","tag_name":"status","value":"green"}`,
  144. `{"group_name":"grp","node_name":"testM","tag_name":"temperature","value":22}`,
  145. `{"group_name":"grp","node_name":"testM","tag_name":"humidity","value":82}`,
  146. `{"group_name":"grp","node_name":"testM","tag_name":"status","value":"wet"}`,
  147. `{"group_name":"grp","node_name":"testM","tag_name":"temperature","value":25}`,
  148. `{"group_name":"grp","node_name":"testM","tag_name":"humidity","value":60}`,
  149. `{"group_name":"grp","node_name":"testM","tag_name":"status","value":"hot"}`,
  150. `{"group_name":"grp","node_name":"testM","tag_name":"temperature","value":33}`,
  151. }
  152. var actual []string
  153. ticker := time.After(10 * time.Second)
  154. for i := 0; i < len(sexp); i++ {
  155. select {
  156. case <-ticker:
  157. t.Errorf("timeout")
  158. return
  159. case d := <-ch:
  160. actual = append(actual, string(d))
  161. }
  162. }
  163. if !reflect.DeepEqual(actual, sexp) {
  164. t.Errorf("result mismatch:\n\nexp=%#v\n\ngot=%#v\n\n", sexp, actual)
  165. }
  166. wg.Wait()
  167. server.Close()
  168. time.Sleep(100 * time.Millisecond)
  169. sinkTest(t)
  170. sinkConnExpTest(t)
  171. connectFailTest(t)
  172. }