neuron_test.go 5.7 KB

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