connection_test.go 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. // Copyright 2021-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 runtime
  15. import (
  16. "fmt"
  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. "go.nanomsg.org/mangos/v3"
  22. "go.nanomsg.org/mangos/v3/protocol/pull"
  23. "go.nanomsg.org/mangos/v3/protocol/push"
  24. "go.nanomsg.org/mangos/v3/protocol/req"
  25. "reflect"
  26. "testing"
  27. "time"
  28. )
  29. var okMsg = []byte("ok")
  30. func TestControlCh(t *testing.T) {
  31. pluginName := "test"
  32. // 1. normal process
  33. ch, err := CreateControlChannel(pluginName)
  34. if err != nil {
  35. t.Errorf("normal process: create channel error %v", err)
  36. return
  37. }
  38. client, err := createMockControlChannel(pluginName)
  39. if err != nil {
  40. t.Errorf("normal process: create client error %v", err)
  41. return
  42. }
  43. clientStopped := false
  44. go func() {
  45. err := client.Run()
  46. if err != nil && !clientStopped {
  47. t.Errorf("normal process: client error %v", err)
  48. }
  49. fmt.Printf("exiting normal client\n")
  50. }()
  51. err = ch.Handshake()
  52. if err != nil {
  53. t.Errorf("normal process: handshake error %v", err)
  54. }
  55. sendCount := 0
  56. for {
  57. sendCount++
  58. err = ch.SendCmd(okMsg)
  59. if err != nil {
  60. t.Errorf("normal process: %d sendCmd error %v", sendCount, err)
  61. }
  62. if sendCount >= 3 {
  63. break
  64. }
  65. }
  66. err = ch.Close()
  67. if err != nil {
  68. t.Errorf("normal process: close error %v", err)
  69. }
  70. // 2. client not closed, channel is still occupied?
  71. ch, err = CreateControlChannel(pluginName)
  72. if err != nil {
  73. t.Errorf("2nd process: recreate channel error %v", err)
  74. }
  75. // 3. server not started
  76. err = ch.Close()
  77. if err != nil {
  78. t.Errorf("normal process: close error %v", err)
  79. }
  80. clientStopped = true
  81. err = client.Close()
  82. if err != nil {
  83. t.Errorf("3rd process: close client error %v", err)
  84. }
  85. _, err = createMockControlChannel(pluginName)
  86. if err == nil || err.Error() == "" {
  87. t.Errorf("3rd process: create client should have error but got %v", err)
  88. return
  89. }
  90. // 4. double control channel client
  91. ch, err = CreateControlChannel(pluginName)
  92. if err != nil {
  93. t.Errorf("4th process: create channel error %v", err)
  94. }
  95. client, err = createMockControlChannel(pluginName)
  96. if err != nil {
  97. t.Errorf("4th process: create client error %v", err)
  98. }
  99. clientStopped = false
  100. go func() {
  101. err := client.Run()
  102. if err != nil && !clientStopped {
  103. t.Errorf("4th process: client error %v", err)
  104. }
  105. fmt.Printf("exiting 4th process client\n")
  106. }()
  107. // 5. no handshake
  108. err = ch.SendCmd(okMsg)
  109. if err != nil {
  110. t.Errorf("5th process: send command should auto handshake but got %v", err)
  111. }
  112. err = ch.Handshake()
  113. if err != nil {
  114. t.Errorf("5th process: handshake error %v", err)
  115. }
  116. err = ch.SendCmd(okMsg)
  117. if err != nil {
  118. t.Errorf("5th process: sendCmd error %v", err)
  119. }
  120. err = ch.Close()
  121. if err != nil {
  122. t.Errorf("5th process: close error %v", err)
  123. }
  124. clientStopped = true
  125. err = client.Close()
  126. if err != nil {
  127. t.Errorf("5th process: client close error %v", err)
  128. }
  129. }
  130. func TestDataIn(t *testing.T) {
  131. i := 0
  132. ctx := context.DefaultContext{}
  133. sctx := ctx.WithMeta("rule1", "op1", &state.MemoryStore{}).WithInstance(1)
  134. for i < 2 { // normal start and restart
  135. ch, err := CreateSourceChannel(sctx)
  136. if err != nil {
  137. t.Errorf("phase %d create channel error %v", i, err)
  138. }
  139. client, err := createMockSourceChannel(sctx)
  140. if err != nil {
  141. t.Errorf("phase %d create client error %v", i, err)
  142. }
  143. go func() {
  144. var c = 0
  145. for c < 3 {
  146. err := client.Send(okMsg)
  147. if err != nil {
  148. t.Errorf("phase %d client send error %v", i, err)
  149. return
  150. }
  151. conf.Log.Debugf("phase %d sent %d messages", i, c)
  152. c++
  153. }
  154. }()
  155. var c = 0
  156. for c < 3 {
  157. msg, err := ch.Recv()
  158. if err != nil {
  159. t.Errorf("phase %d receive error %v", i, err)
  160. return
  161. }
  162. if !reflect.DeepEqual(msg, okMsg) {
  163. t.Errorf("phase %d receive %s but expect %s", i, msg, okMsg)
  164. }
  165. c++
  166. }
  167. err = ch.Close()
  168. if err != nil {
  169. t.Errorf("phase %d close error %v", i, err)
  170. }
  171. client.Close()
  172. if err != nil {
  173. t.Errorf("phase %d close client error %v", i, err)
  174. }
  175. i++
  176. }
  177. }
  178. func TestDataOut(t *testing.T) {
  179. i := 0
  180. ctx := context.DefaultContext{}
  181. sctx := ctx.WithMeta("rule1", "op1", &state.MemoryStore{}).WithInstance(1)
  182. for i < 2 { // normal start and restart
  183. client, err := createMockSinkChannel(sctx)
  184. if err != nil {
  185. t.Errorf("phase %d create client error %v", i, err)
  186. }
  187. ch, err := CreateSinkChannel(sctx)
  188. if err != nil {
  189. t.Errorf("phase %d create channel error %v", i, err)
  190. }
  191. go func() {
  192. var c = 0
  193. for c < 3 {
  194. err := ch.Send(okMsg)
  195. if err != nil {
  196. t.Errorf("phase %d client send error %v", i, err)
  197. return
  198. }
  199. conf.Log.Debugf("phase %d sent %d messages", i, c)
  200. c++
  201. }
  202. }()
  203. var c = 0
  204. for c < 3 {
  205. msg, err := client.Recv()
  206. if err != nil {
  207. t.Errorf("phase %d receive error %v", i, err)
  208. return
  209. }
  210. if !reflect.DeepEqual(msg, okMsg) {
  211. t.Errorf("phase %d receive %s but expect %s", i, msg, okMsg)
  212. }
  213. c++
  214. }
  215. err = ch.Close()
  216. if err != nil {
  217. t.Errorf("phase %d close error %v", i, err)
  218. }
  219. client.Close()
  220. if err != nil {
  221. t.Errorf("phase %d close client error %v", i, err)
  222. }
  223. i++
  224. }
  225. }
  226. type mockControlClient struct {
  227. sock mangos.Socket
  228. }
  229. // Run until process end
  230. func (r *mockControlClient) Run() error {
  231. err := r.sock.Send([]byte("handshake"))
  232. if err != nil {
  233. return fmt.Errorf("can't send handshake: %s", err.Error())
  234. }
  235. for {
  236. msg, err := r.sock.Recv()
  237. if err != nil {
  238. return fmt.Errorf("cannot receive on rep socket: %s", err.Error())
  239. }
  240. if !reflect.DeepEqual(msg, okMsg) {
  241. return fmt.Errorf("control client recieve %s but expect %s", string(msg), string(okMsg))
  242. }
  243. err = r.sock.Send(okMsg)
  244. if err != nil {
  245. return fmt.Errorf("can't send reply: %s", err.Error())
  246. }
  247. }
  248. return nil
  249. }
  250. func (r *mockControlClient) Close() error {
  251. return r.sock.Close()
  252. }
  253. func createMockControlChannel(pluginName string) (*mockControlClient, error) {
  254. var (
  255. sock mangos.Socket
  256. err error
  257. )
  258. if sock, err = req.NewSocket(); err != nil {
  259. return nil, fmt.Errorf("can't get new req socket: %s", err)
  260. }
  261. url := fmt.Sprintf("ipc:///tmp/plugin_%s.ipc", pluginName)
  262. if err = sock.Dial(url); err != nil {
  263. return nil, fmt.Errorf("can't dial on req socket: %s", err.Error())
  264. }
  265. return &mockControlClient{sock: sock}, nil
  266. }
  267. func createMockSourceChannel(ctx api.StreamContext) (mangos.Socket, error) {
  268. var (
  269. sock mangos.Socket
  270. err error
  271. )
  272. if sock, err = push.NewSocket(); err != nil {
  273. return nil, fmt.Errorf("can't get new push socket: %s", err)
  274. }
  275. setSockOptions(sock, map[string]interface{}{
  276. mangos.OptionRecvDeadline: 500 * time.Millisecond,
  277. })
  278. url := fmt.Sprintf("ipc:///tmp/%s_%s_%d.ipc", ctx.GetRuleId(), ctx.GetOpId(), ctx.GetInstanceId())
  279. if err = sock.Dial(url); err != nil {
  280. return nil, fmt.Errorf("can't dial on push socket: %s", err.Error())
  281. }
  282. return sock, nil
  283. }
  284. func createMockSinkChannel(ctx api.StreamContext) (mangos.Socket, error) {
  285. var (
  286. sock mangos.Socket
  287. err error
  288. )
  289. if sock, err = pull.NewSocket(); err != nil {
  290. return nil, fmt.Errorf("can't get new pull socket: %s", err)
  291. }
  292. setSockOptions(sock, map[string]interface{}{
  293. mangos.OptionSendDeadline: 1000 * time.Millisecond,
  294. })
  295. url := fmt.Sprintf("ipc:///tmp/%s_%s_%d.ipc", ctx.GetRuleId(), ctx.GetOpId(), ctx.GetInstanceId())
  296. if err = listenWithRetry(sock, url); err != nil {
  297. return nil, fmt.Errorf("can't listen on pull socket for %s: %s", url, err.Error())
  298. }
  299. return sock, nil
  300. }