connection_test.go 7.9 KB

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