connection_test.go 8.0 KB

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