test_source.go 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  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 mock
  15. import (
  16. "reflect"
  17. "testing"
  18. "time"
  19. "github.com/lf-edge/ekuiper/sdk/go/api"
  20. )
  21. func TestSourceOpen(r api.Source, exp []api.SourceTuple, t *testing.T) {
  22. ctx, cancel := newMockContext("rule1", "op1").WithCancel()
  23. consumer := make(chan api.SourceTuple)
  24. errCh := make(chan error)
  25. go r.Open(ctx, consumer, errCh)
  26. ticker := time.After(10 * time.Second)
  27. limit := len(exp)
  28. var result []api.SourceTuple
  29. outerloop:
  30. for {
  31. select {
  32. case err := <-errCh:
  33. t.Errorf("received error: %v", err)
  34. cancel()
  35. return
  36. case tuple := <-consumer:
  37. result = append(result, tuple)
  38. limit--
  39. if limit <= 0 {
  40. break outerloop
  41. }
  42. case <-ticker:
  43. t.Errorf("stop after timeout")
  44. t.Errorf("expect %v, but got %v", exp, result)
  45. cancel()
  46. return
  47. }
  48. }
  49. err := r.Close(ctx)
  50. if err != nil {
  51. t.Errorf(err.Error())
  52. return
  53. }
  54. if !reflect.DeepEqual(exp, result) {
  55. t.Errorf("result mismatch:\n exp=%s\n got=%s\n\n", exp, result)
  56. }
  57. }