conf_test.go 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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 conf
  15. import (
  16. "fmt"
  17. "github.com/lf-edge/ekuiper/pkg/api"
  18. "reflect"
  19. "testing"
  20. )
  21. func TestSourceConfValidate(t *testing.T) {
  22. var tests = []struct {
  23. s *SourceConf
  24. e *SourceConf
  25. err string
  26. }{
  27. {
  28. s: &SourceConf{},
  29. e: &SourceConf{
  30. HttpServerIp: "0.0.0.0",
  31. HttpServerPort: 10081,
  32. },
  33. err: "invalidHttpServerPort:httpServerPort must between 0 and 65535",
  34. }, {
  35. s: &SourceConf{
  36. HttpServerIp: "192.168.0.1",
  37. },
  38. e: &SourceConf{
  39. HttpServerIp: "192.168.0.1",
  40. HttpServerPort: 10081,
  41. },
  42. err: "invalidHttpServerPort:httpServerPort must between 0 and 65535",
  43. }, {
  44. s: &SourceConf{
  45. HttpServerPort: 99999,
  46. },
  47. e: &SourceConf{
  48. HttpServerIp: "0.0.0.0",
  49. HttpServerPort: 10081,
  50. },
  51. err: "invalidHttpServerPort:httpServerPort must between 0 and 65535",
  52. }, {
  53. s: &SourceConf{
  54. HttpServerPort: 9090,
  55. HttpServerTls: &tlsConf{
  56. Certfile: "certfile",
  57. Keyfile: "keyfile",
  58. },
  59. },
  60. e: &SourceConf{
  61. HttpServerIp: "0.0.0.0",
  62. HttpServerPort: 9090,
  63. HttpServerTls: &tlsConf{
  64. Certfile: "certfile",
  65. Keyfile: "keyfile",
  66. },
  67. },
  68. },
  69. }
  70. fmt.Printf("The test bucket size is %d.\n\n", len(tests))
  71. for i, tt := range tests {
  72. err := tt.s.Validate()
  73. if err != nil && tt.err != err.Error() {
  74. t.Errorf("%d: error mismatch:\n exp=%s\n got=%s\n\n", i, tt.err, err)
  75. }
  76. if !reflect.DeepEqual(tt.s, tt.e) {
  77. t.Errorf("%d\n\nstmt mismatch:\n\nexp=%#v\n\ngot=%#v\n\n", i, tt.s, tt.e)
  78. }
  79. }
  80. }
  81. func TestRuleOptionValidate(t *testing.T) {
  82. var tests = []struct {
  83. s *api.RuleOption
  84. e *api.RuleOption
  85. err string
  86. }{
  87. {
  88. s: &api.RuleOption{},
  89. e: &api.RuleOption{},
  90. },
  91. {
  92. s: &api.RuleOption{
  93. LateTol: 1000,
  94. Concurrency: 1,
  95. BufferLength: 1024,
  96. CheckpointInterval: 300000, //5 minutes
  97. SendError: true,
  98. Restart: &api.RestartStrategy{
  99. Attempts: 0,
  100. Delay: 1000,
  101. Multiplier: 1,
  102. MaxDelay: 1000,
  103. JitterFactor: 0.1,
  104. },
  105. },
  106. e: &api.RuleOption{
  107. LateTol: 1000,
  108. Concurrency: 1,
  109. BufferLength: 1024,
  110. CheckpointInterval: 300000, //5 minutes
  111. SendError: true,
  112. Restart: &api.RestartStrategy{
  113. Attempts: 0,
  114. Delay: 1000,
  115. Multiplier: 1,
  116. MaxDelay: 1000,
  117. JitterFactor: 0.1,
  118. },
  119. },
  120. },
  121. {
  122. s: &api.RuleOption{
  123. LateTol: 1000,
  124. Concurrency: 1,
  125. BufferLength: 1024,
  126. CheckpointInterval: 300000, //5 minutes
  127. SendError: true,
  128. Restart: &api.RestartStrategy{
  129. Attempts: 3,
  130. Delay: 1000,
  131. Multiplier: 1,
  132. MaxDelay: 1000,
  133. JitterFactor: 0.1,
  134. },
  135. },
  136. e: &api.RuleOption{
  137. LateTol: 1000,
  138. Concurrency: 1,
  139. BufferLength: 1024,
  140. CheckpointInterval: 300000, //5 minutes
  141. SendError: true,
  142. Restart: &api.RestartStrategy{
  143. Attempts: 3,
  144. Delay: 1000,
  145. Multiplier: 1,
  146. MaxDelay: 1000,
  147. JitterFactor: 0.1,
  148. },
  149. },
  150. },
  151. {
  152. s: &api.RuleOption{
  153. LateTol: 1000,
  154. Concurrency: 1,
  155. BufferLength: 1024,
  156. CheckpointInterval: 300000, //5 minutes
  157. SendError: true,
  158. Restart: &api.RestartStrategy{
  159. Attempts: 3,
  160. Delay: 1000,
  161. Multiplier: 1.5,
  162. MaxDelay: 10000,
  163. JitterFactor: 0.1,
  164. },
  165. },
  166. e: &api.RuleOption{
  167. LateTol: 1000,
  168. Concurrency: 1,
  169. BufferLength: 1024,
  170. CheckpointInterval: 300000, //5 minutes
  171. SendError: true,
  172. Restart: &api.RestartStrategy{
  173. Attempts: 3,
  174. Delay: 1000,
  175. Multiplier: 1.5,
  176. MaxDelay: 10000,
  177. JitterFactor: 0.1,
  178. },
  179. },
  180. },
  181. {
  182. s: &api.RuleOption{
  183. LateTol: 1000,
  184. Concurrency: 1,
  185. BufferLength: 1024,
  186. CheckpointInterval: 300000, //5 minutes
  187. SendError: true,
  188. Restart: &api.RestartStrategy{
  189. Attempts: -2,
  190. Delay: 0,
  191. Multiplier: 0,
  192. MaxDelay: 0,
  193. JitterFactor: 1.1,
  194. },
  195. },
  196. e: &api.RuleOption{
  197. LateTol: 1000,
  198. Concurrency: 1,
  199. BufferLength: 1024,
  200. CheckpointInterval: 300000, //5 minutes
  201. SendError: true,
  202. Restart: &api.RestartStrategy{
  203. Attempts: 0,
  204. Delay: 1000,
  205. Multiplier: 2,
  206. MaxDelay: 1000,
  207. JitterFactor: 0.1,
  208. },
  209. },
  210. err: "multiple errors",
  211. },
  212. }
  213. fmt.Printf("The test bucket size is %d.\n\n", len(tests))
  214. for i, tt := range tests {
  215. err := ValidateRuleOption(tt.s)
  216. if err != nil && tt.err == "" {
  217. t.Errorf("%d: error mismatch:\n exp=%s\n got=%s\n\n", i, tt.err, err)
  218. }
  219. if !reflect.DeepEqual(tt.s, tt.e) {
  220. t.Errorf("%d\n\nstmt mismatch:\n\nexp=%#v\n\ngot=%#v\n\n", i, tt.s, tt.e)
  221. }
  222. }
  223. }