source_test.go 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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 memory
  15. import (
  16. "fmt"
  17. "github.com/lf-edge/ekuiper/internal/testx"
  18. "reflect"
  19. "testing"
  20. )
  21. func TestTopic(t *testing.T) {
  22. var tests = []struct {
  23. wildcard string
  24. topics []string
  25. results []bool
  26. err string
  27. }{
  28. {
  29. wildcard: "rule1/op1/+/temperature",
  30. topics: []string{
  31. "rule1/op1/ins1/temperature",
  32. "rule1/op1/ins2/temperature",
  33. "rule1/op1/ins1/humidity",
  34. "rule1/op2/ins1/temperature",
  35. "rule1/op1/ins1/sensor1/temperature",
  36. },
  37. results: []bool{
  38. true, true, false, false, false,
  39. },
  40. }, {
  41. wildcard: "rule1/op1/#",
  42. topics: []string{
  43. "rule1/op1/ins1/temperature",
  44. "rule1/op1/ins2/temperature",
  45. "rule1/op1/ins1/humidity",
  46. "rule1/op2/ins1/temperature",
  47. "rule1/op1/ins1/sensor1/temperature",
  48. },
  49. results: []bool{
  50. true, true, true, false, true,
  51. },
  52. }, {
  53. wildcard: "rule1/+/+/temperature",
  54. topics: []string{
  55. "rule1/op1/ins1/temperature",
  56. "rule1/op1/ins2/temperature",
  57. "rule1/op1/ins1/humidity",
  58. "rule1/op2/ins1/temperature",
  59. "rule1/op1/ins1/sensor1/temperature",
  60. },
  61. results: []bool{
  62. true, true, false, true, false,
  63. },
  64. }, {
  65. wildcard: "rule1/#/temperature",
  66. err: "invalid topic rule1/#/temperature: # must at the last level",
  67. },
  68. }
  69. fmt.Printf("The test bucket size is %d.\n\n", len(tests))
  70. for i, tt := range tests {
  71. r, err := getRegexp(tt.wildcard)
  72. if err != nil {
  73. if !reflect.DeepEqual(tt.err, testx.Errstring(err)) {
  74. t.Errorf("%d: error mismatch:\n exp=%s\n got=%s\n\n", i, tt.err, err)
  75. }
  76. continue
  77. }
  78. results := make([]bool, len(tt.topics))
  79. for j, topic := range tt.topics {
  80. results[j] = r.MatchString(topic)
  81. }
  82. if !reflect.DeepEqual(tt.results, results) {
  83. t.Errorf("%d\n\nstmt mismatch:\n\nexp=%#v\n\ngot=%#v\n\n", i, tt.results, results)
  84. }
  85. }
  86. }