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