executors_msgpack_test.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. // Copyright 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. //go:build msgpack
  15. package service
  16. import (
  17. "net"
  18. "reflect"
  19. "testing"
  20. "github.com/msgpack-rpc/msgpack-rpc-go/rpc"
  21. "github.com/lf-edge/ekuiper/internal/topo/topotest"
  22. "github.com/lf-edge/ekuiper/pkg/api"
  23. )
  24. func TestMsgpackService(t *testing.T) {
  25. // mock server
  26. res := Resolver{"SayHello": reflect.ValueOf(SayHello), "object_detection": reflect.ValueOf(object_detection), "get_feature": reflect.ValueOf(get_feature), "getStatus": reflect.ValueOf(getStatus)}
  27. serv := rpc.NewServer(res, true, nil)
  28. l, _ := net.Listen("tcp", ":50000")
  29. serv.Listen(l)
  30. go serv.Run()
  31. // Comment out because the bug in the msgpack rpc
  32. // defer serv.Stop()
  33. // Reset
  34. streamList := []string{"helloStr", "commands", "fakeBin"}
  35. topotest.HandleStream(false, streamList, t)
  36. // Data setup
  37. tests := []topotest.RuleTest{
  38. {
  39. Name: `TestRestRule1`,
  40. Sql: `SELECT helloFromMsgpack(name) as wc FROM helloStr`,
  41. R: [][]map[string]interface{}{
  42. {{
  43. "wc": map[string]interface{}{
  44. "message": "world",
  45. },
  46. }},
  47. {{
  48. "wc": map[string]interface{}{
  49. "message": "golang",
  50. },
  51. }},
  52. {{
  53. "wc": map[string]interface{}{
  54. "message": "peacock",
  55. },
  56. }},
  57. },
  58. M: map[string]interface{}{
  59. "op_2_project_0_exceptions_total": int64(0),
  60. "op_2_project_0_process_latency_us": int64(0),
  61. "op_2_project_0_records_in_total": int64(3),
  62. "op_2_project_0_records_out_total": int64(3),
  63. "sink_mockSink_0_exceptions_total": int64(0),
  64. "sink_mockSink_0_records_in_total": int64(3),
  65. "sink_mockSink_0_records_out_total": int64(3),
  66. },
  67. }, {
  68. Name: `TestRestRule2`,
  69. Sql: `SELECT objectDetectFromMsgpack(*)->result FROM commands`,
  70. R: [][]map[string]interface{}{
  71. {{
  72. "kuiper_field_0": "get success",
  73. }},
  74. {{
  75. "kuiper_field_0": "detect success",
  76. }},
  77. {{
  78. "kuiper_field_0": "delete success",
  79. }},
  80. },
  81. M: map[string]interface{}{
  82. "op_2_project_0_exceptions_total": int64(0),
  83. "op_2_project_0_process_latency_us": int64(0),
  84. "op_2_project_0_records_in_total": int64(3),
  85. "op_2_project_0_records_out_total": int64(3),
  86. "sink_mockSink_0_exceptions_total": int64(0),
  87. "sink_mockSink_0_records_in_total": int64(3),
  88. "sink_mockSink_0_records_out_total": int64(3),
  89. },
  90. }, {
  91. Name: `TestRestRule3`,
  92. Sql: `SELECT getFeatureFromMsgpack(self)->feature[0]->box->h FROM fakeBin`,
  93. R: [][]map[string]interface{}{
  94. {{
  95. "kuiper_field_0": float64(106), // Convert by the testing tool
  96. }},
  97. {{
  98. "kuiper_field_0": float64(107),
  99. }},
  100. {{
  101. "kuiper_field_0": float64(108),
  102. }},
  103. },
  104. M: map[string]interface{}{
  105. "op_2_project_0_exceptions_total": int64(0),
  106. "op_2_project_0_process_latency_us": int64(0),
  107. "op_2_project_0_records_in_total": int64(3),
  108. "op_2_project_0_records_out_total": int64(3),
  109. "sink_mockSink_0_exceptions_total": int64(0),
  110. "sink_mockSink_0_records_in_total": int64(3),
  111. "sink_mockSink_0_records_out_total": int64(3),
  112. },
  113. //}, {
  114. // Name: `TestRestRule4`,
  115. // Sql: `SELECT getStatusFromMsgpack(), command FROM commands`,
  116. // R: [][]map[string]interface{}{
  117. // {{
  118. // "getStatusFromRest": true,
  119. // "command": "get",
  120. // }},
  121. // {{
  122. // "getStatusFromRest": true,
  123. // "command": "detect",
  124. // }},
  125. // {{
  126. // "getStatusFromRest": true,
  127. // "command": "delete",
  128. // }},
  129. // },
  130. // M: map[string]interface{}{
  131. // "op_2_project_0_exceptions_total": int64(0),
  132. // "op_2_project_0_process_latency_us": int64(0),
  133. // "op_2_project_0_records_in_total": int64(3),
  134. // "op_2_project_0_records_out_total": int64(3),
  135. //
  136. // "sink_mockSink_0_exceptions_total": int64(0),
  137. // "sink_mockSink_0_records_in_total": int64(3),
  138. // "sink_mockSink_0_records_out_total": int64(3),
  139. // },
  140. },
  141. }
  142. topotest.HandleStream(true, streamList, t)
  143. topotest.DoRuleTest(t, tests, 0, &api.RuleOption{
  144. BufferLength: 100,
  145. SendError: true,
  146. }, 0)
  147. }