random_source_test.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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 main
  15. import (
  16. "fmt"
  17. "github.com/lf-edge/ekuiper/sdk/go/api"
  18. "github.com/lf-edge/ekuiper/sdk/go/mock"
  19. "reflect"
  20. "testing"
  21. )
  22. func TestConfigure(t *testing.T) {
  23. var tests = []struct {
  24. p map[string]interface{}
  25. err string
  26. }{
  27. {
  28. p: map[string]interface{}{
  29. "interval": float64(-2),
  30. "seed": float64(1),
  31. "pattern": map[string]interface{}{"count": float64(5)},
  32. "deduplicate": 0,
  33. },
  34. err: "source `random` property `interval` must be a positive integer but got -2",
  35. }, {
  36. p: map[string]interface{}{
  37. "intervala": float64(-2),
  38. "seed": float64(1),
  39. "pattern": map[string]interface{}{"count": float64(5)},
  40. "deduplicate": 0,
  41. },
  42. err: "source `random` property `interval` must be a positive integer but got 0",
  43. }, {
  44. p: map[string]interface{}{
  45. "interval": float64(200),
  46. "seed": float64(-1),
  47. "pattern": map[string]interface{}{"count": float64(5)},
  48. "deduplicate": 0,
  49. },
  50. err: "source `random` property `seed` must be a positive integer but got -1",
  51. }, {
  52. p: map[string]interface{}{
  53. "interval": float64(5),
  54. "seed": float64(5),
  55. "deduplicate": 0,
  56. },
  57. err: "source `random` property `pattern` is required",
  58. },
  59. }
  60. fmt.Printf("The test bucket size is %d.\n\n", len(tests))
  61. for i, tt := range tests {
  62. r := &randomSource{}
  63. err := r.Configure("new", tt.p)
  64. if !reflect.DeepEqual(tt.err, Errstring(err)) {
  65. t.Errorf("%d. %q: error mismatch:\n exp=%s\n got=%s\n\n", i, err, tt.err, err)
  66. }
  67. }
  68. }
  69. func TestRunRandom(t *testing.T) {
  70. exp := []api.SourceTuple{
  71. api.NewDefaultSourceTuple(map[string]interface{}{"count": 50}, nil),
  72. api.NewDefaultSourceTuple(map[string]interface{}{"count": 50}, nil),
  73. api.NewDefaultSourceTuple(map[string]interface{}{"count": 50}, nil),
  74. api.NewDefaultSourceTuple(map[string]interface{}{"count": 50}, nil),
  75. api.NewDefaultSourceTuple(map[string]interface{}{"count": 50}, nil),
  76. }
  77. p := map[string]interface{}{
  78. "interval": float64(100),
  79. "seed": float64(1), // Use seed = 1, to make sure results always the same
  80. "pattern": map[string]interface{}{"count": float64(50)},
  81. "deduplicate": 0,
  82. }
  83. r := &randomSource{}
  84. err := r.Configure("new", p)
  85. if err != nil {
  86. t.Errorf(err.Error())
  87. return
  88. }
  89. mock.TestSourceOpen(r, exp, t)
  90. }
  91. func Errstring(err error) string {
  92. if err != nil {
  93. return err.Error()
  94. }
  95. return ""
  96. }